Regular Expressions for Java

Source: Internet
Author: User
Tags stringbuffer

Tag: represents AAAAA util code [] static parameter test regular

java Regular expression: a pattern that defines a string, which can be used to search, edit, or manipulate text;

A regular expression that is specified as a string must first be compiled into an instance of this class. The resulting pattern is then used to create the Matcher object, which, according to the regular expression, can match any character sequence, and all the States involved in performing the match reside in the match, so multiple matches can share the same pattern

Therefore, the typical invocation order is:

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

Matcher m = P.matcher ("AAAAAAABCCCCC");

Boolean B = m.matches ();

In the following example:

Boolean isMatch = pattern.matches (pattern,content), which makes it easy to use the matches method only if you use a regular expression once. 

The Java.util.regex package mainly consists of the following three classes:

1. Pattern class: The pattern object is a compiled representation of a regular expression. The pattern class has no public construction method, and to create a pattern object, you must first call its public static compilation method and return a pattern object that takes a regular expression as its first argument.

 PackageCuiyuee;Importjava.util.regex.*; Public classPatterntest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString content = "I am noob from runoob.com."; String pattern= ". *runoob.*"; BooleanIsMatch =pattern.matches (pattern,content); System.out.println ("Does the string contain the ' Runoob ' substring?" "+IsMatch); }}

2. Matcher class: The Matcher object is an engine that interprets and matches the input string, and, like the pattern class, Matcher does not have a public construction method and needs to invoke the Matcher method of the Pattern object to obtain a Matcher object.

Methods of the Matcher class:

Index method: Provides useful index values that exactly indicate where a match is found in the input string.

public int Start (): Returns the initial index of the previous match.

public int start (int group): Returns the initial index of a subsequence captured by a given group during a previous match operation.

public int end (int group): Returns the offset after the last character of a subsequence captured by a given group during a previous match operation.

Research method: Used to check the input string and return a Boolean value indicating whether the pattern was found.

public boolean Lookingat (): Attempts to match the input sequence starting at the beginning of the range with the pattern.

public Boolean find (): Attempts to find the next subsequence of the input sequence that matches the pattern.

public boolean find (int start): Resets this match to try to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.

public Boolean matches (): attempts to match the entire region to the pattern.

Replacement method: Replaces the text in the input string.

Public Matcher appednreplacement (stringbuffer sb,string Replacement): Implements non-terminal add and replace steps.

Public StringBuffer Appendtail (StringBuffer SB): Implement terminal Add and replace steps

public string ReplaceAll (string replacement): Replaces each word sequence of the input sequence that matches the given replacement string.

public string Replacefirst (string replacement): The first subsequence of the input sequence that replaces the pattern with the given replacement string

public static string Quotereplacement (string s): Returns the literal substitution string for the specified string. This method returns a string that works like a literal string passed to the Appendreplacement method of the Matcher class.

3, Patternsyntaxexception class

Patternsyntaxexception is a non-mandatory exception class that indicates a syntax error in a regular expression pattern.

Here are some ways to help us see what went wrong.

Public String getdescription (): Gets the description of the error.

public int getindex (): Gets the index of the error.

Public String Getpattern (): Gets the regular expression pattern of the error.

public string GetMessage (): Returns a multiline string that contains a syntax error and a description of its index, an incorrect regular expression pattern, and a visual indication of the error index in the pattern.

Regular Expressions for Java

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.