为什么“[”与[a-zA-Z]相匹配

时间:2015-07-15 04:41:16

标签: c# regex character-class

Regex oRegex = new Regex(@"test[a-zA-z]");
string st = @"this is a test1 and testA and test[abc] another testB and test(xyz) again.";
foreach(Match match in oRegex.Matches(st))
{
     Console.WriteLine(match.Value);
}

输出

种皮

测试[

TESTB

问题:为什么输出中有test[?字符类[a-zA-Z]应该只匹配字母a到z和A到Z.

3 个答案:

答案 0 :(得分:3)

由于[属于ascii range A-z,因此将char类中的A-z更改为A-Z

Regex oRegex = new Regex(@"test[a-zA-Z]");

答案 1 :(得分:3)

在你的情况下,Z是拼写错误。更改此[a-zA-Z]

Regex oRegex = new Regex(@"test[a-zA-Z]");

答案 2 :(得分:2)

你的正则表达式中有拼写错误。 [a-zA-z]应为[a-zA-Z]

字符[位于Az字符之间。

相关问题