Regular expressions for Java interview preparation

Source: Internet
Author: User

In the common Java programming, regular expressions are often combined with the string processing, Io flow, in the "Java programming thought" even the regular expression in the description of the IO stream in the content of the chapter, it can be seen that the regular expression is closely related to the flow. Java designers also use the package that the regular expression requires: Java.util.regex is placed in a common tool.

In Java,

the quantifier of the regular expression is divided into three levels:

1) the greedy

To make as many matches as possible for all possible patterns, we typically use this pattern;

2) barely.

As long as the match to no longer proceed, this pattern in the string of multiple self-contained methods have a good embodiment;
3) accounted for

This pattern is only available in Java, and is used to place regular expressions out of control, which we don't normally use.

Implementation method:

We use the pattern and Matcher classes in Java.util.regex to achieve generic regular expression matching.

A pattern object represents a compiled version of a regular expression. The static Complie () method compiles a regular expression into a pattern object, and through a pattern instance of the Matcher () we can get a Matcher object instance that can present the result we want;

Cases:

Pattern p = pattern.compile ("a*b"= P.matcher ("Aaaaab"); Boolean B = m.matches ();

This is the most typical example of a regular expression, "Aaaaab" is the input, "a*b" is the matching pattern, if the input can be matched by a pattern, the value of B is true, and vice versa is false.

Pattern has two very useful methods, one is static public static Boolean matches (string regex, charsequence input), which is used to view the string in input that has not been matched by a regex;

Another is the split () method, which produces an array that is broken by the regex, much like the method of string. What is Charsequence

There are many useful methods in matcher, such as the Find () method, you can find multiple pattern matches and return a Boolean value, so it is generally used in the while loop.

Example: How to use regular expressions to extract URLs from a Web page? This is used more in web crawlers.

  

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.