Can I use an OR in regex without capturing what's enclosed?

RegexCaptureRegex Group

Regex Problem Overview


I'm using rubular.com to build my regex, and their documentation describes the following:

(...)	Capture everything enclosed
(a|b)	a or b

How can I use an OR expression without capturing what's in it? For example, say I want to capture either "ac" or "bc". I can't use the regex

(a|b)(c)

right? Since then I capture either "a" or "b" in one group and "c" in another, not the same. I know I can filter through the captured results, but that seems like more work...

Am I missing something obvious? I'm using this in Java, if that is pertinent.

Regex Solutions


Solution 1 - Regex

Depending on the regular expression implementation you can use so called non-capturing groups with the syntax (?:…):

((?:a|b)c)

Here (?:a|b) is a group but you cannot reference its match. So you can only reference the match of ((?:a|b)c) that is either ac or bc.

Solution 2 - Regex

If your implementation has it, then you can use non-capturing parentheses:

(?:a|b)

Solution 3 - Regex

If your OR alternatives are all single characters - you can just use "character set" operator:

([ab]c)

it will only match ac or bc and it's more readable.

Solution 4 - Regex

Even rubular doesn't make you use parentheses and the precedence of | is low. For example a|bc does not match ccc

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongoggin13View Question on Stackoverflow
Solution 1 - RegexGumboView Answer on Stackoverflow
Solution 2 - RegexMarc Mutz - mmutzView Answer on Stackoverflow
Solution 3 - RegexyrtimiDView Answer on Stackoverflow
Solution 4 - RegexmswView Answer on Stackoverflow