嘿,伙计们,谢谢你们能提供的帮助。我需要一点正则表达式的帮助,这远远超出了我的知识。
我有一个列表框,里面有一个文件名,例如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 04:54:13
根据您的示例,我创建了以下regexp:
\|(.)(.*)\|(.*)\{\d{2}(\d{2})\-(\d{2}).*(\..*)
结果应该如下:
group1 + "\\" + group2 + "\\" + group3 + group5 + group4 + group6
如果你不满意,你可以随时给它一个旋转自己here。
编辑:
记住我关于命名组的内容后:
\|(?<drive>.)(?<path>.*)\|(?<prefix>.*)\{\d{2}(?<number2>\d{2})\-(?<number1>\d{2}).*(?<extension>\..*)
drive + "\\" + path + "\\" + prefix + number1 + number2 + extension
发布于 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;
}
发布于 2010-01-12 04:44:13
你的例子看起来不是很清楚。
也就是说,
/.*\\(.*)-[^-]*$/
将捕获最后一个反斜杠和最后一个连字符之间的所有文本。
https://stackoverflow.com/questions/2044745
复制相似问题