Regular Expressions and Commands HowTo
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" |
|
$ |
End of string |
ls |grep -oE "w+(.jpg$)" |
|
b |
Word boundary |
ls |grep -oE "bChunkab.jpg" |
|
< |
Start of word |
ls |grep -oE "<Chw+.jpg" |
|
> |
End of word |
ls Scripts/Python/ |grep -oE "w+>.py" |
|
Character Classes |
What it means |
Example |
|---|---|---|
|
w |
Word |
ls | grep -oE "w" |
|
W |
Non word |
ls |grep -oE "W" |
|
Quantifiers |
what it means |
example |
|---|---|---|
|
* |
zero or more times |
ls | grep -oE "w*.jpg" |
|
+ |
one or more times |
ls | grep -oE "w+" |
|
? |
matches either once or zero times |
ls | grep -oE "Chunka1?.jpg" |
|
{2} |
Exactly 2 times |
ls | grep -oE "(^C{2}hw+.jpg)" |
|
{1,} |
1 or more times |
ls | grep -oE "(^C{1,}hw+.jpg)" |
|
{1,2} |
1 through 2 times |
ls | grep -oE "(^C{1,2}hw+.jpg)" |
|
Special Characters |
What it means |
Example |
|---|---|---|
|
n |
New line |
|
|
r |
Carriage return |
|
|
t |
Tab |
|
|
v
|
Vertical tab |
|
|
f |
Form feed |
|