About the use of pattern and matcher in Java

Source: Internet
Author: User

Java.util.regex.Pattern
A compiled implementation of a regular expression.
Regular expressions usually appear as strings, which must first be compiled into an instance of the pattern class.
The result model can be used to generate a matcher, which (the generated Macher instance) can match the base
Any sequence of characters generated by this regular expression. When you implement a match in a match, you include the
Any number of cases, and multiple matches can share the same pattern.
The following is a typical invocation order:
Pattern p = pattern.compile ("A*b");
Matcher m = P.matcher ("Aaaaab");
Boolean B = m.matches ();
For ease of use, the pattern class also defines the matches () method,
Because sometimes a regular expression makes it only once.
In one call, this method first compiles the expression and then matches the input sequence.
The following sentence:
Boolean B = pattern.matches ("A*b", "Aaaaab");
Equivalent to the above three sentences. But since it does not allow subsequent patterns to be reused, duplicate matches are required
is obviously less efficient than the above method.
Instances of the pattern class cannot be changed and are thread-safe. Note that the Matcher class is not thread-safe


public static void Main (string[] args) {
String str = "Aaaaaaaaaaaaww

Wwwwwwwwwwwwwwwwwwwww

Wwwwwwwwwwwwwwwwwwwwww

Wwwwwwwwwwwwwwwwwwwwwww

Wwwwwwwwwwwwwwwwwwwwwww

Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ";
Pattern PA;
Matcher Ma;
PA = pattern
. Compile ("<meta.*?name.*?=.*?/". *? description.*?/". *content.*?=.*?/");
Ma = pa.matcher (str);
if (Ma.find ()) {
System.out.println ("-----");
System.out.println (Ma.group ());
} else {
System.out.println ("No matching string");
}

}

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.