Bug in. NET development regular expression,. net regular expression

Source: Internet
Author: User

Bug in. NET development regular expression,. net regular expression

Another. net bug was found! Recently, when using a regular expression, we found that when case sensitivity is ignored, the regular expression can match all the characters from 0xff to 0 xffff. The regular expression can also match two ASCII characters: I (code: 0x69) and I (code: 0x49); but still cannot match other ASCII letters and numbers.

For example, the following code is used to test the use of regular expressions to match characters from 0xff to 0 xffff. All characters whose values range from 0 to 0xfe cannot be matched.

1234567891011121314151617 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 <= 0 xffff; 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 ));
}

At this time, the running result is normal and no asserted error occurs.

However, when the case-insensitive match mode is used, the results are different. Change the first line in the above Code:

1 Regex regex = new Regex (@ "[\ u00FF-\ uFFFF] +", RegexOptions. IgnoreCase );

When the program runs, there will be two asserted errors. They are character values 73 and 105, that is, the lower case letter I and the upper case letter I. This bug is very strange, and other characters are normal! In addition, using javascript scripts to run in IE (version 6.0) also has such a bug (such as the following code ). However, there is no problem in running Firefox. Good for Firefox, huh, huh!

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 <= 0 xffff; I ++ ){
Var s = String. fromCharCode (I );
If (! Re. test (s )){
Alert (''should be matched: ''+ I + ''! '');
}
}

Reprinted from: http://www.aspnetjia.com/Cont-171.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.