Java Regular Groups Group

Source: Internet
Author: User

Made a small program, in order to replace similar (8, 8, ' Jilin ', ' 0 ') ==> (' 8 ', ' 8 ', ' Jilin ', ' 0 ')

Two concepts were learned, capturing groups and non-capturing groups

Capture Group [()] means that the regular is divided into one group (whole), and in the form of a group to obtain his

The non-capturing group [?:] means capturing these things, but not getting them when they get them, like

Pattern pattern = pattern.compile (?: \ \ () ([0-9]+) (?: [,]) (\\s*\\d+) ");

Where I don't want to get to the first ' (' So I'll take it with (?: \ In the form of \ () so that the code is not fetched.

The overall meaning of this regular is to get a match for two 8 if we want to get these two 8, just so get

while (M.find ()) {
M.group (1);
M.group (2);
}

The overall code is as follows

/*** Add the number outside the single quotation mark (8, 8, ' Jilin ', ' 0 ') ==> (' 8 ', ' 8 ', ' Jilin ', ' 0 ') *@paramLine *@return     */     Public Staticstring replaceNum2 (string line) {//(8, 8, ' Jilin ', ' 0 ')pattern Pattern= Pattern.compile (?: \ \ () ([0-9]+) (?: [,]) (\\s*\\d+) "); Matcher m=Pattern.matcher (line); String[] s=NewString[2];  while(M.find ()) {s[0] = M.group (1); s[1] = M.group (2); }        if(S[0].equals (s[1]) { line= Line.replace (S[0], "'" +s[0].trim () + "'"); }Else{ Line= Line.replace (S[0], "'" +s[0].trim () + "'"); Line= Line.replace (s[1], "'" +s[1].trim () + "'"); }        returnLine ; }

Java Regular Groups Group

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.