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