Regular expression matching correct also error?

Source: Internet
Author: User
Tags stringbuffer

Regular expressions, I think we are not unfamiliar. Java has, C # has, JavaScript has, other languages have, the approximate matching rules are very similar. Developers who have used regular expressions feel that this is a good thing. But I had a strange problem getting the numbers in a string at a time. The code is as follows:

Package test; Import Java.util.regex.Matcher; Import Java.util.regex.Pattern; public class Patterntest {public static void main (string[] args) {StringBuffer a = new StringBuffer ("100,200,300,400,50 0,600,700,800,900, "); Pattern p = pattern.compile ("//d*[^,]"); Matcher m = P.matcher (a); String num = "200"; while (M.find ()) {if (Num.equals (M.group ())) {A.delete (A.indexof (num), a.indexof (num) + num.length () + 1); System.out.println (a); } } } }

100,300,400,500,600,700,800,900,
Exception in thread ' main ' java.lang.StringIndexOutOfBoundsException:String index out of range:32
At Java.lang.StringBuffer.charAt (stringbuffer.java:162)
At Java.util.regex.pattern$bmpcharproperty.match (pattern.java:3366)
At Java.util.regex.pattern$curly.match0 (pattern.java:3760)
At Java.util.regex.pattern$curly.match (pattern.java:3744)
At Java.util.regex.pattern$start.match (pattern.java:3055)
At Java.util.regex.Matcher.search (matcher.java:1105)
At Java.util.regex.Matcher.find (matcher.java:535)
At Test. Patterntest.main (PATTERNTEST.JAVA:18)

The result is this. At first I feel so strange, why match out, return array subscript out of bounds. I tried to match the character sequence A with the ToString () method to a string in the Matcher, after which it could be matched and without an error. Strange, is it true that the sequence of matched characters cannot be changed. So I looked up the document and found that the parameters in Matcher were charsequence types, and then I found the document describing the interface, which says:

"Charsequence is a readable sequence of char values. This interface provides uniform, read-only access to many different kinds of char sequences. ”

So this sequence of characters really can't be changed. So I ran the following code:

Package test; Import Java.util.regex.Matcher; Import Java.util.regex.Pattern; public class Patterntest {public static void main (string[] args) {StringBuffer a = new StringBuffer ("100,200,300,400,50 0,600,700,800,900, "); Pattern p = pattern.compile ("//d*[^,]"); Matcher m = P.matcher (a); String num = "200"; while (M.find ()) {if (Num.equals (M.group ())) {a.append ("100,"); System.out.println (a); } } } }

The result output is: 100,200,300,400,500,600,700,800,900,100,

This I wonder, not to say can not change, how to add a no error. In fact, I do not know is not that I understand the mistake. The following code was then tested:

Package test; Import Java.util.regex.Matcher; Import Java.util.regex.Pattern; public class Patterntest {public static void main (string[] args) {StringBuffer a = new StringBuffer ("100,200,300,400,50 0,600,700,800,900, "); Pattern p = pattern.compile ("//d*[^,]"); Matcher m = P.matcher (a); String num = "200"; while (M.find ()) {if (Num.equals (M.group ())) {A.append ("M,"); A.delete (A.indexof (num), a.indexof (num) + num.length () + 1); System.out.println (a); } } } }

The result input is: 100,300,400,500,600,700,800,900,100, and there is no error, it seems really not change the character sequence, then why to a with the Delete method will be an error. I suspect that every regular match is going to check that the length of the character sequence that needs to be matched has not changed. Then I'll check it out and stick to the code.

Package test; Import Java.util.regex.Matcher; Import Java.util.regex.Pattern; public class Patterntest {public static void main (string[] args) {StringBuffer a = new StringBuffer ("100,200,300,400,50 0,600,700,800,900, "); Pattern p = pattern.compile ("//d*[^,]"); Matcher m = P.matcher (a); String num = "200"; int i = 0; while (M.find ()) {if (Num.equals (M.group ())) {A.delete (A.indexof (num), a.indexof (num) + num.length () + 1); A.append ("200 ,"); System.out.println (a); } i++; } System.out.println ("i=" + i); } }

The output is: Two lines 100,300,400,500,600,700,800,900,200, and i=9, which shows that the Matcher method each match will check the length of the matched sequence, if the length is less than the first match when the length of the sequence of characters, it will error. And I=9 also said that Matcher only 9 times, even if append two 200, will only leave one, because the first num and match the same value is, the 100 after the 200 deleted, and at the end of the character sequence added 200, wait until the nineth time, and then the last number, Delete 200, and then add a 200 at the end. At this point the Matcher no longer matches, so it does not cause a while infinite loop.

So we can draw the conclusion that when a regular expression matches a value, each match checks for a decrease in the length of the original sequence, an error if there is a decrease, and the matching character length is immutable, which is the length of the character sequence entered when the match is created.

The first time to send the article, the Code Editor is not very accustomed to, a bit embarrassing ... These are the conclusions of their own experiments, I hope that everyone on the sidelines, such as wrong, but also please point out that everyone progress together.

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.