Yesterday encountered a small problem, the need to batch processing some users, and the foreground of the user format is as follows, to extract the contents of brackets (without parentheses)
Teacher 10 (0010)
Teacher 11 (0011)
Teacher 9 (009)
Teacher 12 (0012)
Teacher 13 (0013)
Teacher 14 (0014)
Originally wanted to use the Java String.Split () and substring () to fix, but need to deal with many times more troublesome, use regular expression. Although the grammar is almost forgotten, the impression is more convenient with assertions (the key is that the results are not enclosed). Open Regexbuddy try, easy to handle:
Below is the Java implementation code:
Copy Code code as follows:
Public list<string> getteacherlist (String managers) {
List<string> ls=new arraylist<string> ();
Pattern pattern = Pattern.compile ("<=\\ () (. +?) (?=\\))");
Matcher Matcher = pattern.matcher (managers);
while (Matcher.find ())
Ls.add (Matcher.group ());
return LS;
}
Finally, attach the 0-wide assertion used:
| Wide Assertion |
(? =exp) |
Match the position of the exp front |
| (? <=exp) |
Match the position of the exp back |
| (?! Exp |
Match the back to the position not exp |
| (? <!exp) |
Matches a position that is not exp at the front |