Java regular expression, Regular Expression
Java Regular Expression
Regular Expression
1. Two classes are provided in the java. util. regex package to support regular expressions. 1. Matcher: Engine for matching character sequence by interpreting Pattern
Public final class Matcher implements MatchResult
2. Pattern: Expression of Regular Expression Compilation
Public final class Pattern implements java. io. Serializable
Code:
Package yuki. regular; import java. util. regex. matcher; import java. util. regex. pattern; public class FirstTest {public static void main (String [] args) {/*** Pattern. The regular expression is compiled in the form of * public final class Pattern implements java. io. serializable */String str = "hi! I am a tony; gglad to see you! "; String regex =" \ p {Punct} "; Pattern pattern = Pattern. compile (regex); String [] strArr = pattern. split (str); for (int I = 0; I <strArr. length; ++ I) System. out. println ("strArr [" + I + "] =" + strArr [I]);/*** Matcher, by interpreting Pattern, perform the matching operation on character sequence * public final class Matcher implements MatchResult */Matcher matcher = pattern. matcher (str); System. out. println (matcher. matches ()? "Match": "mismatch"); System. out. println (Pattern. compile ("\ p {Punct} + "). matcher (".,.; "). matches ()? "Match": "do not match"); String s2 = "yuki@gmail.com"; Pattern p2 = Pattern. compile ("\ w + @ \ w +. [a-zA-Z] + "); Matcher m2 = p2.matcher (s2); System. out. println ("m2.matches () =" + m2.matches ());}}
Running result:
StrArr [0] = histrArr [1] = I am a tonystrArr [2] = gglad to see that you do not match m2.matches () = true
2. Regular Expressions supported by the String class
Code:
Package yuki. regular; import java. util. regex. matcher; import java. util. regex. pattern; public class SecondTest {public static void main (String [] args) {/*** match and replace */String date = "2015/2/27"; Pattern p = Pattern. compile ("/"); Matcher m = p. matcher (date); String s = m. replaceAll ("-"); System. out. println ("m. matches () = "+ m. matches (); System. out. println ("s =" + s); System. out. println ("m. replaceFirst (\ "-\") = "+ m. replaceFirst ("-"); // match the phone number String phone = "0755-28792686 "; string regex = "\ d {3, 4}-\ d {7, 8}"; boolean isPhone = phone. matches (regex); System. out. println ("isPhone =" + isPhone );}}
Running result:
m.matches() = falses = 2015-2-27m.replaceFirst("-") = 2015-2/27isPhone = true
3. Common examples
Code:
Package yuki. regular; public class ThirdTest {public static void main (String [] args) {/*** contains at least one of the strings */String s = "123,456,789,012,345 "; string s2 = "123,456,789,013,345"; String regex = ". * (234 | 678 | 012 ). * "; boolean isMatch = s. matches (regex); boolean isMatch2 = s2.matches (regex); System. out. println ("isMatch =" + isMatch); System. out. println ("isMatch2 =" + isMatch2); // matching amount String price = "499.00"; System. out. println ("price. matches (\ "\ d +. \ d + \ ")" + price. matches ("\ d +. \ d + "));}}
Running result:
isMatch = trueisMatch2 = falseprice.matches("\d+.\d+")true
For more information, see:
API: java. util. regex
Ma Jianwei _ JAVA basics _ Regular Expressions
Click the red button belowFollow meRight!
Kong Dongyang
February 27, year 15
Http://www.cnblogs.com/kodoyang/