Basic HTML version of Foils prepared Sept 20 97

Foil 44 Matched Variables 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


The variables \1, \2, \3 etc. correspond to parentheses inside the regex. Outside the regular expression use the PERL variables $1, $2, $3 etc.
In string matching, we identify three parts:
  • $` is variable holding substring BEFORE matched part
  • $& is variable holding substring matched by regular expression
  • $' is variable holding substring AFTER matched part
So original string is the concatenation $` . $& . $'
Note, however, any script that uses these variables suffers a significant performance hit!



© 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