Search code examples
regexbashgrepbrainfuck

Incorrect egrep command


The problem is the following: I have a document called brain.txt with lines of (brainfuck) code like this:

++++++++++[>++++++++>++++++>+<<<-]>+++.>+++++.<-.+.>>. word

The word at the end can really be any word. How can i filter the lines from the document where the length of every sequence of < and > is uneven?

I wrote the following command:

egrep -v '^(([^<]*(<<)*[^<]*)|([^>]*(>>)*[^>]*))*$' brain.txt

But it does not seem to work, can anyone explain me what is incorrect about this command please?

EDIT: Instead of negating the even ocurrences, i used regex to match the uneven occurences, as Karoly Horvath mentioned. So I wrote the following command:

egrep '^(([^<]*<(<<)*[^<]*)|([^>]*>(>>)*[^>]*))*$' brain.txt

But for some reason i still match even lines.


Solution

  • Your regex matches every line where the length of every sequence of < and > is even.

    If you negate it, you will print the rest of the lines - the ones which contain at least one uneven sequence.

    The other problem is [^<] can eat >.

    Don't negate, just search for uneven:

    grep -P '^(([^<>]*)|((<<)*<(?!<))|((>>)*>(?!>)))*$'