[SATLUG] character classes with *

Bruce Dubbs bruce.dubbs at gmail.com
Wed Oct 1 15:46:23 CDT 2014


MaxPower wrote:
> I'm having a bit of a hard time wrapping my head around what is
> actually going on here.  I'm attempting to learn about regular
> expressions and this use of character classes with * has me
> befuddled.

lap007:~/crap$ cat testaaLLyLayLaaya*

So what is the content of the file(s) named testaaLLyLayLaaya*?

lap007:~/crap$ grep [abcdez]* test
##why does this command include everything in the file
(even the upper case L)?aaLLyLayLaaya*


lap007:~/crap$ grep [a-z]* test
##while this command excludes everything in the file?

lap007:~/crap$
lap007:~/crap$ grep [abcde]* test
## once again total file returned aaLLyLayLaaya*


lap007:~/crap$ grep [abcdef]* test
##the addition of the letter f returns a null set? buh?


> I understand that * isn't being used in its normal way and while I
> grasp the logic behindgrep [a-z].* test, grep [a-z]\* test, and grep
> '[a-z]*' the above has me quite miffed.

First of all, the shell may be interpreting characters like [, ], and *.
Eliminate that by enclosing in single quotes:

grep '[abcdez]*' test

Secondly, the above is asking to match zero or more of the characters a, 
b, c, d, e, or z.  That probably matches anything but I really don't 
know the contents of the file you are asking about.

   -- Bruce


More information about the SATLUG mailing list