Java uses regular expressions to extract () Content

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.