6.10 Regular Expression Registers

The special regexp parenthesis (i.e. \( ... \)) are used for three different purposes:

We would like to give a little more details on the (3) purpose. Let us assume, that we invoke:

'sum: \([0-9]+\)\.\([0-9]\) US$'
  -> t.regexp_search 
        (# 
        do 'The amount is: '->puttext;
           (1->regs.start+1,1->regs.end)->t.sub->puttext;
           ' dollars and '->puttext;
           (2->regs.start+1,2->regs.end)->t.sub->puttext;
           ' cents'->putline
        #)

The intent is to find some amount i the t text variable. The amount should be found in a context of the form: 'sum: XXX.YYY US$', where XXX and YYY may be any number. If found, the amount will be written on standard screen in the form: 'The amount is: XXX dollars and YYY cents'.

Note the use of i->regs.start and i->regs.end, which returns the character positions of the i'th matched substring (in this case the 1'st and 2'nd substring, containing the amount of dollars and the amount of cents, respectively). As it can be seen, these constructs can be used within the action-parts of all regexp operations to gain access to the matched substrings (note that the 0'th substring is the entire string matched).


Basic Libraries - Reference Manual
© 1990-2002 Mjølner Informatics
[Modified: Wednesday October 18th 2000 at 13:14]