Suppose you have the following text:
The following regular expression will match it (let’s assume that for some reason you want to use groups):
Doing so will yield the following results:
> Group 1: 'foo' > Group 2: 'bar'
But you are not interested in capturing the second group as you do not need it for anything later - you only want to match on it. You can exclude it from the results by applying a non-capturing group:
(foo)(?:bar) > Group 1: 'foo'
If you want to make the entire non-capturing group optional, you can do it like so: