Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ?:, ?! and ?=

People also ask

What is ?! In regular expression?

It's a negative lookahead, which means that for the expression to match, the part within (?!...) must not match. In this case the regex matches http:// only when it is not followed by the current host name (roughly, see Thilo's comment). Follow this answer to receive notifications.

What is b regex?

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length. There are three different positions that qualify as word boundaries: Before the first character in the string, if the first character is a word character.

What is non-capturing group in regex?

Non-capturing groups are important constructs within Java Regular Expressions. They create a sub-pattern that functions as a single unit but does not save the matched character sequence. In this tutorial, we'll explore how to use non-capturing groups in Java Regular Expressions.

What is a capturing group regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .


The difference between ?= and ?! is that the former requires the given expression to match and the latter requires it to not match. For example a(?=b) will match the "a" in "ab", but not the "a" in "ac". Whereas a(?!b) will match the "a" in "ac", but not the "a" in "ab".

The difference between ?: and ?= is that ?= excludes the expression from the entire match while ?: just doesn't create a capturing group. So for example a(?:b) will match the "ab" in "abc", while a(?=b) will only match the "a" in "abc". a(b) would match the "ab" in "abc" and create a capture containing the "b".


?:  is for non capturing group
?=  is for positive look ahead
?!  is for negative look ahead
?<= is for positive look behind
?<! is for negative look behind

Please check here: http://www.regular-expressions.info/lookaround.html for very good tutorial and examples on lookahead in regular expressions.


To better understand let's apply the three expressions plus a capturing group and analyse each behaviour.

  • () capturing group - the regex inside the parenthesis must be matched and the match create a capturing group
  • (?:) non-capturing group - the regex inside the parenthesis must be matched but does not create the capturing group
  • (?=) positive lookahead - asserts that the regex must be matched
  • (?!) negative lookahead - asserts that it is impossible to match the regex

Let's apply q(u)i to quit.
q matches q and the capturing group u matches u.
The match inside the capturing group is taken and a capturing group is created.
So the engine continues with i.
And i will match i.
This last match attempt is successful.
qui is matched and a capturing group with u is created.

Let's apply q(?:u)i to quit.
Again, q matches q and the non-capturing group u matches u.
The match from the non-capturing group is taken, but the capturing group is not created.
So the engine continues with i.
And i will match i.
This last match attempt is successful.
qui is matched.

Let's apply q(?=u)i to quit.
The lookahead is positive and is followed by another token.
Again, q matches q and u matches u.
But the match from the lookahead must be discarded, so the engine steps back from i in the string to u.
Given that the lookahead was successful the engine continues with i.
But i cannot match u.
So this match attempt fails.

Let's apply q(?=u)u to quit.
The lookahead is positive and is followed by another token.
Again, q matches q and u matches u.
But the match from the lookahead must be discarded, so the engine steps back from u in the string to u.
Given that the lookahead was successful the engine continues with u.
And u will match u. So this match attempt is successful.
qu is matched.

Let's apply q(?!i)u to quit.
Even in this case lookahead is positive (because i does not match) and is followed by another token.
Again, q matches q and i doesn't match u.
The match from the lookahead must be discarded, so the engine steps back from u in the string to u.
Given that the lookahead was successful the engine continues with u.
And u will match u.
So this match attempt is successful.
qu is matched.

So, in conclusion, the real difference between lookahead and non-capturing groups is all about if you want just to test the existence or test and save the match.

But capturing groups are expensive so use it judiciously.


Try matching foobar against these:

/foo(?=b)(.*)/
/foo(?!b)(.*)/

The first regex will match and will return "bar" as first submatch — (?=b) matches the 'b', but does not consume it, leaving it for the following parentheses.

The second regex will NOT match, because it expects "foo" to be followed by something different from 'b'.

(?:...) has exactly the same effect as simple (...), but it does not return that portion as a submatch.


The simplest way to understand assertions is to treat them as the command inserted into a regular expression. When the engine runs to an assertion, it will immediately check the condition described by the assertion. If the result is true, then continue to run the regular expression.