Tuesday, May 8, 2012

Metacharacter in Linux Regular Expression

Note : * and ? in grep means repetition. It does not behave as a wildcard in regular expression syntax (as it is in UNIX or DOS patterns). The / ?  +  are part of regex metacharacter.

Metacharacter

Meaning

?0 or 1
o? matches '' or 'o' but not 'oo'
bo?k matches : bk, bok but doesn't match : book,booooook
*0 or more
a* matches '' , 'a' , 'aa' , 'aaa'
ga*z matches : gz, gaz, gaaz, gaaaz but doesn't match : goz,giz
+1 or more
u+ matches 'u', 'uuu' but not ''
mu+te matches : mute,muute but not : mte,mate
{n}Matches the preceding character, or character range, n times exactly, for example, to find a local phone number we could use [0-9]{3}-[0-9]{4} which would find any number of the form 123-4567.
Note: The - (dash) in this case, because it is outside the square brackets, is a literal*. Value is enclosed in braces (curly brackets).
{n,m}Matches the preceding character at least n times but not more than m times, for example, 'ba{2,3}b' will find 'baab' and 'baaab' but NOT 'bab' or 'baaaab'. Values are enclosed in braces (curly brackets).
*literal means normal character (not metacharacter or escape or any other special character) , so it will be interpreted as is.

source : http://www.zytrax.com/tech/web/regex.htm#more-notes

No comments:

Post a Comment