我想构建正则表达式,它会返回特定的字符串
I want to build regex, which gives me back specific string
我有一个字符串数组。现在我必须构建一个正则表达式,它会给我一个字符串,它以一个字母开头,然后是 \ ,然后是 4 位数字。
我尝试了以下代码。
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string[] testStrings = new string[] { "w:\WES34", "w:\WES67", "w:\WES6432", "w:\WES\AdK1234qw", "w:\WES\abcdesf};
string RegEx = Regex.Escape("\b\d+\w*\b");
foreach(var testString in testStrings)
{
string f = Regex.Escape(testString );
Match match = Regex.Match(f, @RegEx);
// Here we check the Match instance.
if (!match.Success)
{
continue;
}
console.writeline("teststringcorrect", match.value);
}
我期待回复。
"w:\WES34" 和 "w:\WES67"
如何更改我的正则表达式模式以找到适合我的字符串?
编辑 Andrew Morton 建议调整后的代码:
string root = @"C:emp\WES"; // Outputdebug: C:emp\WES
Regex re= new Regex("^" + Regex.Escape(root) + @"\[0-9]{4}$"); // outputdebug: re.Pattern : ^C:emp\WES\[0-9]{4}$
string[] subfolder = Directory.GetDirectories(root); // ouputdebug: {string[4]}. [0]:C:emp\WES34 [1]:C:emp\WES78 [3]:C:emp\WES\wqder [4]:C:emp\WES435632
var dirs = Directory.EnumerateDirectories(root).Where(d => re.IsMatch(d)); // outputdebug: {system.link.Enumerable.WhereEnumarableIteraTOR<STRING>} Current is Null
foreach (var folder in subfolder)
{
string f = Regex.Escape(folder);
Match match = re.Match(f); // outputdebug: groups is 0
// Here we check the Match instance.
if (!match.Success)
{
continue;
}
}
您不能转义您正在检查的字符串 (testString
),因为这会改变它。
您可以使用 @
符号使以下字符串成为文字(例如 @"\r"
将被视为反斜杠和 r,而不是回车 return 字符) .即使这样,正则表达式中的文字反斜杠也需要转义为 \
.
要使用 Console.WriteLine() 输出多个字符串,您必须将它们与 +
连接起来。
static void Main(string[] args)
{
string[] testStrings = new string[] { @"w:\WES34", @"w:\WES67", @"w:\WES6432", @"w:\WES\AdK1234qw", @"w:\WES\abcdesf"};
Regex re =new Regex(@"^w:\WES\[0-9]{4}$");
foreach (var testString in testStrings)
{
Match match = re.Match(testString);
if (match.Success)
{
Console.WriteLine("teststringcorrect " + match.Value);
}
}
Console.ReadLine();
}
输出:
teststringcorrect w:\WES34
teststringcorrect w:\WES67
基于此,如果你有一个目录并且你想找到它的子目录,其名称恰好是四位数字,你可以这样做:
string root = @"C:\temp";
Regex re = new Regex("^" + Regex.Escape(root) + @"\[0-9]{4}$");
var dirs = Directory.EnumerateDirectories(root).Where(d => re.IsMatch(d));
Console.WriteLine(string.Join("\r\n", dirs));
可能会输出
C:\temp21
C:\temp76
我有一个字符串数组。现在我必须构建一个正则表达式,它会给我一个字符串,它以一个字母开头,然后是 \ ,然后是 4 位数字。
我尝试了以下代码。
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string[] testStrings = new string[] { "w:\WES34", "w:\WES67", "w:\WES6432", "w:\WES\AdK1234qw", "w:\WES\abcdesf};
string RegEx = Regex.Escape("\b\d+\w*\b");
foreach(var testString in testStrings)
{
string f = Regex.Escape(testString );
Match match = Regex.Match(f, @RegEx);
// Here we check the Match instance.
if (!match.Success)
{
continue;
}
console.writeline("teststringcorrect", match.value);
}
我期待回复。
"w:\WES34" 和 "w:\WES67"
如何更改我的正则表达式模式以找到适合我的字符串?
编辑 Andrew Morton 建议调整后的代码:
string root = @"C:emp\WES"; // Outputdebug: C:emp\WES
Regex re= new Regex("^" + Regex.Escape(root) + @"\[0-9]{4}$"); // outputdebug: re.Pattern : ^C:emp\WES\[0-9]{4}$
string[] subfolder = Directory.GetDirectories(root); // ouputdebug: {string[4]}. [0]:C:emp\WES34 [1]:C:emp\WES78 [3]:C:emp\WES\wqder [4]:C:emp\WES435632
var dirs = Directory.EnumerateDirectories(root).Where(d => re.IsMatch(d)); // outputdebug: {system.link.Enumerable.WhereEnumarableIteraTOR<STRING>} Current is Null
foreach (var folder in subfolder)
{
string f = Regex.Escape(folder);
Match match = re.Match(f); // outputdebug: groups is 0
// Here we check the Match instance.
if (!match.Success)
{
continue;
}
}
您不能转义您正在检查的字符串 (testString
),因为这会改变它。
您可以使用 @
符号使以下字符串成为文字(例如 @"\r"
将被视为反斜杠和 r,而不是回车 return 字符) .即使这样,正则表达式中的文字反斜杠也需要转义为 \
.
要使用 Console.WriteLine() 输出多个字符串,您必须将它们与 +
连接起来。
static void Main(string[] args)
{
string[] testStrings = new string[] { @"w:\WES34", @"w:\WES67", @"w:\WES6432", @"w:\WES\AdK1234qw", @"w:\WES\abcdesf"};
Regex re =new Regex(@"^w:\WES\[0-9]{4}$");
foreach (var testString in testStrings)
{
Match match = re.Match(testString);
if (match.Success)
{
Console.WriteLine("teststringcorrect " + match.Value);
}
}
Console.ReadLine();
}
输出:
teststringcorrect w:\WES34
teststringcorrect w:\WES67
基于此,如果你有一个目录并且你想找到它的子目录,其名称恰好是四位数字,你可以这样做:
string root = @"C:\temp";
Regex re = new Regex("^" + Regex.Escape(root) + @"\[0-9]{4}$");
var dirs = Directory.EnumerateDirectories(root).Where(d => re.IsMatch(d));
Console.WriteLine(string.Join("\r\n", dirs));
可能会输出
C:\temp21
C:\temp76