Regular Expression

Previously we have an example on regular expression, but It doesn’t shows the power of square brackets ( [ ] )

Let say you want to search for string fprintf, vprintf and sprintf using grep, usually what you do is

egrep "fprintf|vprintf|sprintf" *.c

You may be ask why don’t just uses the word “printf”? If uses the word printf, it will return all of them but also include printf itself. But in this case i don’t want to grep other printf besides f,v,s printf. Thats the square brackets comes in to lessen your trouble.

egrep "[sfv]printf" *.c

It simply return the result with any character specified in [ ] with word printf concatenated.

The square brackets can be used with other RE symbols, here is another example, let say I want to gets all lists with words start with a character “a to f”, I can do this

egrep "^[a-f]" com-book.txt

It is case sensitive, I want all a to f including the upper case A to F.

egrep "^[a-fA-F]" com-book.txt


Leave a Comment