In Java, using regular expressions to extract () the contents of the _java

Source: Internet
Author: User

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

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: info-contact@alibabacloud.com 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.