aspnet项目开发教程 .NET开发中正则表达式中BUG一例
.NET开发中正则表达式中BUG一例
又发现了一个的bug!最近在使用正则表达式的时候发现 在忽略大小写的时候 匹配值从 xff 到 xffff 之间的所有字符 正则表达式竟然也能匹配两个 ASCII 字符 i(code: x ) 和 I(code: x );但是仍然不能匹配其他的 ASCII 字母和数字
比如以下的代码就是用来测试用正则表达式匹配从 xff 到 xffff 的字符 而值范围在 到 xfe 的所有字符是不能被匹配的
Regex regex = new Regex(@ [/u FF /uFFFF]+ );
// The characters whoes value are smaller than xff
// are not expected to be matched
for (int i = ; i < xff; i++) {
string s = new string(new char[] { (char)i });
Debug Assert(!regex IsMatch(s) string Format(
The character was not expected to be matched: x{ :X}! i));
}

// However the characters whoes value
// are greater than xfe are expected to be matched
for (int i = xff; i <= xffff; i++) {
string s = new string(new char[] { (char)i });
Debug Assert(regex IsMatch(s) string Format(
The character was expected to be matched: x{ :X}! i));
}
这时的运行结果是正常的 没有任何的断言错误出现
然而当使用忽略大小写的匹配模式时 结果就不一样了 将上面代码中的第一行改成
Regex regex = new Regex(@ [/u FF /uFFFF]+ RegexOptions IgnoreCase);
程序运行的时候就会有两处断言错误 它们分别是字符值为 和 也就是小写字母 i 和大写字母 I 这个 bug 非常奇怪 别的字符都很正常!而且用 javascript脚本在 IE (版本是 )里面运行也同样有这么 bug 存在(比如下面这段代码) 然而在 Firefox中运行就是没有问题的 还是 Firefox 好啊 呵呵!
var re = /[/u FF /uFFFF]+/;
// var re = /[/u FF /uFFFF]+/i;
for(var i= ; i< xff; i++) {
var s = String fromCharCode( i );
if ( re test(s) ) {
alert( Should not be matched: + i + ! );
}
}
for(var i= xff; i<= xffff; i++) {
var s = String fromCharCode( i );
if ( !re test(s) ) {
alert( Should be matched: + i + ! );
}
lishixinzhi/Article/program/net/201311/14000