Extended Regular Expressions


The egrep utility, as well as grep and sed when run with the E option, provide all the special characters that are included in ordinary regular expressions, except for \( and \), as well as several others. The vim editor includes the additional characters as well as \( and \). Patterns using the extended set of special characters are called full regular expressions or extended regular expressions.

Two of the additional special characters are the plus sign (+) and the question mark (?). They are similar to *, which matches zero or more occurrences of the previous character. The plus sign matches one or more occurrences of the previous character, whereas the question mark matches zero or one occurrence. You can use any one of the special characters *, +, and ? following parentheses, causing the special character to apply to the string surrounded by the parentheses. Unlike the parentheses in bracketed regular expressions, these parentheses are not quoted (Table A-7).

Table A-7. Extended regular expressions

Regular expression

Matches

Examples

/ab+c/

a followed by one or more b's followed by a c

yabcw, abbc57

/ab?c/

a followed by zero or one b followed by c

back, abcdef

/(ab)+c/

One or more occurrences of the string ab followed by c

zabcd, ababc!

/(ab)?c/

Zero or one occurrence of the string ab followed by c

xc, abcc


In full regular expressions, the vertical bar (|) special character is a Boolean OR operator. Within vim, you must quote the vertical bar by preceding it with a backslash to make it special (\|). A vertical bar between two regular expressions causes a match with strings that match the first expression, the second expression, or both. You can use the vertical bar with parentheses to separate from the rest of the regular expression the two expressions that are being ORed (Table A-8).

Table A-8. Full regular expressions

Regular expression

Meaning

Examples

/ab|ac/

Either ab or ac

ab, ac, abac (abac is two matches of the regular expression)

/^Exit|^Quit/

Lines that begin with Exit or Quit

Exit, Quit, No Exit

/(D|N)\. Jones/

D. Jones or N. Jones

P.D. Jones, N. Jones





A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net