May 182008
 

There are a bunch of great regular expressions HowTo’s out there….. Now you may be asking why am I creating another??? Well quite simple, I am creating this one because this is not really just a regular expression howto but more a regular expression HowTo with its everyday uses with linux/unix commands.  So it will not cover perl/python/ruby/…etc programming languages regular expressions (That will be for another tutorial :) )

To start this off I will give you a basic introduction to regular expressions using the output
of "ls" and piping it "grep" using the -oE options (The o stands for Exact match and the E stands for Extended Regular Expressions). Using "(BRE and ERE) Basic Regular Expressions and Posix Extended Regular Expressions"…

THIS IS NOT A TUTORIAL BUT A HOWTO!! So this means more examples and less explaining..

 

Anchors

What it means 

Example 

^

Start of string

ls |grep -oE "^Cw+.jpg"
Chunka.jpg
Chunka1.jpg

$ 

End of string 

ls |grep -oE "w+(.jpg$)"
Chunka.jpg
Chunka1.jpg
DSCF0732.jpg

b 

Word boundary 

ls |grep -oE "bChunkab.jpg"
Chunka.jpg

< 

Start of word 

ls |grep -oE "<Chw+.jpg"
Chunka.jpg
Chunka1.jpg

> 

End of word 

ls Scripts/Python/ |grep -oE "w+>.py"
xen_mac_generate.py 

 Character Classes 

What it means 

Example 

w 

Word 

ls | grep -oE "w"
C
C

Non word 

ls |grep -oE "W"
.
.

Quantifiers 

what it means 

example 

* 

zero or more times

ls | grep -oE "w*.jpg"
Chunka.jpg
Chunka1.jpg
DSCF0732.jpg

+ 

one or more times 

ls | grep -oE "w+"
Chunka
Chunka1
DSCF0732

? 

matches either once or zero times

ls | grep -oE "Chunka1?.jpg"
Chunka.jpg
Chunka1.jpg

{2} 

Exactly 2 times

ls | grep -oE "(^C{2}hw+.jpg)"
CChunka.jpg

{1,}

1 or more times

ls | grep -oE "(^C{1,}hw+.jpg)"
CChunka.jpg
CCChunka.jpg
Chunka.jpg
Chunka1.jpg
Chunka11.jpg

{1,2} 

1 through 2 times

ls | grep -oE "(^C{1,2}hw+.jpg)"
CChunka.jpg
Chunka.jpg
Chunka1.jpg
Chunka11.jpg

 

Special Characters 

What it means 

Example 

n 

New line 

 

r 

Carriage return 

 

t

Tab 

 

v

Vertical tab 

 

f

Form feed 

 

 

  

dynasty


Warning: fopen(/home/dynasty/linuxdynasty.org/wp-content/plugins/wp-google-plus-one/lib/standard.txt) [function.fopen]: failed to open stream: No such file or directory in /home/dynasty/linuxdynasty.org/wp-content/plugins/wp-google-plus-one/plusone.php on line 104

Warning: fread(): supplied argument is not a valid stream resource in /home/dynasty/linuxdynasty.org/wp-content/plugins/wp-google-plus-one/plusone.php on line 105

Warning: fclose(): supplied argument is not a valid stream resource in /home/dynasty/linuxdynasty.org/wp-content/plugins/wp-google-plus-one/plusone.php on line 106
.