Java-11.4 Regular Expressions (3)-Pattern and Matcher
In this section, we will discuss Pattern and Matcher.
Previously, we used regular expressions to match strings. In fact, java provides a powerful regular expression matching class. We will illustrate it with several examples below.
package com.ray.ch11;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test {public static void main(String[] args) {String testStr = abc;String[] regulars = { a, abc, //d,a? };System.out.println(input: + testStr);for (String item : regulars) {Pattern pattern = Pattern.compile(item);Matcher matcher = pattern.matcher(testStr);while (matcher.find()) {System.out.println(regular: + item + start:+ matcher.start() + end: + matcher.end());}}}}
Output:
Input: abc
Regular: a start: 0 end: 1
Regular: abc start: 0 end: 3
Regular:? Start: 0 end: 1
Regular:? Start: 1 end: 1
Regular:? Start: 2 end: 2
Regular:? Start: 3 end: 3
From the above output, we can see that we can not only get the matching result, but also get a lot of other information through matcher, for example, where the above position matches and where it ends.
Next let's change the input, and then change the rule to see another example:
package com.ray.ch11;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test {public static void main(String[] args) {String testStr = java now has regular expression;String[] regulars = { ^java, \Breg.*, n.w\s+h(a|i)s, s?,s+, s*, s{4}, s{1}, s{1,3} };System.out.println(input: + testStr);for (String item : regulars) {Pattern pattern = Pattern.compile(item);Matcher matcher = pattern.matcher(testStr);while (matcher.find()) {System.out.println(regular: + item + start:+ matcher.start() + end: + matcher.end());}}}}
Output:
Input: java now has regular expression
Regular: ^ java start: 0 end: 4
Regular: n. ws + h (a | I) s start: 5 end: 12
Regular: s? Start: 0 end: 0
Regular: s? Start: 1 end: 1
Regular: s? Start: 2 end: 2
Regular: s? Start: 3 end: 3
Regular: s? Start: 4 end: 4
Regular: s? Start: 5 end: 5
Regular: s? Start: 6 end: 6
Regular: s? Start: 7 end: 7
Regular: s? Start: 8 end: 8
Regular: s? Start: 9 end: 9
Regular: s? Start: 10 end: 10
Regular: s? Start: 11 end: 12
Regular: s? Start: 12 end: 12
Regular: s? Start: 13 end: 13
Regular: s? Start: 14 end: 14
Regular: s? Start: 15 end: 15
Regular: s? Start: 16 end: 16
Regular: s? Start: 17 end: 17
Regular: s? Start: 18 end: 18
Regular: s? Start: 19 end: 19
Regular: s? Start: 20 end: 20
Regular: s? Start: 21 end: 21
Regular: s? Start: 22 end: 22
Regular: s? Start: 23 end: 23
Regular: s? Start: 24 end: 24
Regular: s? Start: 25 end: 25
Regular: s? Start: 26 end: 27
Regular: s? Start: 27 end: 28
Regular: s? Start: 28 end: 28
Regular: s? Start: 29 end: 29
Regular: s? Start: 30 end: 30
Regular: s? Start: 31 end: 31
Regular: s + start: 11 end: 12
Regular: s + start: 26 end: 28
Regular: s * start: 0 end: 0
Regular: s * start: 1 end: 1
Regular: s * start: 2 end: 2
Regular: s * start: 3 end: 3
Regular: s * start: 4 end: 4
Regular: s * start: 5 end: 5
Regular: s * start: 6 end: 6
Regular: s * start: 7 end: 7
Regular: s * start: 8 end: 8
Regular: s * start: 9 end: 9
Regular: s * start: 10 end: 10
Regular: s * start: 11 end: 12
Regular: s * start: 12 end: 12
Regular: s * start: 13 end: 13
Regular: s * start: 14 end: 14
Regular: s * start: 15 end: 15
Regular: s * start: 16 end: 16
Regular: s * start: 17 end: 17
Regular: s * start: 18 end: 18
Regular: s * start: 19 end: 19
Regular: s * start: 20 end: 20
Regular: s * start: 21 end: 21
Regular: s * start: 22 end: 22
Regular: s * start: 23 end: 23
Regular: s * start: 24 end: 24
Regular: s * start: 25 end: 25
Regular: s * start: 26 end: 28
Regular: s * start: 28 end: 28
Regular: s * start: 29 end: 29
Regular: s * start: 30 end: 30
Regular: s * start: 31 end: 31
Regular: s {1} start: 11 end: 12
Regular: s {1} start: 26 end: 27
Regular: s {1} start: 27 end: 28
Regular: s {1, 3} start: 11 end: 12
Regular: s {1, 3} start: 26 end: 28
Explanation:
(1) The above find () function is like an iterator, which contains every character in the iteration string. In addition, parameters can be placed in the find (int) to determine where to start iteration.
(2) The start () and end () above return the matched start and end positions.
In addition to the above features, we can also add parameters in pattern to limit matching.
package com.ray.ch11;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test {public static void main(String[] args) {String testStr = java now has regular expression+ JAVA now has regular expression;String[] regulars = { ^java };System.out.println(input: + testStr);for (String item : regulars) {Pattern pattern = Pattern.compile(item, Pattern.CASE_INSENSITIVE| Pattern.MULTILINE);Matcher matcher = pattern.matcher(testStr);while (matcher.find()) {System.out.println(regular: + item + start:+ matcher.start() + end: + matcher.end() + group:+ matcher.group());}}}}
Output:
Input: java now has regular expression
JAVA now has regular expression
Regular: ^ java start: 0 end: 4 group: java
Regular: ^ java start: 32 end: 36 group: JAVA
Summary: This chapter describes Pattern and Matcher.