Christopher
Christopher

Reputation: 3469

Regex - ignore when exists a certain prefix

I want to match any number after the letter v.

So,

But i want to ignore any match when there's any letter behind v.

Like xv: 123 must not match anything.

I'm working with this pattern:

/v[\s\pP]*(\d+)/

but it doesn't ignore situations when somethingbehindv: 123.

Upvotes: 0

Views: 375

Answers (2)

mwp
mwp

Reputation: 8467

An alternative solution would be to use a negative look-behind assertion. This is an extended regular expression and may not supported in whatever language or platform you're using. It would look something like this:

/(?<!\S)v[\s\pP]*(\d+)/

Which basically prevents the pattern from matching if any non-whitespace character(s) (\S) precede the v. You can tweak this, of course, if other characters should be disallowed.

Upvotes: 1

hwnd
hwnd

Reputation: 70722

Use a word boundary \b to fix your problem:

/\bv[\s\pP]*(\d+)/

Upvotes: 0

Related Questions