Extended Regular Expressions

 < Day Day Up > 

The three utilities egrep, grep when run with the E option (similar to egrep), and gawk 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


     < Day Day Up > 


    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    ISBN: 131478230
    EAN: N/A
    Year: 2005
    Pages: 213

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