rhughes
rhughes

Reputation: 9583

Matching Regex with # character

I am trying to match the following string:

style #

My regex is as follows:

^\s*\b(style #)\b\s*$

This is not matching my string.

If I try this regex:

^\s*\b(style n)\b\s*$

It matches the following string:

style n

This leads me to think that I am using the # character incorrectly.

What am I doing wrong?

Upvotes: 2

Views: 37

Answers (1)

ruakh
ruakh

Reputation: 183261

The problem is that \b means a word boundary (with a letter/number/underscore on exactly one side), and your string doesn't have a word boundary after the # (because it's not followed by a letter/number/underscore). Just drop that part.

^\s*\b(style #)\s*$

(And you actually don't need the first \b, either, since the context guarantees there'll be a word boundary there.)

Upvotes: 4

Related Questions