KddC
KddC

Reputation: 3013

Regex - dollar sign only matches if another character follows

I know there are lot of threads about regex and dollar signs. But the one I read didn't help at all. I have this regex \b(foo bar\$)s?\b which should match foo bar$ and foo bar$s. The thing is, the regex only matches foo bar$s.

For \b(foo bar)s?\b it works for foo bar and foo bars

The dollar is part of a name, so I can't remove it.

Any ideas?

Upvotes: 1

Views: 275

Answers (1)

John Kugelman
John Kugelman

Reputation: 361739

\b(foo bar\$)(s\b)?

\b matches word boundaries, which are defined as a word-character followed by a non-word character, or vice-versa. $ is a non-word character so $\b<space> is a failed match since the \b is surrounded by non-word characters on both sides.

The solution is to only look for the second \b if it's after an s.

Upvotes: 6

Related Questions