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

Viewed 125595

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.

4 Answers

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.

Related