Java: The basics of regular expressions

Source: Internet
Author: User
Tags character classes

Regular expression is a kind 匹配和处理字符串 of text Processing tool, Java mainly through java.util.regex the package Pattern , Matcher and PatternSyntaxException three classes to implement the regular expression.

Common Syntax
Character Meaning
X Character X
\ Marks the next character as a special character, text, reverse reference, or octal escape character.
Character class Meaning
[ABC] A, B or C
[A-za-z] Case A to Z
[^ABC] Non-A, B, c
Predefined character classes Meaning
. Any character
\d Number: [0-9]
\d Non-numeric: [^0-9]
\w Word character: [a-za-z_0-9]
\s whitespace characters: [\t\n\x0b\f\r]
Boundary Matching Device Meaning
\s whitespace characters: [\t\n\x0b\f\r]
^ The beginning of the line
$ End of Line
\b Word boundaries
\b Non-word boundary
Quantifiers Meaning
X? X, not once or once
x* X, 0 or more times
x+ X, one or more times
X{n} X, exactly n times
Logical operator Meaning
Xy X followed by Y
X| Y X or Y
X X, as capturing group



You can Pattern类 find all the syntax in the API

pattern and Matcher

The pattern object is instantiated by a static method Pattern.compile () Compile regular expression, and the string that you want to retrieve is passed into the Matcher () method of the Pattern object, generating a Matcher object.

Several important methods of the Matcher class:

    1. boolean find()
      Attempts to find the next subsequence of the input sequence that matches the pattern.
    2. int start()
      Returns the initial index of the previous match.
    3. int end()
      Returns the offset after the last matching character.
    4. Matcher appendReplacement(StringBuffer sb, String replacement)
      Implement non-terminal add and replace steps.
    5. StringBuffer appendTail(StringBuffer sb)
      Implement terminal add and replace steps.
    6. boolean matches()
      Try to match the entire region to the pattern.
    7. boolean lookingAt()
      Attempts to match the input sequence starting at the beginning of the zone with the pattern.
    8. String replaceFirst(String replacement)
      Replaces the first subsequence of an input sequence that matches a given replacement string.
    9. String replaceAll(String replacement)
      The replacement pattern matches each subsequence of the input sequence with the given replacement string.
    10. Matcher reset()Matcher reset(CharSequence input)
      Resets the matching device.

Import Java. Util. Regex. Matcher;Import Java. Util. Regex. Pattern;public class Test {/** * @param args * *public static void Main (string[] args) {//TODO auto-generated method stub/ * Test for SATRT () and End () */System. out. println("/* Test for SATRT () and End () */");String regex ="\\bcat\\b";String input ="Cat Cattie Cat";Pattern P = Pattern. Compile(regex);//instantiation of patternMatcher m = P. Matcher(input);//instantiation of Matcherint count =0;while (m. Find()) {count++;System. out. println("Match number"+ count);System. out. println("Start ():"+ M. Start());System. out. println("End ():"+ M. End());}/ * Test for Appendreplacement () and Appendtail () */System. out                . println("/* Test for Appendreplacement () and Appendtail () */");Regex ="\\d";input ="Hello1world2haha";m = Pattern. Compile(regex). Matcher(input);StringBuffer sb = new StringBuffer ();while (m. Find()) {m. Appendreplacement(SB,"-");} m. Appendtail(SB);//The rest of the pick upSystem. out. println(SB. toString());        / * Test for matches () and Lookingat () */System. out. println("/* Test for matches () and Lookingat () */");Regex ="Hello";input ="Hellooo";m = Pattern. Compile(regex). Matcher(input);System. out. println("Islookingat?:"+ M. Lookingat());System. out. println("Ismatches?:"+ M. Matches());        /* Test for String Replacefirst () and ... and reset () */System. out. println("/* Test for String Replacefirst () and ... and reset () */");Regex ="Cat";//Notice that the/b boundary character is less than the first Testinput ="Cat Cattie Cat";m = P. Compile(regex). Matcher(input);//instantiation of MatcherSystem. out. println("Replacefirst ():"+ M. Replacefirst("Dog"));M. Reset("CAT1 cat2 cat3");System. out. println("ReplaceAll ():"+ M. ReplaceAll("Dog"));}}
/ * Test for SATRT () and End () */Match Number 1Start():0End():3Match  Number 2Start(): OneEnd(): -/* Test forAppendreplacement () andAppendtail () */hello-world-haha/* Test forMatches () andLookingat () */islookingat?:trueIsmatches?:false/* Test forString Replacefirst () and... andReset () */replacefirst (): Dog Cattie Catreplaceall (): Dog1 dog2 Dog3
Group

A group is a regular expression separated by parentheses to obtain information about the group. In fact, some of the previously mentioned methods are overloaded with an int group parameter, which is used for groups.

If we call GroupCount () in the above code, it will return 0, because none of the above regular expressions involve groups.

A few related methods:

    1. String group()
      Returns the input subsequence that was matched by the previous match operation.
    2. String group(int group)
      Returns the input subsequence captured by a given group during a previous match operation.
    3. int groupCount()
      Returns the number of capturing groups in this match pattern.
Pick up/ * Test for Group () and GroupCount () */System. out. println("/* Test for Group () and GroupCount () */");p = Pattern. Compile("(\\d+,) (\\d+)");//two groupsm = P. Matcher("12,34-56,78");System. out. println("group count is:"+ M. GroupCount());while (m. Find()) {System. out. println("group ():"+ M. Group());//equivalent to group (0)System. out. println("Group (1):"+ M. Group(1));System. out. println("Group (2):"+ M. Group(2));}
/* Test for  group  () and  groupcount () */group  count is : 2  group  (): 12 , 34  group  (1 ): 12 , group  (2 ): 34  group  (): Span class= "Hljs-number" >56 , 78  group  ( Span class= "Hljs-number" >1 ): 56 , group  ( 2 ): 78  

First match \\d+,\\d , represent group(0) ;
group(1)Representative \\d+, ;
group(2)Represent\\d+

Split

Split is a method of implementing a string cut that can be called directly from a string object or through a pattern object.
Related methods:

    1. String[] split(String regex)
      Splits the string by matching the given regular expression.
    2. String[] split(String regex, int limit)
      Splits the string by matching the given regular expression.
        /* Test for Split () */System.out.println ("/* Test for Split () */"); String string ="I-1am-23leelit-456haha"; string[] strings1 = String.Split ("-\\d+"); for(String str:strings1) System.out.print (str +" ");//No Line breakSystem.out.println ("");//Same resultp = pattern.compile ("-\\d+"); string[] Strings2 = P.split (string); for(String STR:STRINGS2) System.out.print (str +" ");//No Line breakSystem.out.println ("");//Limitstring[] Strings3 = String.Split ("-\\d+",2);//Limit 2         for(String STR:STRINGS3) System.out.println (str);//Line break
forsplit*/i am leelit haha i am leelit haha iam-23leelit-456haha

Material
You can also refer to the API

Java: The basics of regular expressions

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.