سؤال

I wanted to test this (<\/?)+[a-z]+(>) regex against different tools to see what I would get. Actually the purpose of the experiment was to test how each of the utility displayed results back to console. Basically it searches the below html document and then matches all html tags. To my surprise the result that I got from grep, ack and ag varied significantly. grep did not even return anything back

Ack

enter image description here

Grep

enter image description here

Ag

enter image description here

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Test</title>
    </head>
    <body>
        body and so
        df
        s;kl,
        <p>Hello code kit</p>
        <p>Thats pretty great</p>
        <p>Really neat!</p>
        <p>I wanna be a developer for life</p>
        <nav>
             breeee!
        </nav>
        I am in love 
        <p>Ummmmmm!</p>
        wtf
        okay buddy!
        <p>
            I took that shit out
        </p>
        <aside>
            eeem
        </aside>
        Sometimes u really get bored
    </body>
</html>

Just for sanity, I went ahead and tested it on rubular and viola! the output was just like when I used ag from the CLI. So my question is why does the result of this simple regex varies based on the tool used

enter image description here

هل كانت مفيدة؟

المحلول

You need to use an extended regular expression for grep to work in this case. Add the -E flag. I get pretty much the same output for all three tools in that case:

ack

ag

grep

نصائح أخرى

Congratulations. You've found a bug in ack. Apparently ack is colorizing the two capture groups and not the entire matching string.

I've created a bug report for it in GitHub: https://github.com/petdance/ack2/issues/304

Thanks for reporting this, although it wasn't your intent. :-)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top