Search code examples
regexnlpregex-lookarounds

using lookahead and lookbehind with or ( | )


I am trying to match some special characters that are not preceded and followed by a white space.

enter image description here

Normally the hyphen before the ww and the hyphen after the ww should match.

This is the following regex I wrote: (?<!\s)([.([]!):,+"\/-])(?!\s)

I tried this too:

enter image description here

((?<!\s)|(?!\s))([.([]!):,+"\/-])

But the hyphen with the white space before and after also matches. (this should not be the case)

Can you tell me what I'm doing wrong? Thank you for your help !


Solution

  • The (?<!\s)([.([\]!):,+"\\/-])(?!\s) regex will fail the special char match when it is either not preceded with a whitespace or when it is followed with a whitespace.

    The ((?<!\s)|(?!\s))([.([\]!):,+"\\/-]) regex matches a special char when not preceded with a whitespace, or requires the special char (matched with [.([\]!):,+"\\/-]) to be not a whitespace char (the (?!\s) before a consuming pattern restricts this pattern).

    You need to make sure the match is failed only in case it is enclosed with whitespace chars on both ends:

    [.([\]!):,+"\\/-](?!(?<=\s.)\s)
    (?<!\s)[.([\]!):,+"\\/-]|[.([\]!):,+"\\/-](?!\s)
    

    See the regex demo #1 and regex demo #2

    Note the [.([\]!):,+"\\/-](?!(?<=\s.)\s) matches a special char (with [.([\]!):,+"\\/-]) that is not followed with a whitespace that is immediately preceded with a whitespace and any one char (actually, . is used to simply match the special char here).

    The (?<!\s)[.([\]!):,+"\\/-]|[.([\]!):,+"\\/-](?!\s) pattern matches a special char not immediately preceded with a whitespace or will match a special char that is not followed with a whitespace.