Understanding java-11.4 Regular Expressions from the beginning (3)-pattern and Matcher

Source: Internet
Author: User

In this chapter we will discuss the pattern and matcher.

Before we were all simply using regular expressions to match strings, in fact Java provides a strong regular matching class, we will follow a few examples to illustrate.

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:" + MATC Her.start () + "End:" + matcher.end ());}}}

Output:

Input:abc
Regular:a start:0 end:1
REGULAR:ABC start:0 End:3
Regular:a? start:0 end:1
Regular:a? Start:1 end:1
Regular:a? Start:2 End:2
Regular:a? Start:3 End:3


As you can see from the above output, we can not only get the result of the match, but also through Matcher, we can get a lot of other information, such as where the above match in that position, in which place to end.


Let's change the input below and then change the rules 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:" + MATC Her.start () + "End:" + matcher.end ());}}}

Output:

Input:java now have regular expression
Regular:^java start:0 End:4
Regular:n.w\s+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


Explain:

(1) The Find () function above is like an iterator that iterates through every character in a string. and find (int) can also place parameters, to decide to start the iteration from the first few positions.

(2) The above start () and end () returns the position of the beginning and end of the match


In addition to the above features, we can also add parameters to the pattern to qualify the match.

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 have regular expression\n" + "Java now have 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:" + MATC Her.start () + "End:" + matcher.end () + "group:" + Matcher.group ());}}}


Output:

Input:java now have regular expression
JAVA now have regular expression
Regular:^java start:0 End:4 Group:java
Regular:^java start:32 end:36 Group:java


Summary: This chapter mainly describes the pattern and matcher.


This chapter is here, thank you.

-----------------------------------

Directory



Understanding java-11.4 Regular Expressions from the beginning (3)-pattern and Matcher

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.