Friday, August 1, 2008

Extended Regular Expression

The question mark -- ? -- matches zero or one of the previous RE. It is generally used for matching single characters.

. The plus -- + -- matches one or more of the previous RE. It serves a role similar to the *, but does not match zero occurrences.
echo a111b | sed -ne '/a1\+b/p'
echo a111b | grep 'a1\+b'
echo a111b | gawk '/a1+b/'

• Escaped "curly brackets" -- \{ \} -- indicate the number of occurrences of a preceding RE to match.
"[0-9]\{5\}" matches exactly five digits (characters in the range of 0 to 9).
bash$ echo 2222 | gawk --re-interval '/2{3}/'

2222

No comments: