Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get extra, unexpected results with my ack regex?

Tags:

regex

perl

ack

I'm finally learning regexps and training with ack. I believe this uses Perl regexp.

I want to match all lines where the first non-blank characters are if (<word> !, with any number of spaces in between the elements.

This is what I came up with:

^[ \t]*if *\(\w+ *!

It only nearly worked. ^[ \t]* is wrong, since it matches one or none [space or tab]. What I want is to match anything that may contain only space or tab (or nothing).

For example these should not match:

// if (asdf != 0)
else if (asdf != 1)

How can I modify my regexp for that?


EDIT adding command line

ack -i --group -a '^\s*if *\(\w+ *!' c:/work/proj/proj 

Note the single quotes, I'm not so sure about them anymore.

My search base is a larger code base. It does include matching expressions (quite some), but even for example:

274:                }else if (y != 0) 

, which I get as a result of the above command.


EDIT adding the result of mobrule's test

Mobrule, thanks for providing me a text to test on. I'll copy here what I get on my prompt:

C:\Temp\regex>more ack.test
# ack.test
if (asdf != 0)    # no spaces - ok
 if (asdf != 0)   # single space - ok
    if (asdf != 0) # single tab - ok
   if (asdf != 0) # multiple space - ok
        if (asdf != 0) # multiple tab - ok
    if (asdf != 0) # spaces + tab ok
     if (asdf != 0) # tab + space ok
     if (asdf != 0) # space + tab + space ok
// if (asdf != 0)  # not ok
} else if (asdf != 0) # not ok

C:\Temp\regex>ack '^[ \t]*if *\(\w+ *!' ack.test

C:\Temp\regex>"C:\Program\git\bin\perl.exe" C:\bat\ack.pl '[ \t]*if *\(\w+ *!' a
ck.test
if (asdf != 0)    # no spaces - ok
 if (asdf != 0)   # single space - ok
    if (asdf != 0) # single tab - ok
   if (asdf != 0) # multiple space - ok
        if (asdf != 0) # multiple tab - ok
    if (asdf != 0) # spaces + tab ok
     if (asdf != 0) # tab + space ok
     if (asdf != 0) # space + tab + space ok
// if (asdf != 0)  # not ok
} else if (asdf != 0) # not ok

The problem is in my call to my ack.bat!

ack.bat contains:

"C:\Program\git\bin\perl.exe" C:\bat\ack.pl %*

Although I call with a caret, it gets away at the call of the bat file!

Escaping the caret with ^^ does not work.

Quoting the regex with " " instead of ' ' works. My problem was a DOS/win problem, sorry for bothering you all for that.

like image 450
Gauthier Avatar asked Dec 22 '22 04:12

Gauthier


2 Answers

^\s*if\s*\(\S+\s*!
  • Use \S for non-white-space. \w will not match any special chars, so if ($word will not match. May be that's OK with your specs, in which case \w (alphanumeric plus "_" ) is OK
$ perl5.8 -e '{$s="else if (asdf \!= 1)"; if ($s =~ /^\s*if\s*\((\S+)\s*\!/) { print "|$1|\n";} else { print "NO MATCH\n";}}'
NO MATCH
$ perl5.8 -e '{$s="// if (asdf \!= 0)"; if ($s =~ /^\s*if\s*\((\S+)\s*\!/) { print "|$1|\n";} else { print "NO MATCH\n";}}'
NO MATCH
$ perl5.8 -e '{$s=" if (asdf \!= 0)"; if ($s =~ /^\s*if\s*\((\S+)\s*\!/) { print "|$1|\n";} else { print "NO MATCH\n";}}'  
|asdf|
$ perl5.8 -e '{$s="if (asdf \!= 0)"; if ($s =~ /^\s*if\s*\((\S+)\s*\!/) { print "|$1|\n";} else { print "NO MATCH\n";}}' 
|asdf|
$ perl5.8 -e '{$s="if (\$asdf \!= 0)"; if ($s =~ /^\s*if\s*\((\S+)\s*\!/) { print "|$1|\n";} else { print "NO MATCH\n";}}'
|$asdf|
like image 193
DVK Avatar answered Jan 01 '23 07:01

DVK


In both ack and grep, * matches zero or more, not zero or one. So I think you already have the right solution. What test cases aren't giving you the results you want?

# ack.test
if (asdf != 0)    # no spaces - ok
 if (asdf != 0)   # single space - ok
    if (asdf != 0) # single tab - ok
   if (asdf != 0) # multiple space - ok
        if (asdf != 0) # multiple tab - ok
    if (asdf != 0) # spaces + tab ok
     if (asdf != 0) # tab + space ok
     if (asdf != 0) # space + tab + space ok
// if (asdf != 0)  # not ok
} else if (asdf != 0) # not ok

Results:

$ ack '^[ \t]*if *\(\w+ *!' ack.test
if (asdf != 0)    # no spaces - ok
 if (asdf != 0)   # single space - ok
        if (asdf != 0) # single tab - ok
   if (asdf != 0) # multiple space - ok
                if (asdf != 0) # multiple tab - ok
        if (asdf != 0) # spaces + tab ok
         if (asdf != 0) # tab + space ok
         if (asdf != 0) # space + tab + space ok

$ ack -v '^[ \t]*if *\(\w+ *!' ack.test
// if (asdf != 0)  # not ok
} else if (asdf != 0) # not ok
like image 42
mob Avatar answered Jan 01 '23 07:01

mob