Search code examples
regexregex-lookaroundsword-boundary

Regex allowing word boundaries with lookahead and "."


I have the strings:

lorem ipsum Outer.Middle.Last dolor #1
lorem ipsum Outer.Middle dolor      #2

Currently I am using the regex [[:<:]]Outer.Middle[[:>:]] to try and match only string #2. However, this matches both strings #1 and #2 due to the naive boundaries.

I have attempted to do some sort of lookahead e.g. [[:<:]]Outer.Middle(?!.)[[:>:]], but this is abysmally wrong.

The regex is necessary as I am using PostgreSQL and querying class names contained within text bodies.

Any solution or guidance to a solution would be highly appreciated.


Solution

  • This worked for me:

    select * from my_table where my_col ~ '[[:<:]]Outer\\.Middle[[:>:]](?!\\.)'
    

    The . characters should be escaped (otherwise they are treated as a wildcard matching any one character), and backslashes need escaping too.

    This is also equivalent to:

    select * from my_table where my_col ~ '[[:<:]]Outer[.]Middle[[:>:]](?![.])'