Louis
Louis

Reputation: 63

Syntax for multiple negative lookaheads

I want to extract texts that are between /nr AND an opening square bracket or comma plus space, like this

[A/nrf, B/cc, C/nrf, (/w, D/nr, )/w, ,/w, E/p, F/rr, G/ude1]

I want both A and D. I have tried (?!,\s)(?!\[)([^,]+)/nr(?=,) but it only matches D. Could anyone help please?

Upvotes: 5

Views: 79

Answers (1)

The fourth bird
The fourth bird

Reputation: 163362

You want to match A and D, but according to the logic in the comment extract texts that are between /nr AND an opening square bracket or comma plus space this will give you A, C and D.

(?:\[|, )([^,]+)/nr

You could use a capturing group to capture what you want and match what you want in front and after the group.

Explanation

  • (?:\[|, ) Non capturing group, match either [ or a comma and a space
  • ([^,]+) Capture in group 1 matching 1+ times not a comma
  • /nr Match /nr

Regex demo

Upvotes: 1

Related Questions