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