oyan11
oyan11

Reputation: 373

Exclude a certain match in a capturing group in regex

I have a regex capturing group and I want to exclude a number if it matches a certain pattern also.

This is my capturing group:

https://regex101.com/r/zL1tL8/1

if \n is followed by a number and character like "1st", "2nd", "4dffgsd", "3sf" then it should stop the match BEFORE the number.

0-9 is important in the capturing group.

So far I have this pattern [0-9][a-zA-Z]+ to match a number followed by characters. How do I apply this to the capturing group as a condition?

Update:

https://regex101.com/r/zL1tL8/4

Line 1 is wrong.

It should not match a number followed by characters

Upvotes: 2

Views: 18702

Answers (1)

Jake Bathman
Jake Bathman

Reputation: 1278

You'll want to use a negative lookahead to "stop" the match if something after matches your pattern. So, something like this might work:

(\\n(?![0-9][a-zA-Z]))

See it in use here: https://regex101.com/r/zL1tL8/2

Here's a page with some more info on lookahead and lookbehind: http://www.rexegg.com/regex-lookarounds.html

Upvotes: 4

Related Questions