Java Regular Expression Basics

Source: Internet
Author: User
Tags character classes

1 Basic 2.1 Simple character class
Construction Description
[ABC] A, B or C
[^ABC] Characters except A, B or C
[A-za-z] A to Z or a to Z
[A-d[m-p]] A to D or M to P
[A-z&&[def]] D,e or F
@Test  Public void Testchar () {    // matches b,c or D    asserttrue ("D". Matches ("[BCD]"));     // match characters except a,b,c (negation)    Asserttrue ("D". Matches ("[^ABC]"));     // match any character from A to Z (range)    Asserttrue ("D". Matches ("[A-z]")    ; // match d,e or F (intersection)    Asserttrue ("D". Matches ("[A-z&&[def]]");}

2.2 Predefined character classes
Construction Description
. Any character
\d A number [0-9]
\d A non-numeric [^0-9]
\s White space character [\t\n\x0b\f\r]
\s Non-null characters
\w Regular characters [a-za-z_0-9]
\w Non-regular characters
@Test Public voidtestpredef () {//match any character (may not match line terminator)Asserttrue ("D". Matches ("."))); //Match NumbersAsserttrue ("4". Matches ("\\d"))); //match non-numericAsserttrue ("D". Matches ("\\d"))); //match white space characters [\t\n\x0b\f\r]Asserttrue ("". Matches ("\\s"))); //match non-null charactersAsserttrue ("D". Matches ("\\s"))); //match regular characters [a-za-z_0-9]Asserttrue ("D". Matches ("\\w"))); //match non-regular charactersAsserttrue ("!". Matches ("\\w"));}

2.3 quantifier
Greedy barely Description
X? X?? X appears once or no
x* X*? X appears 0 or more times
x+ X+? X appears one or more times
X{n} X{n}? X appears n times
X{n,} X{n,}? X appears at least n times
X{N,M} X{n,m}? X appears at least n times, up to M times
@Test Public voidtestquant () {//* Indicates occurrences 0 or more times (greedy, maximum match)Assertequals ("<b>d<b>". ReplaceAll ("<.*>", "#"), "#"); //* Indicates the occurrence of 0 or more times, which represents the minimum matchAssertequals ("<b>d<b>". ReplaceAll ("<.*?>", "#"), "#d #"); //+ indicates 1 or more occurrences (greedy, maximum match)Assertequals ("<b>d<b>". ReplaceAll ("<.+>", "#"), "#"); //* Indicates the occurrence of 1 or more times, which represents the minimum matchAssertequals ("<b>d<b>". ReplaceAll ("<.+?>", "#"), "#d #"); //{1} indicates 1 occurrencesAssertequals ("<b>d<b>". ReplaceAll ("<.{ 1}> "," # ")," #d # "); //{1,} indicates 1 times to multiple occurrencesAssertequals ("<b>d<b>". ReplaceAll ("<.{ 1,}> "," # ")," # "); //{1,8} indicates occurrences 1 to 8 timesAssertequals ("<b>d<b>". ReplaceAll ("<.{ 1,8}> "," # ")," # ");}

2.4 Grouping
@Test  Public void Testgroup () {    //  () denotes grouping, which indicates that there are 0 or 1 occurrences, and \\d\\d represents two digits    assertfalse ("Du2". Matches ("Du (\\d\\d)? " ));     // Match    Asserttrue ("Du12". Matches ("Du (\\d\\d)?") ));     // Mismatch , grouping conditions have occurred 2 times    Assertfalse ("du1212". Matches ("Du (\\d\\d)?") ));     // Match    Asserttrue ("du1212". Matches ("Du (\\d\\d) +");}

2.5 Boundary Matching
boundary constructs description
^ line Start
$ line end
\b word boundary
\b non-word boundary
\a input start
\z Enter end
@Test  Public void Testbound () {    //  ^ line start    asserttrue ("42du". Matches ("^ (\\d\\d) du"));     // $ line End    Asserttrue ("42du". Matches ("\\d\\d (DU) $"));     // \\b denotes word boundaries    Asserttrue ("A Dog". Matches (". *\\bdog\\b"));     // \\b represents a non-word boundary    Asserttrue ("A doggie". Matches (". *\\bdog\\b.*"));}

2.6 Pattern and Matcher basic
ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classMatcherdemo { Public Static voidMain (string[] args) {// Case insensitivePattern pattern = pattern.compile ("\\bdog\\B", pattern.case_insensitive); Matcher Matcher= Pattern.matcher ("The Doggie plays");  while(Matcher.find ()) {System.out.println (Matcher.group ());            System.out.println (Matcher.start ());        System.out.println (Matcher.end ()); }    }}

Output

Dog47
3 Example 3.1 user name regularization
/* *  ^            start *   [a-z0-9_-]  match a-z,0-9, underline, midline *    {4,10}     length 4 to 10*  $            End */  = "^[a-z0-9_-]{4,10}$"; 

3.2 Color Regular
/* *    ^ start    *     # "         #" Symbol *    (         start Group *      [a-fa-f0-9]{6} alphanumeric with length 6 *      |              or *      [a-fa-f0-9]{3} length 3 alphanumeric *    )         Group End *    $    End * / = "^# ([a-fa-f0-9] {6}| [A-fa-f0-9] {3}) $";

3.3 24 O'Clock Time
/*  *  (    Group Start *    [01]?[ 0-9]    0-9,00-09,10-19 *    |                or *    2[0-3]        20-23 *  )    Group End *  :     : Symbol *  [0-5][0-9]     */ = "([01]?[ 0-9]|2[0-3]): [0-5][0-9] ";

4 Conclusion

This paper briefly introduces the basic knowledge content of Java regular expression, and gives a simple and practical small example to understand the content of digestion. To master these basics, try to practice more and read the official documentation. The advanced content of the Java regular expression I will introduce in the next article.
Recommended Java Regular expression Learning some of the information:

Official: docs.oracle.com

English: vogella.com

English: runoob.com

Java Regular Expression Basics (GO)

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.