This article for everyone to share the Java regular expression matching, replacement, find and cut operations, interested friends can refer to the
Import java.util.ArrayList;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern; public class Test {public static void main (string[] args) {getstrings ()/////Get the specified content in the specified string content with a regular expression System.out.pr
Intln ("********************"); Replace ();
Replaces the string content with a regular expression System.out.println ("********************"); Strsplit ();
Use regular expressions to cut string System.out.println ("********************"); Strmatch ();
String match} private static void Strmatch () {String phone = "13539770000"; Check whether phone is a qualified mobile number (standard: 1 start, second for 3,5,8, 9 digits for any number) System.out.println (phone + ":" + phone.matches ("1[358][0-9]{9,9}"));
True String str = "ABCD12345EFGHIJKLMN"; Check whether the middle of 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 Strsplit () {String str = ' asfasf.sdfsaf.sdfsdfas.asdfasfdasfd.wrqwrwqer.asfsafasf.
SAFGFDGDSG "; string[] STRs = StR.split ("\.");
for (String s:strs) {System.out.println (s); The private static void Getstrings () {String str = "rrwerqq84461376qqasfdasdfrrwerqq84461377qqasfdasdaa65464
5aafrrwerqq84461378qqasfdaa654646aaasdfrrwerqq84461379qqasfdasdfrrwerqq84461376qqasfdasdf "; Pattern p = pattern.compile ("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);
The private static void replace () {String str = "ASFAS5FSAF5S4FS6AF.SDAF.ASF.WQRE.QWR.FDSF.ASF.ASF.ASF";
Replace the. In the string with the _, because. is a special character, so you use \. expression, and because \ is a special character, you use \ n to express it.
str = Str.replaceall ("\.", "_");
System.out.println (str);
}
}
The focus of the code has been noted for everyone, I hope you can carefully study, summed up learning skills.