[Java learning notes] Regular Expression

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

1. Introduction:

Regular Expressions are an important tool for Java to process strings and texts.

Java. util. RegEx. pattern matching class: matches the abstract results expressed by a string in a pattern.

Java. util. RegEx. matcher pattern class: used to represent a compiled regular expression.

 


2. Example

Import java. util. RegEx. pattern;

Public class regexdemo1 {
Public static void main (string Arg []) {
String expr;
String STR;

Expr = "P. t ";
STR = "pot ";
Boolean B = pattern. Matches (expr, STR); // static method, called directly
If (B)
System. Out. println (STR + "matches pattern" + expr );
Else
System. Out. println (STR + "does not match pattern" + expr );
}
}

 

However, expr in this example must be compiled each time before matching is performed, and the following example does not.

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

Public class regexdemo2 {
Public static void main (string Arg []) {
String expr;
String STR;

Expr = "P // WT"; // in Java, // indicates the backslash of the regular expression to be inserted, and the subsequent characters have special meanings. /W word characters
STR = "pot ";
Pattern pattern = pattern. Compile (expr );
Matcher = pattern. matcher (STR );
If (matcher. Matches ())
System. Out. println (STR + "matches pattern" + expr );
Else
System. Out. println (STR + "does not match pattern" + expr );
}
}

 

The pattern in the above example can be reused, for example:

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

Public class regexdemo3 {
Public static void main (string Arg []) {
String expr;

Expr = "P [aeiou] t ";
Pattern pattern = pattern. Compile (expr );
For (INT I = 0; I <Arg. length; I ++ ){
Matcher = pattern. matcher (ARG [I]);
If (matcher. Matches ())
System. Out. println (ARG [I] + "matches pattern" + expr );
Else
System. Out. println (ARG [I] + "does not match pattern" + expr );
}
}
}

 

Find the matched location in a paragraph:

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

Public class regexdemo4 {
Public static void main (string Arg []) {
String expr;
String STR;

Expr = "Bark ";
STR = "this is a larger block of text the pattern matcher" +
"Will search for the word bark. The pattern is very" +
"Simple. It will only match the word bark, and nothing" +
"Else. You can search a block of text for any of the" +
"Regular Expression Patterns -- not just bark .";
Pattern pattern = pattern. Compile (expr );
Matcher = pattern. matcher (STR );
Boolean found = matcher. Find ();
While (found ){
Int location = matcher. Start ();
System. Out. println (expr + "found at" + location );
Found = matcher. Find (location + 1 );
}
}
}
========================

Example:

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

/**
* Regular Expression example
*
* @ Author leizhimin 2009-7-17 9:02:53
*/
Public class testregx {
Public static void main (string [] ARGs ){
Pattern P = pattern. Compile ("F (. + ?) K ");
Matcher M = P. matcher ("fckfkkfkf ");
While (M. Find ()){
String S0 = M. Group ();
String S1 = M. Group (1 );
System. Out. println (S0 + "|" + S1 );
}
System. Out. println ("---------");
M. Reset ("fucking! ");
While (M. Find ()){
System. Out. println (M. Group ());
}

Pattern p1 = pattern. Compile ("F (. + ?) I (. + ?) H ");
Matcher M1 = p1.matcher ("finishabigfishfrish ");
While (m1.find ()){
String S0 = m1.group ();
String S1 = m1.group (1 );
String S2 = m1.group (2 );
System. Out. println (S0 + "|" + S1 + "|" + S2 );
}

System. Out. println ("---------");
Pattern P3 = pattern. compile ("(19 | 20) // D ([-/.]) (0 [1-9] | 1 [012]) // 2 (0 [1-9] | [12] [0-9] | 3 [01]) ");
Matcher m3 = p3.matcher ("1900-01-01 2007/08/13 19004251.01 1900 01 01 1900-01.01 1900 13 01 1900 02 31 ");
While (m3.find ()){
System. Out. println (m3.group ());
}
}
}

Output result: fck | C
Fkk | K
---------
Fuck
Finish | in | S
Fishfrish | ishfr | S
---------
1900-01-01
2007/08/13
190000001.01
1900 01 01
1900 02 31

Process finished with exit code 0

 

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

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.