-3
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);
}

输出

测试A

测试[

测试B

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

4

3 回答 3

3

因为[ascii 范围内 A-z,所以A-z将 char 类中的 present 更改为A-Z

Regex oRegex = new Regex(@"test[a-zA-Z]");
于 2015-07-15T04:44:15.330 回答
3

Z 在你的情况下是错字。改变这个[a-zA-Z]

Regex oRegex = new Regex(@"test[a-zA-Z]");
于 2015-07-15T04:44:28.880 回答
2

您的正则表达式中有错字。[a-zA-z]应该是[a-zA-Z]

字符[介于Az字符之间。

于 2015-07-15T04:44:25.183 回答