REX Replace Syntax

When replacing the match of a REX/RE2 expression, the replacement string has the following syntax:

  • The characters "?#{}+\" are special. To use them literally, precede them with the escapement character "\'.

  • Replacement strings may just be a literal string or they may include the "ditto" character "?'. The ditto character will copy the character from the search buffer that is in the same position as the ditto character is in the replacement string.

  • A decimal digit placed within curly-braces (e.g. {5}) will copy the character at that index (of the search buffer) to the output. Characters are indexed starting at 1. An index beyond the end of the search buffer will not print anything.

  • A "\" followed by a decimal number will copy that subexpression (REX) or parenthetical numbered capturing group (RE2) to the output. Subexpressions and groups are numbered starting at 1. Named groups (RE2) are not currently supported. See here for more on RE2.

  • The sequence "\&" will copy the entire expression match (sans \P and \F portions, if REX syntax) to the output. This escape was added in Texis version 7.06.

  • A plus-character "+" will place an incrementing decimal number to the output. One purpose of this operator is to number lines.

  • A "#" followed by a number will cause the numbered subexpression (REX) or parenthetical numbered capturing group (RE2) to be printed in hexadecimal form. Subexpressions and groups are numbered starting at 1. Named groups (RE2) are not currently supported.

  • Any character in the replace-string may be represented by the hexadecimal value of that character using the following syntax: \xhh where hh is the hexadecimal value.


EXAMPLE

<$data = "Roses are red" "Violets are blue">
<$expr = "blue" "red">
<$replace = "you" "dead">
<sandr $expr $replace $data>
<LOOP $ret>
  $ret
</LOOP>

The output would be:

Roses are dead
Violets are you


CAVEATS
The sandr function was added Aug. 23 1996.

The replacements occur in the order of values in $expr, so a later $expr might match a previous $replace value.


SEE ALSO
rex, split


Copyright © Thunderstone Software     Last updated: Apr 15 2024
Copyright © 2024 Thunderstone Software LLC. All rights reserved.