Why “[” gets matched by [a-zA-Z]
1 2 3 4 5 6
| 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);
} |
输出:P></
头P></
[测试P></
》教学大纲P></
问题:为什么在test[输出?the character ZA Z级[阿尔法] is only to Match做特点Z和Z一通。P></
Z是你的错别字,把这个换成1〔4〕。
1
| Regex oRegex = new Regex (@"test[a-zA-Z]"); |
因为[属于ASCII范围A-z,所以将char类中存在的A-z改为A-z。
1
| Regex oRegex = new Regex (@"test[a-zA-Z]"); |
你的正则表达式有错别字。[a-zA-z]应为[a-zA-z]。
字符[介于A和z字符之间。
- 谢谢你们指出了打字错误。在某种程度上,打字错误帮助我理解了A-Z的范围。这篇文章帮助我进一步解释了为什么"["包含在A-Z范围内。
- @Blorgbeard我是第一个发布这个答案的人,为什么在单击最旧的选项卡时,您的帖子会首先显示出来?
- @Avinashraj不确定。我的现在被接受了,所以它总是先显示。你可以在"2小时前"上徘徊查看时间戳-看起来你比我快了10秒:)