Monday, 2 January 2017

Shell Script 14 Regular Expressions - User Guide (b)

Metacharacter

Meaning

?The ? (question mark) matches when the preceding character occurs 0 or 1 times only, for example, colou?r will find both color (u is found 0 times) and colour (u is found 1 time).
*The * (asterisk or star) matches when the preceding character occurs 0 or more times, for example, tre* will find tree (e is found 2 times) and tread (e is found 1 time) and trough (e is found 0 times and thus returns a match only on the tr).
+The + (plus) matches when the preceding character occurs 1 or more times, for example, tre+ will find tree (e is found 2 times) and tread (e is found 1 time) but NOT trough (0 times).
{n}Matches when the preceding character, or character range, occurs 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. Value is enclosed in braces (curly brackets).Note: The - (dash) in this case, because it is outside the square brackets, is a literal. Louise Rains writes to say that it is invalid to commence a NXX code (the 123) with a zero (which would be permitted in the expression above). In this case the expression [1-9][0-9]{2}-[0-9]{4} would be necessary to find a valid local phone number.
{n,m}Matches when the preceding character occurs 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).
{n,}Matches when the preceding character occurs at least n times, for example, ba{2,}b will find 'baab', 'baaab' or 'baaaab' but NOT 'bab'. Values are enclosed in braces (curly brackets).

Examples:
            (1)
jtony@genoa:~/learn/shell/UserGuide$ grep -rn  --color 'W*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)
jtony@genoa:~/learn/shell/UserGuide$ grep -rn  --color 'W*in' string2.txt
1:Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
2:Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)

(2):
egrep -rn --color '[xX][0-9a-z]{2}'  string2.txt
2:Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)
 


No comments:

Post a Comment