2

How to use a posix BRE or a posix ERE regex to match a line that doesn't contain 834 at any place in string?

I want to find grep '!<pattern>' file which will be equal to grep -v '<pattern>' file.

It is theoretical question on regex, that's why I don't want to use -v flag.

I figured out how to list lines, that doesn't contain one character: ^[^8]*$, for my case I also tried ^$|^.$|^..$|^([^8]..|.[^3].|..[^4])*$, but that's not working as expected.

  • 2
    ^([^8]*(8([^38]|3[^48])*)*)*$ – mikeserv Nov 16 '15 at 19:20
  • @mikeserv, not working either :( – Ivan Chebykin Nov 16 '15 at 20:05
  • yeah. think i stuck in one too many asterisks. the (8(...)*)* will match 8 then fall back to the [^8]* when one of the [^3]|3[^4] stuff matches. you have to something very like that - but expanded for all possibilities. regex is math - its why * 0 is a place-holder. negation is perfectly valid regex - you should not try to avoid doing it. – mikeserv Nov 16 '15 at 20:07
  • @mikeserv, Anyway, why my pattern is not working? Shouldn't it check each character from beginning of line to be not a part of 834? – Ivan Chebykin Nov 17 '15 at 16:27
2

The ^([^8]*(8([^38]|3[^4])+)*)+$ pattern should do the trick, thanks to mikeserv, who pointed it out to me.

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.