Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class Test {
public static void Main (string[] args) {
Pattern p = pattern. Compile ("\\d{3,5}");
Matcher m = P.matcher ("123-4567812-910-11");
/*print (M.matches ());
M.reset (); */
Print (M.find ());//true
Print (M.group ());//123
Print (M.start () + "-" +m.end ());//0-3:1~-
System.out.printf ("newline--------------------\ n");
Print (M.find ());//true
Print (M.group ());//45678
Print (M.start () + "-" +m.end ());//4-9:4~1
System.out.printf ("newline--------------------\ n");
Print (M.find ());//true
Print (M.group ());//910
Print (M.start () + "-" +m.end ());//12-15:9~-
System.out.printf ("newline--------------------\ n");
Print (M.find ());//false
System.out.printf ("newline--------------------\ n");
System.out.println ();
Print (M.matches ());//false
System.out.printf ("line change \ n");
M.reset ();
Print (M.find ());//true
Print (M.group ());//45678
Print (M.start () + "-" +m.end ());//4-9
System.out.printf ("line change \ n");
Print (M.find ());//true
Print (M.group ());//910
Print (M.start () + "-" +m.end ());//12-15
System.out.printf ("line change \ n");
Print (M.find ());//false
System.out.printf ("line change \ n");
Print (M.find ());//false
System.out.printf ("line change \ n");
System.out.println ();
Print (M.matches ());//false
System.out.printf ("line change \ n");
M.reset ();
Print (M.find ());//true
Print (M.group ());//123
Print (M.start () + "-" +m.end ());//0-3
System.out.printf ("line change \ n");
Print (M.find ());//true
Print (M.group ());//45678
Print (M.start () + "-" +m.end ());//4-9
System.out.printf ("line change \ n");
Print (M.find ());//true
Print (M.group ());//910
Print (M.start () + "-" +m.end ());//12-15
System.out.printf ("line change \ n");
Print (M.find ());//false
System.out.printf ("line change \ n");
System.out.println ();
Print (M.lookingat ());//true
Print (M.lookingat ());//true
}
public static void print (Object o) {
System.out.println (o);
}
}
/*
* Explanation of this program: the most difficult part of this program is the understanding of the Find () method
* In the first paragraph of the program, find will be a character of a character match, when detected 3, found that there are already 3 characters, then this find can return true, but not finished, it will continue to detect, found that 3 is behind-instead of the number, the end of the first find, So in group this output 123, from 0 to 3 (note that the subscript is starting from 0)
* The second find starts with the character Subscript 3, which is where the last find ended, so this find is found--will it return false, the answer is no, it will continue to look back, to see if it can be found in the string in the following string fragment, fortunately, It was found, that's 45678, because the specified string
* The upper limit of the fragment length is 5, so in the group output 45678 instead of 4567812, then why is the subscript the beginning of 4-9 instead of 4-8? Although the specified upper limit for a string fragment is 5, it does not continue to check for a bit behind.
* Third find from subscript 9 where to start looking for, 1,2,-, found no, continue to find! 9,1,0,-found that the condition is satisfied, return true, and the group output 910, subscript and why start from 12? Because find finds a string fragment that satisfies a condition from subscript 12.
* Fourth find very poor,-,1,1 are not satisfied, and there is no string, so I have to return false,
* Why does the output become different after adding matches and reset? Starting with matches, matches requires all matches to return true, but unfortunately the length of the string is more than 5 characters long, so returning false is no doubt. Specifically, matches is also a character checked by a character, when he checks to the first "-" when found
* Not a number, so returns false (but even if all the numbers return false, as mentioned above), the key is that this matches has an effect on the back of find, because find is checked from the matches stop bit, Just like the next find is checked from the previous find stop bit, so the output is not the same as the previous one.
* But when reset is added, the match is reset, which is the equivalent of find to start checking from the first character of the string.
* Lookingat () is every time you check from the beginning is not meeting the requirements, no matter how many Lookingat (), and 123 is in line with the requirements, so it will output true
* Note that only the match succeeds, you can get more information through the start, end, and group methods.
*/
Use of the Find () method in a Java regular expression