0

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.

1 Answer 1

0

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 ^

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.