matches() of the String class uses
The matches() method is a global match. If regular can match the whole string, it returns true; otherwise, it returns false
String value4 = "11111111112"; if (value4.matches("\\d+")) { System.out.println("matches Match successful"); }else{ System.out.println("matches Matching failed"); } String value5 = "11111111112QWE"; if (value5.matches("\\d+")) { System.out.println("matches Match successful"); }else{ System.out.println("matches Matching failed"); }
Operation results:
matches match succeeded
matches failed
matches() of the Matcher class uses
The matches() method is also a global matching method. Its usage is similar to that of String's matches(). The specific differences are found in terms of performance through online query. The matches() method of the Matcher class is faster. (must match globally)
String value2 = "123456789"; Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(value2); if (m.matches()) { System.out.println("matches Matched content:" + m.group()); System.out.println("matches Match successful"); } else { System.out.println("matches Matching failed"); }
Operation results:
matches: 123456789
matches match succeeded
find() of the Matcher class uses
The find() method is a local match. If the match is successful, it returns true. If the find method is called for the second time, the matching position will continue to match downward. (global matching not available)
String value = "123AAA456789BBBC"; Pattern pattern = Pattern.compile("[1-9]\\d{2}"); Matcher matcher = pattern.matcher(value); if (matcher.find()) { System.out.println("find Matched content:" + matcher.group()); System.out.println("find Start position:" + matcher.start() + " →→→→→→→→ End position:" + matcher.end()); } if (matcher.find()) { System.out.println("find Matched content:" + matcher.group()); System.out.println("find Start position:" + matcher.start() + " →→→→→→→→ End position:" + matcher.end()); } if (matcher.find()) { System.out.println("find Matched content:" + matcher.group()); System.out.println("find Start position:" + matcher.start() + " →→→→→→→→ End position:" + matcher.end()); } if (matcher.find()) { System.out.println("find Matched content:" + matcher.group()); System.out.println("find Start position:" + matcher.start() + " →→→→→→→→ End position:" + matcher.end()); }else { System.out.println("find Matching failed"); }
Operation results:
find matching content: 123
find start position: 0 →→→→→→→→ end position: 3
find matching content: 456
find start position: 6 →→→→→→→→ end position: 9
find matching content: 789
find start position: 9 →→→→→→→→ end position: 12
find match failed
Method summary:
It can be seen from the running results that the initial position of the match will be moved backward every time the find() method is called, that is, the matched content will not be matched for the second time
String value2 = "123456789"; Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(value2); if(m.find()){ System.out.println("find Matched content:" + m.group()); System.out.println("find Match successful"); }else { System.out.println("find Matching failed"); }
Operation results:
find match failed
Method summary:
The find() method returns false even if the regular writing is correct.
lookingAt() of the Matcher class uses
The lookingAt() method starts the matching from the initial position of the string. No matter how many times it is matched and whether it is successful or not, it starts the matching from the initial position (global matching is possible)
String value3 = "2222AAAA33333"; Pattern pattern1 = Pattern.compile("[1-9]\\d{3}"); Matcher matcher1 = pattern1.matcher(value3); Pattern pattern2 = Pattern.compile("[1-9]\\d{4}"); Matcher matcher2 = pattern2.matcher(value3); if (matcher1.lookingAt()) { System.out.println("lookingAt Matched content:" + matcher1.group()); System.out.println("lookingAt Match successful"); }else { System.out.println("lookingAt Matching failed"); } if (matcher1.lookingAt()) { System.out.println("lookingAt Matched content:" + matcher1.group()); System.out.println("lookingAt Match successful"); }else { System.out.println("lookingAt Matching failed"); } if (matcher2.lookingAt()) { System.out.println("lookingAt Matched content:" + matcher2.group()); System.out.println("lookingAt Match successful"); }else { System.out.println("lookingAt Matching failed"); }
Operation results:
What lookingAt matches: 2222
lookingAt match succeeded
What lookingAt matches: 2222
lookingAt match succeeded
lookingAt match failed
Method summary:
The lookingAt() method matches only the initial position of the string no matter how many times it is matched.
If the initial position cannot be matched, false is returned regardless of whether it is matched later
String value2 = "123456789"; Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(value2); ` if (m.lookingAt()) { System.out.println("lookingAt Matched content:" + m.group()); System.out.println("lookingAt Match successful"); }else { System.out.println("lookingAt Matching failed"); }
Operation results:
What lookingAt matches: 123456789
lookingAt match succeeded
Method summary:
The lookingAt() method can match globally.
reset() of Matcher class
The reset() method resets the matcher and returns a current matcher.
String value = "123AAA456789BBBC"; Pattern pattern = Pattern.compile("[1-9]\\d{2}"); Matcher matcher = pattern.matcher(value); if (matcher.find()) { System.out.println("find Matched content:" + matcher.group()); System.out.println("find Start position:" + matcher.start() + " →→→→→→→→ End position:" + matcher.end()); } if (matcher.find()) { System.out.println("find Matched content:" + matcher.group()); System.out.println("find Start position:" + matcher.start() + " →→→→→→→→ End position:" + matcher.end()); } if (matcher.find()) { System.out.println("find Matched content:" + matcher.group()); System.out.println("find Start position:" + matcher.start() + " →→→→→→→→ End position:" + matcher.end()); } if (matcher.find()) { System.out.println("find Matched content:" + matcher.group()); System.out.println("find Start position:" + matcher.start() + " →→→→→→→→ End position:" + matcher.end()); }else { System.out.println("find Matching failed"); } if (matcher.lookingAt()) { System.out.println("lookingAt Matched content:" + matcher.group()); System.out.println("lookingAt Start position:" + matcher.start() + " →→→→→→→→ End position:" + matcher.end()); } matcher.reset();//Reset this matcher System.out.println("The matcher is reset"); if (matcher.find()) { System.out.println("find Matched content:" + matcher.group()); System.out.println("find Start position:" + matcher.start() + " →→→→→→→→ End position:" + matcher.end()); } if (matcher.lookingAt()) { System.out.println("lookingAt Matched content:" + matcher.group()); System.out.println("lookingAt Start position:" + matcher.start() + " →→→→→→→→ End position:" + matcher.end()); }
Operation results:
find matching content: 123
find start position: 0 →→→→→→→→ end position: 3
find matching content: 456
find start position: 6 →→→→→→→→ end position: 9
find matching content: 789
find start position: 9 →→→→→→→→ end position: 12
find match failed
lookingAt matches: 123
lookingAt start position: 0 →→→→→→→→ end position: 3
The matcher is reset
find matching content: 123
find start position: 0 →→→→→→→→ end position: 3
lookingAt matches: 123
lookingAt start position: 0 →→→→→→→→ end position: 3
matches: 123456789
Method summary:
From the above code running results, we can see that after reset(), the find() method starts to match from the original position, while the lookingAt() method always matches from the original position, so the results are not affected. Similarly, the matches() method is a global match, so it should not be affected.
Postscript
New year's new weather, summarize the technical documents for the first time. Don't spray Daniel. If there are mistakes, you are welcome to criticize and update them at any time. Newcomer Xiaobai will grow together.