I need to extract with -o1 a group that is matched in lines that do not contain a string.

If I use -v pcregrep does not match any groups - which is reasonable: I'm asking to group-match something in patterns that I'm excluding.

How can I deal with that?

Thanks in advance for any help.


Assuming I've understood your need correctly, if your match command takes the form pcregrep -o1 '(match)', the corresponding regex that would report a match only if another string is not matched is pcregrep -o1 '^(?!.*donotmatch).*(match)'. Look up negative lookahead expressions in the PCRE documentation. Also notice the .* in front of the (match); this is needed due the anchoring of the negative looking ahead expression to the start of the line via the ^

  • Thanks @iruvar, would it be the same regex as (?<!.*donotmatch).*(match).*(?!.*donotmatch) ? – Anton Maria Prati Jun 22 at 23:19
  • @AntonMariaPrati, non-fixed length lookbehind assertions don't work. (?<!.*donotmatch) is not valid regex – iruvar Jun 22 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.