Reputation: 2807
The regex below matches all three examples below.
(?!\d+)(bn)
325bn
325 bn
424bn
I only want to match
325bn
424bn
More specifically, I only want to match the bn
in front of the digits
Upvotes: 1
Views: 1023
Reputation: 163277
Your pattern (?!\d+)(bn)
reads as assert what is directly on the right is not 1+ digits. Which will be true because it is followed by bn
You could use a capturing group with a word boundary instead of a positive lookahead:
\b\d+(bn)\b
If there should be preceded by either the start of the string or a whitespace char (?:\s|^)
and it can only be followed by not a non whitespace char (?!\S)
, you could use:
(?:\s|^)\d+(bn)(?!\S)
Upvotes: 0
Reputation: 1744
Your regex will match any bn
no matter what. Since the lookahead is placed before the (bn)
it will always evaluates to true
(Thanks to @WiktorStribiżew).
However even in the right position, (bn)(?!\d+)
will match any bn
that is not followed by digits (positive/negative lookarounds
).
So all of these will be matched:
sj bn bn bn bn bn
somethingbn
This is not what you want.
As far as I understand you only need to match bn
that is placed after some digits. Why don't you just go with:
\d+(bn)
325bn ---> Match
325 bn
424bn ---> Match
sj bn bn bn bn bn
somethingbn
As @revo mentioned, it might be helpful to have a solution using lookaround to have an idea on how it works.
(?<=\d)bn
This is a positive lookbehind
. This tells the regex engine to temporarily step backwards in the string, to check if the text inside the lookbehind can be matched there.
Upvotes: 3
Reputation: 18357
With your sample data and expected matches, it seems you indeed wanted to use positive look behind but rather you ended up using negative look ahead incorrectly. And also, quantified \d+
look behinds are rarely supported (supported in .NET).
As you wrote, you more specifically wanted to only match the bn
preceded by digit, you can change your regex to this,
(?<=\d)bn\b
This ensures a bn
is matched if it is preceded by a digit and \b
ensures bn
doesn't match in larger word which starts with bn
Upvotes: 0