Regular expression lookup, mainly used to split () in string class;
String str;
Str.split (); In the method to intercept by what rule, return a string array
Common rules for intercepting:
Str.split ("\.") Follow. to intercept.
Str.split ("") Intercept by Space
Str.split ("cc+") in accordance with the C character intercept, 2 C or more
Str.split ((1) \\.+) in the string containing 2 characters or more places to intercept (1) indicates that the group is 1
Examples of interception;
To intercept in accordance with a group; a position in two or more two places.
String str = "Publicstaticccvoidddmain";
Reusing a String ragex1= for an expression in a group
(.) \\1+ ";
string[] ss = Str.split (RAGEX1);
for (String st:ss) {
System.out.println (ST);
}
Intercept
String Ragex = "" according to two cc+;
Cutting
String STRs = "Publicstaticccvoidddmain";
String Ragexs = "cc+";
String[] s = strs.split (Ragexs);
for (String ssss:s) {
System.out.println (SSSS);
}
System.out.println ("=-=========");
substitution in regular expressions;
Syntax definition rules;
String s =str.replaceall (Ragex, NEWSTR);
The substitution in the string is replace ();
Replace 4 or 4 consecutive digits with *
Replace
String str= "wei232123jin234";
String Ragex = "\\d{4,}";
String newstr = "*";
String s =str.replaceall (Ragex, newstr);
System.out.println (s);
Replace the duplicate string with a *
String str = "wwweiei222222jjjiiinnn1232";
String Ragex = "(.) \\1+ ";
String newstr = "*";
String s = Str.replaceall (Ragex, newstr);
System.out.println (s);
Will I ... I... To.. To eat. Eat... I'm going to eat dinner.
String str = "I ... I... To.. To eat. Eat... Rice ";
String regex = "\\.+";
String newstr = "";
Str=test (str, regex, NEWSTR);
Regex = "(.) \\1+ ";
NEWSTR = "$";
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;
}
Get the string of a regular expression
1, create a Pattern object based on a defined regular expression
2, using Pattern object class to match
3, to determine if true
4, add to Group
Example
String str = ' public static void main (string[] args) '
+ ' public static void main (string[] args) public static void Ma In (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);
}
Homework:
1, get the user in
2, get DHFJKSADUIRFN 11@qq.com DSJHKFA wang@163.com the mailbox number in the Wokaz
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 ()/////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); }
}