In Full Match you get everything that regex says about, even non-capturing groups. You need to get appropriate Match to get rid of non-capturing groups. The other solution is to use positive lookahead instead of capturing group. Check the regex below. I also removed some unnecessary (IMO) groups.
(?:Bundle\s+Components|Included\s+Components)\s+.*?(?=Bundle)
It results with only one, full, match.
Demo
PS: The sign of new line just before "Bundle" will be captured as well in this solution.
Answer from Egan Wolf on Stack OverflowNon-capturing group in java RegEx - Stack Overflow
Java Regex Non-Capturing Group - Stack Overflow
Non capturing group java regex - Stack Overflow
java - regular expressions: quantifying a non-capturing group - Stack Overflow
In Full Match you get everything that regex says about, even non-capturing groups. You need to get appropriate Match to get rid of non-capturing groups. The other solution is to use positive lookahead instead of capturing group. Check the regex below. I also removed some unnecessary (IMO) groups.
(?:Bundle\s+Components|Included\s+Components)\s+.*?(?=Bundle)
It results with only one, full, match.
Demo
PS: The sign of new line just before "Bundle" will be captured as well in this solution.
You can do this with positive lookahead, since with this one the pattern inside the lookahead group is not included in the match:
((?:Bundle\\s+Components)|(?:Included\\s+Components))\\s+(.*?)(?=Bundle)
(not tested)
You need to make the String part optional by using a ?:
String regexFilter = "(?:String )?(Transformation) (Action)";
Also, there isn't much point in putting capturing groups around literal text (e.g. Transformation and Action) since you always know what those groups will capture.
String regexFilter = "(?:String )?Transformation Action";
Make String part optional using this regex:
String regexFilter = "\\b(?:String )?Transformation Action\\b";
PS: I have also added \\b (word boundary) to make your don't match Transformation Action111 OR xyzTransformation Action type strings.
It means that the grouping is atomic, and it throws away backtracking information for a matched group. So, this expression is possessive; it won't back off even if doing so is the only way for the regex as a whole to succeed. It's "independent" in the sense that it doesn't cooperate, via backtracking, with other elements of the regex to ensure a match.
I think this tutorial explains what exactly "independent, non-capturing group" or "Atomic Grouping" is
The regular expression
a(bc|b)c(capturing group) matches abcc and abc. The regexa(?>bc|b)c(atomic group) matches abcc but not abc.When applied to abc, both regexes will match
ato a,bcto bc, and thencwill fail to match at the end of the string. Here their paths diverge. The regex with the capturing group has remembered a backtracking position for the alternation. The group will give up its match,bthen matches b andcmatches c. Match found!The regex with the atomic group, however, exited from an atomic group after
bcwas matched. At that point, all backtracking positions for tokens inside the group are discarded. In this example, the alternation's option to trybat the second position in the string is discarded. As a result, whencfails, the regex engine has no alternatives left to try.
Hi everyone!
I'm struggling to understand what are non-capturing groups.
My take and if I understood correctly:
-
when you group, you're applying precedence in terms of evaluation, like normal parenthesis would work in a math expression.
-
a normal group it creates some sort of indexing that the regex engine can use for other checks later on if it has advanced stuff like tagging or recursion.
-
when you use
?:- non-capturing group - you're also grouping as well but it doesn't do any indexing.
Is this correct?
Would there any difference between simple stuff like (^$)|(^(No|Yes)$) to (?:^$)|(?:^(?:No|Yes)$) ?
Thank you in advance.
Why not use look-ahead / look-behind instead?
They are non-capturing and would work easily here:
str = str
.replaceAll(
"(?<=\\[assembly: AssemblyVersion\\(\"\\d\\.\\d\\.).*(?=\"\\)\\])",
"4.0"
);
As an alternative to a look-behind, you can use capturing groups around what you want to keep, and keep what you want to replace in a non-capturing group or no group at all:
String str="[assembly: AssemblyVersion(\"1.0.0.0\")]";
str=str.replaceAll("(\\[assembly:\\s*AssemblyVersion\\(\"\\d+\\.\\d+\\.)\\d+\\.\\d+(?=\"\\)\\])", "$014.0");
System.out.println(str);
See IDEONE demo