dvas0004 / NerdNotes

A collection of notes: things I'd like to remember while reading technical articles, technical questions I couldn't answer, and so on.
12 stars 0 forks source link

Regex: non-matching group #41

Open dvas0004 opened 5 years ago

dvas0004 commented 5 years ago

When grouping expressions together we need to use brackets. For example:

(abc|123)

will match "abc" or "123". However, what if we would not like to include these in our matched groups? The answer is to use a non-matching group:

(?:abc|123)

note the use of:

(?:.*some_regex.*)

Example of the difference:

image

Without using a non-matching group we end up with 2 matching groups:

image

If this is not ideal and instead we use a non-matching group:

image

Then number of matching groups is reduced to only what we're interested in:

image

Note how we have only one matching group per line in the above screenshot compared to the two groups we had before