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 Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ non-capturing regex groups in java
Non-Capturing Regex Groups in Java | Baeldung
January 8, 2024 - The pattern โ€œ[^:]:โ€ matches the protocol โ€” for example, โ€œhttp://โ€œ. The non-capturing group โ€œ(?:[.a-z]+/?)โ€ matches the domain name with an optional slash. Since the โ€œ+โ€ operator matches one or more occurrences of this pattern, weโ€™ll match the subsequent path segments as well.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ non-capturing-groups-java-regular-expressions
Non capturing groups Java regular expressions:
February 21, 2020 - A non-capturing group starts with (?: and ends with ). import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CapturingGroups { public static void main( String args[] ) { System.out.println("Enter ...
Discussions

Non-capturing group in java RegEx - Stack Overflow
I have written a code, but it doesn't work correctly. Here you can find my RegEx, what I have as the input and what I expect as the output. I am using a non-capturing group, because I want to read ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Java Regex Non-Capturing Group - Stack Overflow
I am having some trouble getting regex to match a string in Java. Here are the strings I want to match: String Transformation Action and Transformation Action. Basically, if String is present, I wa... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Non capturing group java regex - Stack Overflow
I saw this post What is a non-capturing group? What does a question mark followed by a colon (?:) mean? And i figured that the following would work, but it doesn't... I have a String "Game No : More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 9, 2017
java - regular expressions: quantifying a non-capturing group - Stack Overflow
See here for some background on what I'm trying to do. In short, I want to match any paths under a /path/foo/, unless the leaf directory (not the leaf file), is script. There's some answers in that More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Java Mex
javamex.com โ€บ tutorials โ€บ regular_expressions โ€บ non_capturing_groups.shtml
Java regular expressions: using non-capturing groups to organise regular expressions
This example matches either a sequence of digits, or a sequence of digits followed by one of the prefixes -st, -nd, -rd, -th (e.g. 1st, 4th). However, only the first group (the digits) is captured. The second group is non-capturing, introduced by ?: inside the brackets.
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ java-9-regular โ€บ 9781787288706 โ€บ a3908c20-1d49-48e1-916f-4fdfcad0af80.xhtml
Non-capturing groups - Java 9 Regular Expressions [Book]
July 25, 2017 - We can mark a group as a non-capturing group by adding a question mark and a colon right after the opening parenthesis.
Author ย  Anubhava Srivastava
Published ย  2017
Pages ย  158
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ java-9-regular โ€บ 9781787288706 โ€บ 0408480c-ba70-4cbe-b442-b02e624cc214.xhtml
Advantages of non-capturing groups - Java 9 Regular Expressions [Book]
July 25, 2017 - A non-capturing group lets us use the grouping inside a regular expression without changing the numbers assigned to the back references (explained in the next section).
Author ย  Anubhava Srivastava
Published ย  2017
Pages ย  158
Find elsewhere
๐ŸŒ
QBasic on Your Computer
chortle.ccsu.edu โ€บ finiteautomata โ€บ Section09 โ€บ sect09_16.html
Non-capturing Groups
matches dollar amounts like $10.43 and USD19.98 and saves the dollar amount in \1 and the cents amount in \2. Now a Java program can refer to the dollar and cents amounts as group 1 and group 2, rather than more obscure numbers. (See chapter 11 for how Java programs can use the values held in backreferences.) Another benefit of non-capturing groups is that matching is performed faster.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ essential โ€บ regex โ€บ groups.html
Capturing Groups (The Javaโ„ข Tutorials > Essential Java Classes > Regular Expressions)
There is also a special group, group 0, which always represents the entire expression. This group is not included in the total reported by groupCount. Groups beginning with (? are pure, non-capturing groups that do not capture text and do not count towards the group total.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Learn Java Programming - Regex Non-Capturing Groups Tutorial - YouTube
A non-capturing group allows you to group a pattern (token) without the regex engine automatically assigning a group number. There are many reasons for using...
Published ย  February 27, 2016
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ tutorial โ€บ java-java-regex-non-capturing-groups
Understanding Non-Capturing Groups in Java Regex - CodingTechRoom
Solution: Use `(?:...)` for grouping instead of `(...)` to reduce overhead when you don't need the captured text. Mistake: Overlooking performance in complex regex patterns. Solution: Refactor complex patterns to utilize non-capturing groups where capturing is not needed, thus enhancing performance.
๐ŸŒ
Reddit
reddit.com โ€บ r/regex โ€บ can someone explain what are non-capture groups?
r/regex on Reddit: Can someone explain what are non-capture groups?
February 8, 2021 -

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.

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 39495166 โ€บ regex-non-capturing-and-capturing-groups-and-unexplained-consumption
java - regex non-capturing and capturing groups and unexplained consumption - Stack Overflow
I can't follow why these two expressions are different: ^(\d+)(?:\.(\d+))?(?:\.(\d+))?$ applied to 1.0.3 group 1 =>1 group 2 =>0 group 3 =>3 which is expected. but if I try to generalize ...
Top answer
1 of 16
3055

Let me try to explain this with an example.

Consider the following text:

http://stackoverflow.com/
https://stackoverflow.com/questions/tagged/regex

Now, if I apply the regex below over it (I did not escape the slashes for clarity; when using it, slashes would have to be escaped to \/ )...

(https?|ftp)://([^/\r\n]+)(/[^\r\n]*)?      // slashes not escaped for clarity
(https?|ftp):\/\/([^/\r\n]+)(\/[^\r\n]*)?   // slashes escaped

... I would get the following result:

Match "http://stackoverflow.com/"
     Group 1: "http"
     Group 2: "stackoverflow.com"
     Group 3: "/"

Match "https://stackoverflow.com/questions/tagged/regex"
     Group 1: "https"
     Group 2: "stackoverflow.com"
     Group 3: "/questions/tagged/regex"

But I don't care about the protocol -- I just want the host and path of the URL. So, I change the regex to include the non-capturing group (?:).

(?:https?|ftp):\/\/([^/\r\n]+)(\/[^\r\n]*)?   // slashes escaped

Now, my result looks like this:

Match "http://stackoverflow.com/"
     Group 1: "stackoverflow.com"
     Group 2: "/"

Match "https://stackoverflow.com/questions/tagged/regex"
     Group 1: "stackoverflow.com"
     Group 2: "/questions/tagged/regex"

See? The first group has not been captured. The parser uses it to match the text, but ignores it later, in the final result.


EDIT:

As requested, let me try to explain groups too.

Well, groups serve many purposes. They can help you to extract exact information from a bigger match (which can also be named), they let you rematch a previous matched group, and can be used for substitutions. Let's try some examples, shall we?

Imagine you have some kind of XML or HTML (be aware that regex may not be the best tool for the job, but it is nice as an example). You want to parse the tags, so you could do something like this (I have added spaces to make it easier to understand):

   \<(?<TAG>.+?)\> [^<]*? \</\k<TAG>\>
or
   \<(.+?)\> [^<]*? \</\1\>

The first regex has a named group (TAG), while the second one uses a common group. Both regexes do the same thing: they use the value from the first group (the name of the tag) to match the closing tag. The difference is that the first one uses the name to match the value, and the second one uses the group index (which starts at 1).

Let's try some substitutions now. Consider the following text:

Lorem ipsum dolor sit amet consectetuer feugiat fames malesuada pretium egestas.

Now, let's use this dumb regex over it:

\b(\S)(\S)(\S)(\S*)\b

This regex matches words with at least 3 characters, and uses groups to separate the first three letters. The result is this:

Match "Lorem"
     Group 1: "L"
     Group 2: "o"
     Group 3: "r"
     Group 4: "em"
Match "ipsum"
     Group 1: "i"
     Group 2: "p"
     Group 3: "s"
     Group 4: "um"
...

Match "consectetuer"
     Group 1: "c"
     Group 2: "o"
     Group 3: "n"
     Group 4: "sectetuer"
...

So, if we apply the substitution string:

$1_$3$2_$4

... over it, we are trying to use the first group, add an underscore, use the third group, then the second group, add another underscore, and then the fourth group. The resulting string would be like the one below.

L_ro_em i_sp_um d_lo_or s_ti_ a_em_t c_no_sectetuer f_ue_giat f_ma_es m_la_esuada p_er_tium e_eg_stas.

You can use named groups for substitutions too, using ${name}.

To play around with regexes, I recommend http://regex101.com/, which offers a good amount of details on how the regex works; it also offers a few regex engines to choose from.

2 of 16
254

You can use capturing groups to organize and parse an expression. A non-capturing group has the first benefit, but doesn't have the overhead of the second. You can still say a non-capturing group is optional, for example.

Say you want to match numeric text, but some numbers could be written as 1st, 2nd, 3rd, 4th,... If you want to capture the numeric part, but not the (optional) suffix you can use a non-capturing group.

([0-9]+)(?:st|nd|rd|th)?

That will match numbers in the form 1, 2, 3... or in the form 1st, 2nd, 3rd,... but it will only capture the numeric part.