当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > .NET开发中正则表达式中BUG一例

ASP.NET
asp.net 动态生成表格
asp.net 程序优化精选
DataGridView自动调整行高和行宽
asp.net+js实现的ajax sugguest搜索提示效果
asp.net 将设有过期策略的项添加到缓存中
asp.net SqlDataAdapter对象使用札记
DataGrid 动态添加模板列 实现代码
asp.net 设置GridView的选中行
the sourcesafe database has been locked by the administrator之解决方法
asp.net 退出登陆(解决退出后点击浏览器后退问题仍然可回到页面问题)
Asp.Net HttpHandler 妙用
ASP.NET 保留文件夹详解
asp.net 中将表单提交到另一页 Code-Behind(代码和html在不同的页面)
SqlDataSource 链接Access 数据
asp.net GridView的删除对话框的两种方法
asp.net 按字节检查包含全半角的文字
asp.net String.IsNullOrEmpty 方法
asp.net System.Net.Mail 发送邮件
c# 读取文件内容存放到int数组 array.txt
asp.net Split分割字符串的方法

ASP.NET 中的 .NET开发中正则表达式中BUG一例


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 109 ::
收藏到网摘: n/a

又发现了一个 .net的bug!最近在使用正则表达式的时候发现:在忽略大小写的时候,匹配值从 0xff 到 0xffff 之间的所有字符,正则表达式竟然也能匹配两个 ASCII 字符:i(code: 0x69) 和 I(code: 0x49);但是仍然不能匹配其他的 ASCII 字母和数字。

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

以下为引用的内容:

1234567891011121314151617Regex 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 好啊,呵呵!

以下为引用的内容:

1234567891011121314151617var 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 + '!' );

  }

  }