There was a small problem yesterday, and some users need to be processed in batches. The user format sent from the front-end is as follows. The content in the middle of the brackets should be extracted (without parentheses)
Instructor 10 (0010)
Instructor 11 (0011)
Instructor 9 (009)
Instructors 12 (0012)
Instructor 13 (0013)
Instructor 14 (0014)
I originally wanted to use Java's string. Split () and substring () to solve the problem, but it was troublesome to handle it multiple times, so I used a regular expression. Although the syntax is almost forgotten, it is more convenient to use assertions in the impression (the key is to expect that the results do not contain parentheses ). Open regexbuddy and try it. It's easy to do:
Below is the Java implementation code:
CopyCode The Code is as follows: public list <string> getteacherlist (string managers ){
List <string> ls = new arraylist <string> ();
Pattern pattern = pattern. Compile ("(? <=\\ () (. + ?) (? = \\))");
Matcher = pattern. matcher (managers );
While (matcher. Find ())
Ls. Add (matcher. Group ());
Return ls;
}
The following assertion is attached:
Wide assertions |
(? = Exp) |
Match the position before exp |
(? <= Exp) |
Match position after exp |
(?! Exp) |
The position behind matching is not exp |
(? <! Exp) |
Match the position that is not exp |