Basic HTML version of Foils prepared Sept 20 97

Foil 41 Anchoring and Alternation in
Regular Expressions

From Tutorial on PERL Computational Science for Information Age Course CPS616 -- Sept 20 97. by Geoffrey C. Fox,Nancy McCracken,Tom Scavo


In regular expressions, the pipe | (read: or) denotes alternation:
  • a|b|c is equivalent to the character class [abc]
  • Fox|Furmanski # alternation is more general than a character class
  • Fox|Furmansk(i|y|ie) # if we don't know how to spell Polish!
Patterns can be anchored in four ways:
  • /^regex/ matches regex at the beginning of the string only -- ^ has this special meaning only at the start of the regular expression
  • /regex$/ matches regex at the end of the string only -- $ has this meaning only at the end of the regular expression
  • \b matches a word boundary so that
    • /Variable\b/ matches Variable but not Variables (in a character class, \b denotes a backspace)
  • \B matches NOT a word boundary so that
    • /Variable\B/ matches Variables but not Variable



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Sep 21 1997