echan00
echan00

Reputation: 2807

Negative lookahead regex without whitespace in between

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

Answers (4)

The fourth bird
The fourth bird

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

Regex demo

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)

Regex demo

Upvotes: 0

ALFA
ALFA

Reputation: 1744

Why your regex is wrong

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.

Solution

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)

Output

325bn ---> Match
325 bn
424bn ---> Match

sj bn bn bn bn bn
somethingbn

Demo

Solution with positive lookbehind

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

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

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

Regex Demo

Upvotes: 0

Styx
Styx

Reputation: 10076

More specifically, I only want to match the bn in front of the digits

Order matters, and it should be positive lookahead then.

(bn)(?=\d+)

Demo

Upvotes: 0

Related Questions