(a) Introduction to regular expressions and grammars
- The string class uses several methods of regular expressions:
- Valid characters supported by regular expressions:
- Special characters:
- Pre-defined characters:
- Square brackets Expression:
- Parenthesis expression: Used to make multiple expressions into a subexpression, you can use the OR operator "|", such as a regular expression: "(AA|BB|CC)" is to match "AA", "BB", "CC" One of the three strings.
- Boundary match:
- Greedy, reluctant, possessive number identifiers:
(ii) Simple usage of Java regular expressions
- Two key classes: (1) Pattern: The representation of a regular expression after it is compiled in memory. is an immutable class that can be used concurrently by multiple threads, (2) Matcher: Saves the various states involved in performing a match, and multiple Matcher objects can share a pattern object.
- Example of a simple usage program:
1 // Output: True 2 Pattern p = pattern.compile ("a*b"); 3 Matcher m = P.matcher ("AABZAAADAAAFBC"); 4 // Output: False
Common methods of the Matcher class:
- Examples of programs:
1 Public Static voidtest1 () {2System.out.println (Pattern.matches ("A\\WB", "A_b"));//Output: True3 4Pattern p = pattern.compile ("A*b");5Matcher m = P.matcher ("AABZAAADAAAFBC");6 7System.out.println (M.matches ());//Output: False8System.out.println (M.find ());//Output: True9System.out.println (M.group ());//output: bTenSystem.out.println (M.start ());//Output: 2 OneSystem.out.println (M.end ());//Output: 3 ASystem.out.println (M.lookingat ());//Output: True -M.reset ("Zab"); -System.out.println (M.lookingat ());//Output: False the } - - Public Static voidtest2 () { -Matcher m = pattern.compile ("\\w+"). Matcher ("Java is very easy!"); + - while(M.find ()) { +System.out.println (M.group () + "substring start position:" + m.start () + ", End position:" A+m.end ()); at } - - inti = 0; - while(M.find (i)) { -System.out.print (M.group () + "\ T"); -i++; in } - to //Output: + //starting position of the Java substring: 0, End position: 4 - //is substring start position: 5, End position: 7 the //very the starting position of the substring: 8, end Position: * //Easy substring start position: 13, End Position: $ //Java Ava va A is are s very very ery ry y easy easy asy sy yPanax Notoginseng } - the Public Static voidtest3 () { +string[] mails = {"[email protected]", "[email protected]", A"[Email protected]", "[email protected]" }; theString Mailregex = "\\w{3,20}@\\w+\\. (com|cn|edu|org|net|gov) "; +Pattern Mailpattern =Pattern.compile (Mailregex); - $Matcher Mailmatcher =NULL; $ - for(String mail:mails) { - if(Mailmatcher = =NULL) { theMailmatcher =mailpattern.matcher (mail); -}Else {Wuyi mailmatcher.reset (mail); the } - WuSystem.out.println (Mail + (mailmatcher.matches)? "Yes": "No") -+ "A valid e-mail address"); About } $ - //Output: - //[email protected] is a legitimate e-mail address - //[email protected] is a legitimate e-mail address A //[email protected] is not a valid email address + //[email protected] is not a valid email address the - } $ the Public Static voidtest4 () { theMatcher m = pattern.compile ("\\bre\\w*"). Matcher ( the"Java is real good at Inrestart and regex."); theSystem.out.println (M.replaceall ("haha")); - in //Output: the //Java is haha good at inrestart and haha. the About}
Finish
Basic collation of Java regular expressions