Regular expression Basics and Java usage

Source: Internet
Author: User

Regular Expression Basics

Regular expression Syntax (1)
Ordinary characters: Letters, numbers, men, underscores, and punctuation that are not specifically defined are "normal characters". An ordinary character in an expression that matches the same character when matching a string
    • Simple escape Character:

\ n: Represents a line break

\ t: Represents a tab

\ \: represents \ Itself

\^ ...... : The representation matches the character itself

Regular expression Syntax (2)

    • Standard Character Set:

expressions that can match multiple characters

Attention is case-sensitive, uppercase is the opposite meaning

\d: Any number, any one of the 0~9

\w: Any letter or underscore or kanji, or any of the a~z,a~z,0~9

\s: Any of the whitespace characters, including spaces, tabs, newline characters, and so on.

. : The decimal point can match any one character

Regular expression Syntax (3)

    • Custom Character Sets:

[] square brackets match, can match any one character in square brackets

[[email protected]]: matches any one of the characters in square brackets

[^ABC]: matches any character except ABC

[F-k]: matches any one of the characters between "F"-"k"

[^a-f0-3]: matches any one of the non-a-f and non-0-3 characters

Regular expression Syntax (4)

    • Special symbols for modifying the number of matches

{n}: Expression repeats n times

{M,n}: expression repeats at least m times and repeats up to n times

{m,}: expression repeats at least M-times

? : Match expression 0 or 1 times, equivalent to {0,1}

+: expression appears at least 1 times, equivalent to {1,}

*: expression does not appear or occurs any time, equivalent to {0,}

The greedy pattern in the number of matches (the more matches the character, the better, the default)

The non-greedy pattern in the number of matches (the fewer matching characters the better, the special symbol that modifies the number of matches, plus one "? "No.). Example: \d{2,4}?, this changes the mode to non-greedy

Regular expression Syntax (5)

    • character boundary (this group of tags matches not a character but a position, where a condition is met)

^: matches the position where the string starts (in one line)

$: matches where the string ends

\b: Match word boundaries//For example boy\b is not a boundary mismatch on boy's right

Using regular expressions and common functions in Java

public class Testregex {public static void main (String []args) {//pattern p = pattern.compile ("^\\d{4}");  The backslash in Java is to be escaped with two preceding, because \ in \d is ambiguous with the Pattern p = pattern.compile ("(\\d{4}) ([a-z]{2})"); Matcher m = P.matcher ("Asdfsadfsafd2342314asdfsadf2342asdfsaf"); System.out.println (M.matches ());//Match entire target string System.out.println (M.find ());//Find string list<string> list = new Arraylist<string> ();//used to store the found Groupwhile (M.find ())//m.find () find one and go for the next {System.out.println ( ());// Group (), group (0) has the same function as System.out.println ( (0)); System.out.println ( (1)); System.out.println ( (2));//If write 3 will be out of range List.add ( ());} string []STR = "aa33fdf55cc". Split ("\\d{2}");//divide string str2 = "Asd2dsfdfds" by two integers. ReplaceAll ("\\ds", "* *");//Replace DS , the new string is generated because the constants above are immutable System.out.println ("Asdfsdf". Matches ("\\w+"));//Match String}}

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: 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.