Regular Expression matches on possessive version of word

I am writing some code to add html tags to certain phrases. I am trying to find exact matches and replace them with the phrase with tags around them. I am very new to regular expressions and apologize in advance if this is something that should be obvious to me.

For example, if I wanted to bold “black cat” in the following sentence:

I am setting my pattern as \bblack cat\b

The black cat sits on the window sill.
would become
The black cat sits on the window sill.

However, it also changes this:
The black cat’s collar is blue.
to this
The black cat’s collar is blue.

It doesn’t recognize that the 's is part of the word and matches.

Thanks in advance for any help

You’re only matching the words black cat with that expression. It’s working perfectly fine.

I’d suggest changing your expression to

black cat('s)?

The above matches

black cat
black cat's

However, it only matches for those exact strings. If you wanted to make it work even when there’s slight human error, such as more than one space between black and cat, or a space between cat and ' or s, you could do this:


which matches:

black cat
black     cat
black cat's
black   cat's
black cat 's
black cat ' s

Though you might not need it.

1 Like

This might give you a clue…