Regular expression substitution

The function of a regular expression substitution is to substitute the occurrence of a pattern with a given string. A regular expression substitution object is created with a tilde followed by three slashes, with the regular pattern between the first two slashes, and the substitute string between the middle and the last. For example
s = ~s/zz*/oz/

Again, a few options can be given after the closing slash. They are m for multi-line, i for ignoring case, and g for finding all matches.

s = ~s/zz*/oz/

To apply a regular expression substitution, use it as function. It doesn't alter the argument string (like any Shang function), but returns an updated copy of the string, which can be assigned to the same variable if one wishes.

text = "Monkey in the zzoo"
s = ~s/zz*/oz/
text = s(text)
A regular expression substitution s has the following attributes

The pattern and options attributes can be accessed and modified, and the match attribute is a function that matches the pattern against the argument string.



oz 2009-12-22