in reply to Re^5: Regex help \b & \Q
in thread Regex help \b & \Q

(?! [^\s,;] )
Sometimes double negation is difficult to understand, so someone would like to read:
(?= [\s,;] | \z)

Replies are listed 'Best First'.
Re^7: Regex help \b & \Q
by AnomalousMonk (Archbishop) on Apr 14, 2016 at 20:51 UTC
    Sometimes double negation is difficult to understand ...

    More than just sometimes, IMHO, but it's tolerable if taken in moderation. E.g., if you need a "digit boundary" assertion analogous to  \b in that it also matches at the start/end of a string, then  (?<! \d) and  (?! \d) are very attractive. Then  (?<! \d) \d{4} (?! \d) matches  '1234' 'x1234' '1234x' 'x1234x' but none of  '12345' 'x12345x' etc. Extend this to  (?<! \D) and  (?! \D) and you have a sometimes-useful double-negation asserting "non-digit boundary".


    Give a man a fish:  <%-{-{-{-<

      Wow!
      But it seems that (?<! \D) and (?! \D) is not "equivalent" to \B, because \B don't match beginning or ending of string:
      # I've changed all x-es to spaces (for comparison). for my $line ('1234', ' 1234', '1234 ', ' 1234 ', '12345', ' 12345 ',, ' 123456 '){ print map { sprintf "%10s: $_\n", "'$line'" } join ' ', map { $line =~ qr/$_/x ? 'OK' : 'NO' } '(?<! \d) \d{4} (?! \d)', '(?<! \D) \d{4} (?! \D)', '\b \d{4} \b ', '\B \d{4} \B ', } __END__ '1234': OK OK OK NO ' 1234': OK NO OK NO '1234 ': OK NO OK NO ' 1234 ': OK NO OK NO '12345': NO OK NO NO ' 12345 ': NO NO NO NO ' 123456 ': NO OK NO OK
        ... (?<! \D) and (?! \D) is not "equivalent" to \B ...

        That's why they're only sometimes useful :)


        Give a man a fish:  <%-{-{-{-<