2

I found a regex not to match a word from this question Regular expression to match a line that doesn't contain a word? .

u(?!i) will match a word with u not followed by i. The answer says that it checks if each character is not followed by hede. So shouldn't the regex be /^(.(?!hede))*$/. It works for words containing hede but not starting with it. Why do they use /^((?!hede).)*$/ . Whats the difference between the position of the dot. I used re=debug but couldn't still get the meaning

Community
  • 1
  • 1
xtreak
  • 1,376
  • 18
  • 42
  • 2
    Re "shouldn't the regex be `/^(.(?!hede))*$/`? That will match `hede` since no character is followed by `hede`. You need to start checking at position 0, not position 1. – ikegami Oct 09 '14 at 07:03

2 Answers2

3

(?!hede) means look ahead to see if there is not: hede. So it make sense if there is any other character followed by hede hence . after (?!hede) is the valid regex.

Regex explanation of ^((?!hede).)*$

  ^                        the beginning of the string
  (                        group and capture to \1 (0 or more times):
    (?!                      look ahead to see if there is not:
      hede                     'hede'
    )                        end of look-ahead
    .                        any character except \n
  )*                       end of \1
  $                        before an optional \n, and the end of the string

Use (?<! for look behind

Pattern explanation of ^(.(?<!hede))*$

  ^                        the beginning of the string
  (                        group and capture to \1 (0 or more times):
    .                        any character except \n
    (?<!                     look behind to see if there is not:
      hede                     'hede'
    )                        end of look-behind
  )*                       end of \1
  $                        before an optional \n, and the end of the string

It's better explained under Lookahead and Lookbehind Zero-Length Assertions

They do not consume characters in the string, but only assert whether a match is possible or not

Braj
  • 46,415
  • 5
  • 60
  • 76
1
`^((?!hede).)*$` means check for condtion ,then consume.

Due to anchors it will not accept any string containing hede.

See demo:

http://regex101.com/r/iM2wF9/17

`^(.(?!hede))*$` means consume,then check for condition.

(that is why words starting from hede were passing because first character was h and it had no hede ahead of it. So it was a pass).

halfer
  • 19,824
  • 17
  • 99
  • 186
vks
  • 67,027
  • 10
  • 91
  • 124