I encountered a small problem yesterday. I needed to process some users in batches. The format of the users sent from the front desk was as follows. I wanted to extract the content between the brackets (without brackets).
Teacher 10(0010)
Teacher 11(0011)
Teacher 9(009)
Teacher 12(0012)
Teacher 13(0013)
Teacher 14(0014)
I originally wanted to use Java's String.split() and substring() to do it, but it was troublesome to process it multiple times, so I used regular expressions. Although I have almost forgotten the syntax, I think it is more convenient to use assertions (the key is to hope that the results do not have parentheses). Open RegexBuddy and give it a try, and it’s easy to do:
The following is the java implementation code:
Finally, here are the zero-width assertions used:
wide assertion | (?=exp) | Match the position before exp |
---|---|---|
(?<=exp) | Match the position after exp | |
(?!exp) | Matches a position that is not followed by exp | |
(?<!exp) | Match the position that is not preceded by exp |