. NET Regular expression bugs

Source: Internet
Author: User
Tags format assert expression net net bug regular expression string
and a. NET bug! recently found in the use of regular expressions: When ignoring the case, match the values from 0xFF to 0xFFFF of all the characters, 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 characters that match a regular expression from 0xFF to 0xFFFF. All characters with a value range of 0 to 0xFE cannot be matched.

1234567891011121314151617Regex regex = new Regex (@ "[\u00ff-\uffff]+");
The characters, whoes value are smaller than 0xFF, are not expected to is 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 is not expected to is 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 is expected to be matched:0x{0:x}!", i));
}
The results of this operation are normal and no assertion error occurs.

However, when you use a matching pattern that ignores case, the results are different. Change the first line in the above code to:

1Regex regex = new Regex (@ "[\u00ff-\uffff]+", regexoptions.ignorecase);
When the program runs, there are two assertion errors. They are character values 73 and 105, which are lowercase i and capital letter I. This bug is very strange, the other characters are very normal! and using JavaScript scripts to run in IE (version 6.0) also has such bugs (for example, the following code). However, running in Firefox is no problem. Or Firefox good ah, hehe!

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 to be matched: ' + i + '! ');
}
}
for (var i=0xff; i<=0xffff; i++) {
var s = string.fromcharcode (i);
if (!re.test (s)) {
Alert (' Should be matched: ' + i + '! ');
}
}



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.