next up previous contents index
Next: 1.8 Code organization Up: 1.7 Regular expressions Previous: 1.7.2 Splitting   Contents   Index

1.7.3 Substituting

The match and split operators provide enough power that with some effort, it is possible to find regular expression matches, modify the matched text, and create a modified string as output. However, this is a common operation when using regular expressions for text processing, so the subst operator is also provided as a more convenient interface for the most common types of substitution.

Suppose that a list of email addresses needs to be converted from one format to another.. The following snippet does such a conversion:

`Jason Evans (jasone@canonware.com)
Jason O. Evans ( jasone@canonware.com )
'

`(\w[A-Za-z. ]*\w) \(\s*([^\s]+)\s*\)' `"\1" <\2>' <$g true> subst pop
stdout exch write pop

This generates the following output:

"Jason Evans" <jasone@canonware.com>
"Jason O. Evans" <jasone@canonware.com>



Jason Evans 2005-03-16