Ruby regex matching a string surrounded by word boundary chars when the string itself ends in a word boundary

My regex: /\b+#{string}\b+/ where string = 'test-'

My test case: #test-#1

I want the regex to match test-. If my test string doesn't end in a word boundary character this works fine but if it does it breaks.

The # chars could be any other word boundary characters also, so another example would be:

-test--1 where I would still only want to match test-

Counter example case that works:

regex: /\b+#{string}\b+/ where string = 'test'

test case: #test#1

1 Answer

  1. Leo- Reply

    2019-11-14

    Since \b meaning is context-dependent, you need to use lookaround-based boundaries in your regex. As you want to match your keywords in between non-word chars, or start/end of string positions, you may use

    /(?<!\w)#{Regexp.escape(string)}(?!\w)/
    

    See a Ruby demo

    Details

    • (?<!\w) - there must not be a word char right before the search string
    • #{Regexp.escape(string)} - the search string must be escaped so that all special chars inside were treated as literal chars
    • (?!\w) - there must be no word char right after the search word.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>