On the regular expression

Source: Internet
Author: User

Regular expressions are a powerful string manipulation tool that can be used to look for strings to extract a split substitution. Several special methods are also available in the String class:

Boolean matches (string regex) determines whether a string conforms to a specified regular expression

String ReplaceAll (string regex,string replacement) replaces all matches of the previous regex with the following string, replacing the former with the latter.

These special methods all rely on regular expressions in Java, and Java also provides support for the pattern and Matcher support for two classes dedicated to providing the expression of a constant.

Regular expressions are a very simple and useful tool. A regular expression is a template that is used to match strings. In fact, any string can be used as a regular expression, such as the string "ABC" is a regular expression such as a regular expression is only match ABC in regular expressions can be used wildcard characters, the wildcard character in regular expressions far beyond the function of wildcards is called a predefined character

The "" "square bracket expression means that the enumeration" ABC "represents any one of the matching ABC

"-" indicates that a range such as "A-Z" means that if you want to use any character between A-Z then use this

"^" means no, "^abc" means not making ABC choice

The expression "&&" a-z&& "def" means to get the intersection of A-Z and def If you want to use match all Chinese characters, you can use "\\u0041-\\u0056" All Chinese Unicode is sequential so that you can match all Chinese characters in such a way regular expressions also support the expression of an original bracket, which is used to make a multi-and-expression a subexpression that can be used in parentheses or an operator

If you want to set up a match 000-000-0000 use regular expressions \\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d regular expressions that look tedious actually the regular expression also provides a quantifier

Greedy (greedy mode): The quantifier defaults to greedy mode unless another expression that represents greedy mode will match until the match is not matched

Reluctant (minimum matching mode) such a pattern with a question mark (? ) suffix means that only the fewest characters will be matched

Possessive (possessive mode) is usually less used

Use regular Expressions:

Once you have defined regular expressions in your program, you can use the pattern and matcher to work with regular expressions.

The pattern is a representation of a regular expression compiled in memory, so the regular expression string must first be compiled into a pattern pair, and then the state that is involved in creating a match for the martcher is persisted in the Matcher object, using pattern. Multiple Matcher objects can share an object

The method is created as follows:

Compiles a string into a pattern pair like

Pattern p = pattern.compile ("a*b");

Create Matcher pairs of images using pattern pairs like

Matcher m = p.matcher ("aaaaaaab");

Boolean B = a.matches ();//Returns True

The pattern pairs defined above can be used multiple times, and if a regular expression needs to be used only once, you can directly use the static method of the Pattern class matches () to compile a string into an anonymous pattern pair and execute the following code:

Boolean B = pattern.matches ("a*b","aaab" );

The above sentence is a combination of the top three sentences.

Pattern is an immutable class that can provide multiple and normal routines for safe use

The Matcher class provides several common methods:

Find (): Returns whether the target string contains a substring that matches the pattern

Group (): Returns the last string matched to pattern

Start (): Returns the position of the last substring that matches the pattern at the beginning of the target string.

End (): the opposite of the above returns the last

Matcher (): Return to integration target ancient wear and pattern match

Reset (): Use an existing matcher pair like a new string sequence

The Matcher's find () and group () methods can be used to remove the substring from the target string (the substring that matches the regular expression is like an Internet crawler)

public class findgroup{

public static void Main (String [] aegs) {

String str = " I have a book called Java, contact me as soon as possible my phone 13500006666 " + " Make friends phone 15322223636 " ;

Matcher m = pattern.compile ("((13\\d|15\\d)) \\d{8}");

while (M.find ()) {

System.out.println (M.group ());

}

}

}

The result of the above output is the phone number

From the results above, one can see that the find () method finds the corresponding substring at the same time as the pattern-matching string in the string, and the next call to the Find () method is then looked down

For a more in-depth understanding, please refer to the website specifically described

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

On the regular expression

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.