An Introduction to Regular Expressions The Basics 22 of 50 : [PREV] [NEXT]

Standard Meta Characters

Backreferences (continued)

Outside regular expressions, such as in the replacement part of a substitution, this backreference special variable is used as if it were a scalar variable named as an integer (e.g., $1, $2, $3, $99). So, if you want to swap the first two words of a string, for example, you could use:

s/(\S+)\s+(\S+)/$2 $1/

More on the substitution operator later.


© 2003 Barbie barbie@missbarbell.co.uk Home http://birmingham.pm.org/