当前位置: 移动技术网 > IT编程>开发语言>.net > asp.NET开发中正则表达式中BUG分析

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

2018年04月20日  | 移动技术网IT编程  | 我要评论

xsxlt,清洁能源有哪些,武汉上门按摩

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

复制代码 代码如下:

regex regex = new regex(@"[/u00ff-/uffff]+");
  // the characters, whoes value are smaller than 0xff,
  // are not expected to be matched.
  for (int i = 0; i <0xff; i++) {
  string s = new string(new char[] { (char)i });
  debug.assert(!regex.ismatch(s), string.format(
  "the character was not expected to be matched: 0x{0:x}!", i));
  }
  // however, the characters whoes value
  // are greater than 0xfe are expected to be matched.
  for (int i = 0xff; i <= 0xffff; i++) {
  string s = new string(new char[] { (char)i });
  debug.assert(regex.ismatch(s), string.format(
  "the character was expected to be matched: 0x{0:x}!", i));
  }

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

var re = /[/u00ff-/uffff]+/;
  // var re = /[/u00ff-/uffff]+/i;
  for(var i=0; i<0xff; i++) {
  var s = string.fromcharcode( i );
  if ( re.test(s) ) {
  alert( 'should not be matched: ' + i + '!' );
  }
  }
  for(var i=0xff; i<=0xffff; i++) {
  var s = string.fromcharcode( i );
  if ( !re.test(s) ) {
  alert( 'should be matched: ' + i + '!' );
  }
  }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网