regex

Regex: non-matching group

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

regex
0
Regex: Look behind

(?<=.*some_regex.*)

Similar to "look ahead", but instead checks for the presence of an expression which comes before your matching group

regex
0
Regex: Look ahead

(?=.*some_regex.*)

A look about is very useful when you'd like your actual regex to match a particular expression only if some other condition is true, without actually matching the condition. For example, given the following test string:

key1=value 1 key2=value 2

how would you extract the first key / value pair using a regex? Notice the lack of enclosing quotes which makes this exercise more difficult. One possible solution is:

  • (.*)=(.*)(?= .*=.*) This uses the look about property to extract only the key (first .*) and value (second .*), but only until another keypair appears, without actually matching the next keypair

The converse of this (instead of looking forward, look backwards) is the look behind functionality

regex
0