1 ImportJava.util.regex.Matcher;2 ImportJava.util.regex.Pattern;3 4 Public classRegexdemo {5 6 Public Static voidMain (string[] args) {7 method_5 ();8 }9 Ten /* One * Requirements: Define a function to check the QQ number. Requirements: Length 5~15. Can only be a number, 0 cannot start A */ - Public Static voidmethod_1 () { -String QQ = "101885"; theString regex = "[1-9]\\d{4,14}"; -SYSTEM.OUT.PRINTLN (QQ + ":" +qq.matches (regex)); - } - + /* - * Requirements: Mobile phone number verification + */ A Public Static voidmethod_2 () { atString Tel = "18190029004"; -String regex = "1[358]\\d{9}"; -SYSTEM.OUT.PRINTLN (tel + ":" +tel.matches (regex)); - } - - /* in * Demand: Overlapping word cutting - */ to Public Static voidmethod_3 () { +String str = "Akjdhkahjjauuuuqjhadwww;k"; -String regex = "(.) \\1+ "; thestring[] arr =str.split (regex); * for(String s:arr) { $ System.out.println (s);Panax Notoginseng } - } the + /* A * Requirements: Replacement 1. Mobile phone number replacement 2. Overlapping word substitution the */ + Public Static voidMethod_4 () { -String Tel = "13840056789"; $String regex = "(\\d{3}) (\\d{4}) (\\d{4})"; $System.out.println (Tel.replaceall (Regex, "$1****$3")); - -String str = "Akjdhkahjjauuuuqjhadwww;k"; theString reg = "(.) \\1+ "; -System.out.println (Str.replaceall (Reg, "*"));Wuyi } the - /* Wu * Need: Get three-letter words - */ About Public Static voidmethod_5 () { $String str = "AJKDH iqw iqiw qii,wip aido q QW"; -String regex = "\\b[a-z]{3}\\b"; -Pattern p =pattern.compile (regex); -Matcher m =P.matcher (str); A while(M.find ()) { + System.out.println (M.group ()); theSystem.out.println (M.start () + "" +m.end ()); - } $ } the}
Regular Expression 1