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) Instructor 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 ImplementationCode:
PublicList <string>Getteacherlist (string managers) {list<String> ls =NewArraylist <string>(); Pattern Pattern= Pattern. Compile ("(? <=\\ () (. + ?) (? = \\))"); Matcher=Pattern. matcher (managers );While(Matcher. Find () ls. Add (matcher. Group ());ReturnLs ;}
The following assertion is attached:
| Assertion with Zero Width |
(? = 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 |
Reference: deerchao large regular expression 30-minute getting started tutorial