How does this regular expression work when searching for the last occurrence of a word?

I came across a regex as follows:

foo(?!.*foo) 

if loaded with foo bar bar foo , it will find the last occurrence of foo. I know that it uses a mechanism called a negative lookahead, which means that it will correspond to a word that does not end with characters after?!. But how does regex work?

+7
regex
source share
3 answers

It matches foo only if it is not respected ( ?! ) By any text ( .* ) Containing foo in it.

+4
source share

A slightly different answer from sshashank (because the word containing in its answer does not work for me, and in regular expression you should be pedantic - it's all about accuracy.) I am 100% sure that sshashank knows this and only formulated it this way for brevity.

The regular expression matches foo , is not executed (i.e. a negative expression (?! ) As follows:

{{{any number of any characters (ie .* ), then the characters foo }}}

If the browsing failure fails, then the part corresponding to .* Does not contain foo . foo will appear later.

See automatic translation :

 NODE EXPLANATION -------------------------------------------------------------------------------- foo 'foo' -------------------------------------------------------------------------------- (?! look ahead to see if there is not: -------------------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- foo 'foo' -------------------------------------------------------------------------------- ) end of look-ahead 

The same thing in different words from regex101:

/ Foo (?!. * Foo) /

 foo matches the characters foo literally (case sensitive) (?!.*foo) Negative Lookahead - Assert that it is impossible to match the regex below .* matches any character (except newline) Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy] foo matches the characters foo literally (case sensitive) 

What has RegexBuddy to say?

Foo (?!. * Foo)

 foo(?!.*foo) 
  • Matches the character string "foo" literally (case sensitive) foo
  • Affirm that it is not possible to combine the regex below starting at this position (negative view) (?!.*foo)
    • Matches any character that is not a line break character (line, carriage return, next line, line separator, paragraph separator) .*
      • Between zero and unlimited time intervals, as many times as possible, returning as necessary (greedy) *
    • Matches the character string "foo" literally (case sensitive) foo
+9
source share

A negative look is necessary if you want to juxtapose something that is not followed by something else.

Brief explanation:

 foo(?!.*foo) matches foo when not followed by any character except \n and `foo` 

For example, let's say you have the following two lines.

 foobar barfoo 

And regex:

 foo(?!bar) 

This matches foo if you don't follow bar to match the barfoo line here.

+4
source share

All Articles