C# 复杂正则表达式
嘿,伙计们,谢谢你们能提供的帮助。我需要一点regex的帮助,这远远超出了我的知识范围 我有一个带有文件名的列表框,例如3123~101,一个带分隔符的文件,其中有一行文本。我需要在文本文件中的last之后\在last之前正则化所有内容。结尾可能包含前缀{-004587}.txt~公式为{+~-1 文件名: 3123~101 因此,示例1: 3123 | X:directory\Path\directory | Pre0{0442-0500}.txt 结果: X:\directory\Path\directory\Pre00542.txt 文件名: 3123~101 因此,示例1: 3123 | X:directory\Path\directory | 0{0442-0500}.txt 结果:C# 复杂正则表达式,c#,regex,C#,Regex,嘿,伙计们,谢谢你们能提供的帮助。我需要一点regex的帮助,这远远超出了我的知识范围 我有一个带有文件名的列表框,例如3123~101,一个带分隔符的文件,其中有一行文本。我需要在文本文件中的last之后\在last之前正则化所有内容。结尾可能包含前缀{-004587}.txt~公式为{+~-1 文件名: 3123~101 因此,示例1: 3123 | X:directory\Path\directory | Pre0{0442-0500}.txt 结果: X:\directory\Path\
X:\directory\Path\directory\00542.txt您的示例似乎不太清楚 也就是说
/.*\\(.*)-[^-]*$/
将捕获最后一个反斜杠和最后一个连字符之间匹配的所有文本。您的示例中似乎不太清楚 也就是说
/.*\\(.*)-[^-]*$/
将捕获最后一个反斜杠和最后一个连字符之间匹配的所有文本。根据您的示例,我创建了以下regexp:
\|(.)(.*)\|(.*)\{\d{2}(\d{2})\-(\d{2}).*(\..*)
结果应如下所示:
group1 + "\\" + group2 + "\\" + group3 + group5 + group4 + group6
public static void Main(string[] args)
{
Console.WriteLine(AdjustPath("3123~101", @"3123|X:directory\Path\Directory|Pre0{0442-0500}.txt"));
Console.WriteLine(AdjustPath("3123~101", @"3123|X:directory\Path\Directory|0{0442-0500}.txt"));
}
如果你不满意的话,你可以自己试一试
编辑:
在记住我有关命名组的内容后:
\|(?<drive>.)(?<path>.*)\|(?<prefix>.*)\{\d{2}(?<number2>\d{2})\-(?<number1>\d{2}).*(?<extension>\..*)
drive + "\\" + path + "\\" + prefix + number1 + number2 + extension
根据您的示例,我创建了以下regexp:
\|(.)(.*)\|(.*)\{\d{2}(\d{2})\-(\d{2}).*(\..*)
结果应如下所示:
group1 + "\\" + group2 + "\\" + group3 + group5 + group4 + group6
public static void Main(string[] args)
{
Console.WriteLine(AdjustPath("3123~101", @"3123|X:directory\Path\Directory|Pre0{0442-0500}.txt"));
Console.WriteLine(AdjustPath("3123~101", @"3123|X:directory\Path\Directory|0{0442-0500}.txt"));
}
如果你不满意的话,你可以自己试一试
编辑:
在记住我有关命名组的内容后:
\|(?<drive>.)(?<path>.*)\|(?<prefix>.*)\{\d{2}(?<number2>\d{2})\-(?<number1>\d{2}).*(?<extension>\..*)
drive + "\\" + path + "\\" + prefix + number1 + number2 + extension
调用AdjustPath,如下所示:
group1 + "\\" + group2 + "\\" + group3 + group5 + group4 + group6
public static void Main(string[] args)
{
Console.WriteLine(AdjustPath("3123~101", @"3123|X:directory\Path\Directory|Pre0{0442-0500}.txt"));
Console.WriteLine(AdjustPath("3123~101", @"3123|X:directory\Path\Directory|0{0442-0500}.txt"));
}
输出:
X:\directory\Path\Directory\Pre00542.txt
X:\directory\Path\Directory\00542.txt
你可以称之为
WriteAdjustedPaths("3123~101", "output.txt");
如果你想要一份清单
可以使用Linq简化语法。请参阅
调用AdjustPath,如下所示:
group1 + "\\" + group2 + "\\" + group3 + group5 + group4 + group6
public static void Main(string[] args)
{
Console.WriteLine(AdjustPath("3123~101", @"3123|X:directory\Path\Directory|Pre0{0442-0500}.txt"));
Console.WriteLine(AdjustPath("3123~101", @"3123|X:directory\Path\Directory|0{0442-0500}.txt"));
}
输出:
X:\directory\Path\Directory\Pre00542.txt
X:\directory\Path\Directory\00542.txt
你可以称之为
WriteAdjustedPaths("3123~101", "output.txt");
如果你想要一份清单
可以使用Linq简化语法。请参见。对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;
}
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;
}
谢谢,我会试一试。但是我该如何使用它呢?我需要将它添加到私有的void列表框1_SelectedIndexChangedobject sender,EventArgs e部分。是的,我可以知道如何将它写入文本文件。我不知道,它一直在给我无法从方法组转换为字符串的信息。你如何将结果写入text文件谢谢,我会试一试。但我该如何使用它?我需要将它添加到private void listBox1_SelectedIndexChangedobject sender,EventArgs e部分下。是的,我知道如何将其写入文本文件。我不知道,它一直给我无法从方法组转换为字符串的信息。你如何也写出结果对于文本文件,我会使用命名组,但都一样:我会使用命名组,但都一样:这是一项bizar要求!这是一项bizar要求!有一天我的老板会允许升级,但我还在等待:-有一天我的老板会允许升级,但我还在等待:-