Sunday, 1 January 2017

Shell Script 13 Regular Expressions - User Guide (a)

Metacharacter

Meaning


^The ^ (circumflex or caret) inside square brackets negates the expression (we will see an alternate use for the circumflex/caret outside square brackets later), for example, [^Ff] means anything except upper or lower case F and [^a-z] means everything except lower case a to z.
Notes:
  1. There are no spaces between the range delimiter values, if there was, depending on the range, it would be added to the possible range or rejected as invalid. Be very careful with spaces.
  2. Some regular expression systems, notably VBScript, provide a negation operator (!) for use with strings. This is a non-standard feature and therefore the resulting expressions are not portable.
  3. Because of the dual nature of the caret (or circumflex) you will frequency see expressions like [^"], [^<] or [^,] which are typically used as separator triggers (when combined with iterations) for more more complex searches or when parsing, say, HTML or comma delimited text.
e.g.

jtony@genoa:~/learn/shell/UserGuide$ cat string1.txt
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)
jtony@genoa:~/learn/shell/UserGuide$ cat string2.txt
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)

jtony@genoa:~/learn/shell/UserGuide$ grep -rn  '[^A-M]in'  string1.txt
1:Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)

jtony@genoa:~/learn/shell/UserGuide$ grep -rn  '[A-Za-z]in'  string1.txt
1:Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
2:Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)








This series refer the online article:
http://www.zytrax.com/tech/web/regex.htm#search

No comments:

Post a Comment