Next: , Previous: , Up: MFL   [Contents][Index]


4.13 Back references

A back reference is a sequence ‘\d’, where d is a decimal number. It refers to the dth parenthesized subexpression in the last matches statement13. Any back reference occurring within a double-quoted string is replaced with the value of the corresponding subexpression. For example:

if $f matches '.*@\(.*\)\.gnu\.org\.ua'
  set host \1
fi

If the value of f macro is ‘smith@unza.gnu.org.ua’, the above code will assign the string ‘unza’ to the variable host.

Notice, that each occurrence of matches will reset the table of back references, so try to use them as early as possible. The following example illustrates a common error, when the back reference is used after the reference table has been reused by another matching:

# Wrong!
if $f matches '.*@\(.*\)\.gnu\.org\.ua'
  if $f matches 'some.*'
    set host \1
  fi
fi

This will produce the following run time error:

mailfromd: RUNTIME ERROR near file.mfl:3: Invalid back-reference number

because the inner match (‘some.*’) does not have any parenthesized subexpressions.

See Special comparisons, for more information about matches operator.


Footnotes

(13)

The subexpressions are numbered by the positions of their opening parentheses, left to right.