Newest Downloads

Mar.08

  [zenoss@zenoss2 ~]$ python Zenoss_Template_Manager.py -husage: Examples: python Zenoss_Templa...

Feb.01

python queryns.py -husage: queryns.py [options] arg --username=username --password=password --net...

Regular Expressions and Commands HowTo
Linux HowTo's - Advance Linux HowTo's
Written by Allen Sanabria   
Sunday, 18 May 2008 16:33

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 "^C\w+\.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 "\bChunka\b\.jpg"
Chunka.jpg

\< 

Start of word 

ls |grep -oE "\<Ch\w+\.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

\W 

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}h\w+.jpg)"
CChunka.jpg

{1,}

1 or more times

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

{1,2} 

1 through 2 times

ls | grep -oE "(^C{1,2}h\w+.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 

 

 

  




Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
Comments
Search RSS
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated on Saturday, 24 May 2008 18:58