Find if either followed by non number or end of file

Viewed 38

I want to match the string b5 with optional $ in front of the b or tha 5 :

=b5
b$5
= $b$5
($b5)

But the 5 can't be followed by any number . And the b can't be preceded by any alphabet. So this should return false :

b55
ab5

I tried this :

\W\$*b\$*5\W

it works fine. i will match X=($b$5) but the problem is : it won't match anymore if the '5' is the last character in the line.

because 5 is last character

1 Answers

You can use

(?:\W|^)\$*b\$*5(?:\W|$)
(?:\W|^)\$*b\$*5\b

See the RE2 regex demo.

Details

  • (?:\W|^) - a non-capturing group matching either a non-word char or start of string
  • \$* - zero or more $ chars
  • b - a b char
  • \$* - zero or more $ chars
  • 5 - a 5 char
  • (?:\W|$) - a non-capturing group matching either a non-word char or end of string or
  • \b - a word boundary.
Related