Stephan
Stephan

Reputation: 581

regex negative lookahead with named capture group

I'm having trouble applying a negative lookahead to a named capture group.

I have the below regex and I want to get some results filtered out.

/^\[.*?(?P<parts>\d{1,3}\/\d{1,3}) \] \- "(?P<name>.*?)\.(vol|rar|par|sfv|nfo|nzb)/i

I want to use a negative lookahead to drop text like S02E02 so I made a negative lookahead like (?!S\d{1,2}), but I'm unable to get it to work within the capture group 'name'

I want the regex to match results like

[ TrollHD ] - [ 002/124 ] - "2015 Dream Concert 1080p Netflix WEBRip DD+ 2.0 x264-TrollHD.part001.rar" yEnc (1/164)

but skip results like

[ TrollHD ] - [ 04/30 ] - "Chelsea S02E05 1080p Netflix WEBRip DD+ 2.0 x264-TrollHD.part03.rar" yEnc (1/164)

Upvotes: 3

Views: 1978

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627219

It seems you want

/^\[.*?(?P<parts>\d{1,3}\/\d{1,3}) ] - "(?!.*\sS\d{1,2}(?:E\d{1,3})?\s)(?P<name>.*?)\.(vol|rar|par|sfv|nfo|nzb)/i

See the regex demo

Details

  • ^ - start of string
  • \[ - a [ char
  • .*? - any 0+ chars other than line break chars as few as possible
  • (?P<parts>\d{1,3}\/\d{1,3}) - A parts group, capturing 1 to 3 digits, /, 1 to 3 digits
  • ] - " - a ] - " - substring
  • (?!.*\sS\d{1,2}(?:E\d{1,3})?\s) - a negative lookahead that fails the match if, immediately to the right of the current location, there are 0+ chars other than line break chars as many as possible followed with a whitespace, S, 1 or 2 digits, then - optionally - with E and 1 to 3 digits and then a whitesdace ((?:E\d{1,3})? is a non-capturing group that is used to match E and \d{1,3} as a sequence of patterns, and the ? quantifier makes it match 1 or 0 times)
  • (?P<name>.*?) - Group name capturing any 0+ chars other than line break chars as few as possible
  • \. - a . char
  • (vol|rar|par|sfv|nfo|nzb) - any of the values in the alternation group.

Upvotes: 1

Related Questions