One minute
Regex Non-Capturing Group
Suppose you have the following text:
foobar
The following regular expression will match it (let’s assume that for some reason you want to use groups):
(foo)(bar)
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:
(foo)(?:bar)?
Read other posts
comments powered by Disqus