嘿,伙计们,谢谢你们能提供的帮助。我需要一点正则表达式的帮助,这远远超出了我的知识。
我有一个列表框,里面有一个文件名,例如3123~101,它是一个有1行文本的分隔文件。我需要对文本文件中最后一个"-“之前的最后一个"\”之后的所有内容进行正则表达式。结束的遗嘱可以包含一个前缀,然后###{#-004587}.txt~公式是{### + ~# -1。
文件名: 3123~101 So示例1: 3123|X:directory\Path\Directory|Pre0{0442-0500}.txt
结果: X:\directory\Path\Directory\Pre00542.txt
文件名: 3123~101 So示例1: 3123|X:directory\Path\Directory|0{0442-0500}.txt
结果: X:\directory\Path\Directory\00542.txt
发布于 2010-01-12 23:42:54
对gbacon的回答稍有不同,也适用于旧版本的.Net:
static void Main(string[] args)
{
Console.WriteLine(Adjust("3123~101", @"3123|X:directory\Path\Directory|Pre0{0442-0500}.txt"));
Console.WriteLine(Adjust("3123~101", @"3123|X:directory\Path\Directory|0{0442-0500}.txt"));
}
private static string Adjust(string name, string file)
{
Regex nameParse = new Regex(@"\d*~(?<value>\d*)");
Regex fileParse = new Regex(@"\d*\|(?<drive>[A-Za-z]):(?<path>[^\|]*)\|(?<prefix>[^{]*){(?<code>\d*)");
Match nameMatch = nameParse.Match(name);
Match fileMatch = fileParse.Match(file);
int value = Convert.ToInt32(nameMatch.Groups["value"].Value);
int code = Convert.ToInt32(fileMatch.Groups["code"].Value);
code = code + value - 1;
string drive = fileMatch.Groups["drive"].Value;
string path = fileMatch.Groups["path"].Value;
string prefix = fileMatch.Groups["prefix"].Value;
string result = string.Format(@"{0}:\{1}\{2}{3:0000}.txt",
drive,
path,
prefix,
code);
return result;
}https://stackoverflow.com/questions/2044745
复制相似问题