zl程序教程

您现在的位置是:首页 >  其他

当前栏目

asp.NET开发中正则表达式中BUG分析

2023-06-13 09:14:02 时间

比如以下的代码就是用来测试用正则表达式匹配从0xff到0xffff的字符。而值范围在0到0xfe的所有字符是不能被匹配的。 
以下为引用的内容:

复制代码代码如下:

Regexregex=newRegex(@"[/u00FF-/uFFFF]+");
  //Thecharacters,whoesvaluearesmallerthan0xff,
  //arenotexpectedtobematched.
  for(inti=0;i<0xff;i++){
  strings=newstring(newchar[]{(char)i});
  Debug.Assert(!regex.IsMatch(s),string.Format(
  "Thecharacterwasnotexpectedtobematched:0x{0:X}!",i));
  }
  //However,thecharacterswhoesvalue
  //aregreaterthan0xfeareexpectedtobematched.
  for(inti=0xff;i<=0xffff;i++){
  strings=newstring(newchar[]{(char)i});
  Debug.Assert(regex.IsMatch(s),string.Format(
  "Thecharacterwasexpectedtobematched:0x{0:X}!",i));
  }

这时的运行结果是正常的,没有任何的断言错误出现。
然而当使用忽略大小写的匹配模式时,结果就不一样了。将上面代码中的第一行改成:
1Regexregex=newRegex(@"[/u00FF-/uFFFF]+",RegexOptions.IgnoreCase);
程序运行的时候就会有两处断言错误。它们分别是字符值为73和105,也就是小写字母i和大写字母I。这个bug非常奇怪,别的字符都很正常!而且用javascript脚本在IE(版本是6.0)里面运行也同样有这么bug存在(比如下面这段代码)。然而在Firefox中运行就是没有问题的。还是Firefox好啊,呵呵!
以下为引用的内容:
复制代码代码如下:

varre=/[/u00FF-/uFFFF]+/;
  //varre=/[/u00FF-/uFFFF]+/i;
  for(vari=0;i<0xff;i++){
  vars=String.fromCharCode(i);
  if(re.test(s)){
  alert("Shouldnotbematched:"+i+"!");
  }
  }
  for(vari=0xff;i<=0xffff;i++){
  vars=String.fromCharCode(i);
  if(!re.test(s)){
  alert("Shouldbematched:"+i+"!");
  }
  }