Java Regular Expressions

Source: Internet
Author: User

Regular expressions
A regular expression is a powerful string processing tool that allows you to find, extract, split, replace, and manipulate strings.
Here are a few special methods
Boolean matches (string regex): Determines whether the string matches the specified regular expression
String ReplaceAll (String regex,string replacement): Replaces all matching regex strings in the string with replacement
String Replacefirst (String regex,string replacement): Replaces the string in the first match of the Regex with replacement
In addition to this, Java also provides pattern and matcher two classes dedicated to providing a match for regular expressions
Using regular expressions
Compiles a string into a pattern object
Pattern P=pattern.complie ("a*b");
To create a Matcher object using the Pattern object
Matcher m=p.matcher ("Aaaaab");
Boolean b=m.matches ();//true
Principle:
A pattern object is a representation of a regular expression after it is compiled in memory, so the regular expression string must first be compiled into a pattern object
The corresponding Matcher object is then created using the Pattern object, and the state involved in performing the match is saved in the Matcher object, and multiple Matcher objects can
Share a Pattern object
Pattern is an immutable class that can be used safely by multiple concurrent threads
The Matcher class provides several common methods:
Find (): Returns whether the string that matches the pattern is included in the target
Group (): Returns the last substring matching the pattern
Start (): Returns the start position in the last substring matching the pattern
End (): Returns the end position in the last string matched by pattern +1
Lookingat (): Returns whether the front part of the target string matches the pattern
Matches () returns whether the entire target string matches the pattern
Reset (): Apply an existing Matcher object with a new character sequence
public class findgroup{
public static void Main (string[] args) {
Using strings to simulate Web page source code
String str= "xxxx4832473248xxxxxxxxxx3q50q5q34q32078rwa34543543";
Create a Pattern object and use it to build a Matcher object
Matcher M=pattern.compile ("(() \\d| ( \d) "). Matches (str);
while (M.find ()) {
System.out.println (M.GROUNP ());
}
}
}

Java Regular Expressions

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.