1 PackageShb.java.demo3;2 3 ImportJava.util.regex.Matcher;4 ImportJava.util.regex.Pattern;5 6 /**7 * Regular expression simple operation8 * @Package: Shb.java.demo39 * @Description:Ten * @authorshaobn One * @Date 2015-8-28 a.m. 10:54:30 A */ - Public classRegexDemo2 { - Public Static voidMain (string[] args) { theIftrue ("[Email protected]", "[0-9]{8,11}@ (\\w*|[ 0-9]+) \ \. [a-z]*]); -Splitregex ("C:\\demo\\txt", "\\\\"); -Iftrue ("ABC bbb", "\\AAbc\\b"); -Iftrue ("131222", "(13|2|3)". { 2,4}2$ "); +Iftrue ("Java", "^j.*a$"); -Iftrue ("Javaa", "java*"); +Iftrue ("aBc123", "[a-za-z]{3}\\d{3,9}"); AIftrue ("13635586954", "[1][3,5,8]\\d{9}"); atIftrue ("13635586954", "[1][3,5,8]\\d*"); -Splitregex ("The Day is Friday", "+"); -Splitregex ("Ttddayiisffriday", "(.) \\1 "); - } - //match a string based on a regular expression - Public Static voidiftrue (String str,string regex) { inPattern p =pattern.compile (regex); -Matcher m =P.matcher (str); to BooleanFlag =m.matches (); + System.out.println (flag); - } the //cutting strings based on regular expressions * Public Static voidSplitregex (String str,string regex) { $string[] STRs =str.split (regex);Panax Notoginseng for(String s:strs) { - System.out.println (s); the } + A } the //cutting strings based on regular expressions + Public Static voidSplitRegex2 () { -Pattern pattern = Pattern.compile ("[, |] +"); $string[] STRs = Pattern.split ("Java Hello World java,hello,,world| Sun "); $ for(inti=0;i<strs.length;i++) { - System.out.println (Strs[i]); - } the } -}
Java Regular Expression exercises