Young boy, I see your skeleton is exquisite, it is a once in a century martial arts talent, you and I are predestined people, with the teacher to cultivate
- Regular formula 18 – type 1: straight huanglong
- Regular 18 – Type 2: control crane to capture the dragon
- Regular formula 18 – Formula 3: The dragon jumps in the deep
- Regular formula 18 – Type 4 ash
Next: regular 18 – type 2: control crane capture dragon
Hide fan fairy :" tu son, just did extract useful information from more than 4 million characters, but well..." Jet :" HMM... ", mixed with some scrap, master please feel at ease, wait for my splite processing "Tibetan fan fairy :" Silly son, more than 4000 strings, you splite not time?" Jet :" disciple stupid, also ask master express."Copy the code
[one]Golden scale trapped cave, dragon leap in the deep
1. The group:Gold scales are not in the pool.
Hide fan fairy :" above like a dragon trapped in the pool, bound by weeds, now you need to find him, and for him to break the barrier..." Jet :" what should a disciple do?" Hide fan fairy :" we need to get the place is.* corresponding parts, can use the grouping "Jet :" that can be fixed point?" Hide fan fairy :" however also, the method is very simple, add bracket line."Copy the code
Matcher. group(1) indicates that the first group is selected. That’s how it works.”
private static void regexHtml(String target) { String regex="username\">(.*?) < "; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(target); while (matcher.find()){ System.out.println(matcher.group(1)); }}Copy the code
2. The group number
Jet: So the default is group 0. If matcher.group(0) is the same as the original result. "Jett :" See, is there a number of brackets equals a number of groups?"Copy the code
Matcher.groupcount () can be used to obtain the number of groups, and see the table below.
regular | Group number | Target group |
---|---|---|
username\">.*? < |
0 | – |
username\">(.*?) < |
1 | 1 |
(username)">(.*?) (<) |
3 | 2 |
(username)(">(.*?) (<)) |
4 | 3 |
Jet :” through my sharp eyes, I just count the left parentheses. The number is the number of groups.” Hide fan fairy :” xiu er like you, indeed so.”
3. The group name
Jet :" this 1,2,3,4, hundreds of groups of regulars, a number is not crazy?" Hide fan fairy :" hey, small sample, and ambition to write hundreds of groups of regular? All right, I'll give you the knack -- name the group." Jet: I know. I can name it as soon as I guess.Copy the code
Hide fan fairy :” in the group at the front?
can be named, for clarity, the teacher likes to add a G prefix.
private static void regexHtml(String target) { String regex="(username)(\">(? <Gname>.*?) (<)) "; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(target); while (matcher.find()){ System.out.println(matcher.group("Gname")); }}Copy the code
[贰] Three thousand flying dragons leap out of the deep
1. Test data
Hidden fan fairy :" the following is a mess of articles, there are many dates, you filter out the year, month and day." Jet :" That's easy. Look at me -- The dragon leaps from the deep."Copy the code
2. Extraction date
private static void regexToday(String target) { String regex="(? The < Gyear > \ \ d {4}) (? The < Gmonth > \ \ d {2}) (? The < Gday > \ \ d {2}) "; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(target); while (matcher.find()){ System.out.println(matcher.group("Gyear")); System.out.println(matcher.group("Gmonth")); System.out.println(matcher.group("Gday")); }}Copy the code
3. Regular expression analysis and gradual improvement
Tibetan fan fairy :” the most difficult regular is careful observation, there are some negative numbers can not match.”
Jet: I’ll see what I can do.
---->[regular expression analysis]---- original regular (? The < Gyear > \ \ d {4}) (? The < Gmonth > \ \ d {2}) (? <Gday>\\d{2} day) This can only match the year of four consecutive digits, the month of two digits, and the date of two digits. Fit -45 Jan 1 now (? <Gyear>-? (\ d {1, 4} years)? The < Gmonth > \ d {1, 2}) (? The < Gday > \ d {1, 2}) -? On behalf of any - can be matched to, then the original rigid \ d {4} lower requirement \ d {1, 4}, suggests that there is a number with respect to OKCopy the code
4. Add requirements
Tibetan fan fairy :" now roughly get the text after the date, not very accurate." Jet :" Let me see... Simply add \W+ but it doesn't match the number." Tibetan Fan xian :" The quality of the string itself is not high, the regularity is not strong. It's a good match." (? <Gyear>-? (\ d {1, 4} years)? The < Gmonth > \ d {1, 2}) (? The < Gday > \ d {1, 2}) (? <Ginfo>\W+)Copy the code
By now, you should have learned to group so that dates and simple introductions can be captured easily.
It’s not too hard to plug into a database or anything. Did you get it?
[C]
I wrote an article on “Playing with Strings” — Automatic code generation, free hands basically have Splite to handle strings
Now I know the third formula, so easy. The following Android custom controls, we should not be unfamiliar. Need :1. Get the class name, 2. Get the attribute name attr, 3
<? The XML version = "1.0" encoding = "utf-8"? > <resources> <declare-styleable name="TolyProgressBar"> <attr name="z_pb_bg_color" format="color"/> <attr name="z_pb_bg_height" format="dimension"/> <attr name="z_pb_on_color" format="color"/> <attr name="z_pb_on_height" format="dimension"/> <attr name="z_pb_txt_color" format="color"/> <attr name="z_pb_txt_size" format="dimension"/> <attr name="z_pb_txt_offset" format="dimension"/> <attr name="z_pb_txt_gone" format="boolean"/> </declare-styleable> </resources>Copy the code
Shout with me: Control the crane, catch the dragon, jump the dragon so you can get the raw data, and then you can do whatever you want.
private static void regexAttr(String target) {
String regex="<attr name=\"(?<Gattr>.*?)\".*format=\"(?<Gtype>.*?)\"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(target);
while (matcher.find()){
System.out.println(matcher.group("Gattr")+" 类型:"+matcher.group("Gtype"));
}
}
Copy the code
For some templating, code generation, or string processing, the main thing is to get the required fields
The two moves are the most basic regular processing, of course, is the most effective and practical.
The same is true for fetching class names. You can use a Map to hold the matched data for use
private static void regexAttr(String target) { Map<String,String> map =new HashMap<>(); String regex="<attr name=\"(? <Gattr>.*?) \".*format=\"(? <Gtype>.*?) \ ""; Matcher matcher = Pattern.compile(regex).matcher(target); while (matcher.find()){ map.put(matcher.group("Gattr"),matcher.group("Gtype")); } String regexClass="<declare.*? (\ "? <Gclass>.*?) \ ""; Matcher matcherClass = Pattern.compile(regexClass).matcher(target); while (matcherClass.find()){ map.put("ClassName",matcherClass.group("Gclass")); } System.out.println(map); }Copy the code
Afterword.
1—- This article was originally written by Zhang Fengjie, please note when reprinted
2—- If there is anything you want to communicate, please leave a comment. Also can add wechat :zdl1994328 3—- personal ability is limited, if there is not right welcome everyone to criticize the evidence, must be modestly correct 4—- see here, I here thank you for your love and support, scan code concern – the king of programming