Regular Expression matching, replacement, search, cutting, and JAVA Regular Expression in java
Regular Expression search; mainly used in the String class split ();
String str;
Str. split (); specifies the rule to intercept the input in the method. A String array is returned.
Common interception rules:
Str. split ("\.") is intercepted.
Str. split ("") by Space
Str. split ("cc +") is intercepted by the c character, with two or more c characters.
Str. split (1) \. +) is truncated Based on the string containing 2 or more characters. (1) indicates that the group is 1.
Screenshot example;
Intercept by group. The intercept is located in two or more places.
String str = "publicstaticccvoidddmain"; // grouping and reusing expressions String ragex1 = "(.) \ 1 + "; String [] ss = str. split (ragex1); for (String st: ss) {System. out. println (st) ;}// extract String ragex = "" according to two cc +; // cut String strs = "publicstaticccvoidddmain"; String ragexs = "cc + "; string [] s = strs. split (ragexs); for (String SSSS: s) {System. out. println (SSSS);} System. out. println ("=-========= ");
Replace in the regular expression;
Syntax defining rules;
String s =str.replaceAll(ragex, newstr);
Replace ();
Replace four or more consecutive numbers *
// Replace String str = "wei232123jin234"; String ragex = "\ d {4,}"; String newstr = "*"; String s = str. replaceAll (ragex, newstr); System. out. println (s );
Replace the repeated string with *
String str ="wwweiei222222jjjiiinnn1232"; String ragex ="(.)\\1+"; String newStr ="*"; String s = str.replaceAll(ragex, newStr); System.out.println(s);
Change "I" to "I" to "I want to eat ".
String str = "I... i... yes .. yes. eat... eat... rice "; String regex = "\\. + "; String newStr =" "; str = test (str, regex, newStr); regex = "(.) \ 1 + "; newStr =" $1 "; test (str, regex, newStr); public static String test (String str, String regex, String newStr) {String str2 = str. replaceAll (regex, newStr); System. out. println (str2); return str2 ;}
Obtain the regular expression string
1. Create a Pattern object based on the defined Regular Expression
2. Use the Pattern object class to match
3. Check whether the value is true.
4. Add to group
Example;
String str = "public static void main(String[] args)" + " public static void main(String[] args)public static void main(String[] args)"; String ragex = "\\b[a-zA-Z]{4,5}\\b"; Pattern p =Pattern.compile(ragex); Matcher m = p.matcher(str); while(m.find()){ String s = m.group(); System.out.println(s); }
Job:
1. Obtain the user in
String str ="
2, get the dhfjksaduirfn 11@qq.com dsjhkfa wang@163.com wokaz in the mailbox number
String regex = " "; String[] strs=str.split(regex); for(String str2:strs){ String ragexDemo = "[a-zA-Z0-9]([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*" + "@([a-zA-Z0-9]+)\\.[a-zA-Z]+\\.?[a-zA-Z]{0,2}";Pattern p = Pattern.compile(ragexDemo);Matcher m = p.matcher(str2);while(m.find()){System.out.println(m.group()); } }
Sample Code:
Import java. util. arrayList; import java. util. regex. matcher; import java. util. regex. pattern; public class test {public static void main (String [] args) {getStrings (); // use a regular expression to obtain the specified content System in the specified String. out. println ("********************"); replace (); // replace the string content System with a regular expression. out. println ("********************"); strSplit (); // use a regular expression to cut the string System. out. println ("*********************"); strMatch (); // string matching} priva Te static void strMatch () {String phone = "13539770000"; // check whether the phone number is a qualified mobile phone number (Standard: 1, the second digit is 3, 5, 8, the last 9 digits are any numbers) System. out. println (phone + ":" + phone. matches ("1 [358] [0-9] {9, 9}"); // true String str = "abcd12345efghijklmn"; // check whether the str contains 12345 System. out. println (str + ":" + str. matches ("\ w + 12345 \ w +"); // true System. out. println (str + ":" + str. matches ("\\ w + 123456 \ w +"); // false} private static void str Split () {String str = "asfasf. sdfsaf. sdfsdfas. asdfasfdasfd. wrqwrwqer. asfsafasf. safgfdgdsg "; String [] strs = str. split ("\\. "); for (String s: strs) {System. out. println (s) ;}} private static void getStrings () {String str = "regular"; Pattern p = Pattern. compil E ("qq (.*?) Qq "); Matcher m = p. matcher (str); ArrayList <String> strs = new ArrayList <String> (); while (m. find () {strs. add (m. group (1);} for (String s: strs) {System. out. println (s) ;}} private static void replace () {String str = "asfas5fsaf5s4fs6af. sdaf. asf. wqre. qwr. fdsf. asf. asf. asf "; // convert. replace with _ because. is a special character, so use \. it must be \ because \ is a special character \\. to express. str = str. replaceAll ("\\. "," _ "); System. out. println (str );}}