replace

Synopsis

set  replace(set data,  string substitution)
eset replace(eset data, string substitution)
mset replace(mset data, string substitution)

Description

The replace function searches for a pattern in the specified column of the parameter data, which can be a set, an eset or an mset. Whenever the pattern is found, it replaces the pattern with a replacement. The column to process, the pattern and the replacement are specified in the parameter substitution.

The format of substitution is as follows:

  &XXX/pattern/replacement/opt_flags
The first part of the substitution string specifies the number of the column to perform the substitution on. Columns are numbered starting fro zero, so &0 would specify the first column, &1 - the second, and so on. Both pattern and replacement are regular expressions specifying what pattern to look for and what to replace the pattern with. The last section of the substitution string is optional and can contain flags that change the way the regular expression is matched.

!!!TODO!!!: nobody knows what regular expression flags are supported; the usual ones are either unnecessary or don't seem to work. This page will be updated as more research is done. Unknown flags do not cause errors and are ignored.

The section on regular expression matching operators contains a detailed description of regular expression features supported by QL. Consult that page to find out more about what regular expression features QL supports.

Example

In this example we create a set, an eset and an mset and apply the replace routine to various columns of the created data.
>> words = {"hello", "world"}
>> anEset = words X words
>> anMset = anEset ** anEset
First, we will deal with the set words; notice that applying the replace function to any column except the first one returns an empty set, because the set only has one column :
>> replace (words, "&0/orl/in/")
hello
wind
>> {} == replace (words, "&1/orl/in/")
true
A binary relation (eset) has two columns, numbered zero and one, and we can apply the replace command to either one. Predictably, only the specified column is affected by the replace function. An attempt to apply the replace command to a non-existant column produces an empty eset.
>> replace (anEset, "&1/world/dog/")
hello hello
hello dog
world hello
world dog
>> replace (anEset, "&0/[elo]/z/")
hzzzz hello
hzzzz world
wzrzd hello
wzrzd world
>> {} X {} == replace (anEset, "&3/[elo]/z/")
true
Dealing with an mset is very much similar to dealing with an eset, except the number of columns is not restricted to two. Performing a replacement on a non-existant column again produces an empty data structure.
>> replace (anMset, "&2/world/hello/")
hello hello hello
hello hello hello
hello world hello
hello world hello
world hello hello
world hello hello
world world hello
world world hello
>> replace (anMset, "&1/./a/")
hello aaaaa hello
hello aaaaa world
hello aaaaa hello
hello aaaaa world
world aaaaa hello
world aaaaa world
world aaaaa hello
world aaaaa world
>> replace (anMset, "&7/./a/")
>>

See also

RE matching operators