Java: Regular Expression matching

Source: Internet
Author: User

1. Regular Expression matching in Java

 

Example:

 

Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;

Public class Wanwan {
 
Public static void main (string [] ARGs ){

// The standard form:

// @ Static pattern compile (string pattern ),
// Compiles a regular expression, creating a new pattern instance in the process.
Pattern P = pattern. Compile ("Hello, a [A-Z] *! ");
// @ Matcher (charsequence input)
// Returns a matcher for the pattern and a given input.
Matcher M = P. matcher ("Hello, android! ");
// @ Boolean matches ()
// Tries to match the pattern against the entire region (or the entire input, if no region has been set ).
Boolean b1 = M. Matches ();
System. Out. println (B1 );

// The more compact form:

// @ Static Boolean matches (string RegEx, charsequence input)
// Tries to match a given regular expression against a given input.
Boolean b2 = pattern. Matches ("Hello, a [A-Z] *! "," Hello, android! ");
System. Out. println (B2 );


}
}

 

Additional content:

 

Package

Java. util. regexregular expressions

A regular expression consists of literal text, meta characters, character sets, and operators. The latter three have a special meaning when encountered during the processing of a pattern.

  • Meta characters are a special means to describe single characters in the input text. A common example for a meta character is the dot '. ', which, when used in a regular expression, matches any character.
  • Character sets are a convenient means to describe different characters that match a single character in the input. character sets are enclosed in angular brackets '[' and ']' and use the dash '-' for forming ranges. A typical example is "[0-9a-fa-f]", which describes the set of all hexadecimal digits.
  • Operators modify or combine whole regular expressions, with the result being a regular expression again. an example for an operator is the asterisk '*', which, together with the regular expression preceding it, matches zero or more repetitions of that regular expression. the plus sign '+' is similar, but requires at least one occurrence.

Meta characters, the '[' and ']' that form a character set, and operators normally lose their special meaning when preceded by a backslash '/'. to get a backslash by itself, use a double backslash. note that when using regular expressions in Java source code, some care has to be taken to get the backslashes right (due to yet another level of escaping being necessary for Java ).

 

 

Related Article

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.