Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for multiple negative lookaheads

Tags:

python

regex

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?

like image 956
Louis Avatar asked Nov 07 '22 14:11

Louis


1 Answers

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

like image 136
The fourth bird Avatar answered Nov 15 '22 06:11

The fourth bird