Menu

REPLACE statement

Rx (9)
Oleg Osepyants

REPLACE statement

This statement replaces with the target string all substrings in the text or text fragment that match the specified Rx string. And, for sure, you will use this statement most often.

Usage

REPLACE <FindString> TO <ReplaceString>
REPLACE <FindString> TO <ReplaceString> INSIDE OF <ContextArea>
REPLACE <FindString> TO <ReplaceString> OUTSIDE OF <ContextArea>

Parameters

  • FindString - An Rx string that specifies the substrings in the text to be replaced.
  • ReplaceString - An Rx string to which all found substrings will be replaced. This parameter must not be a regular expression (otherwise you will get an error).
  • ContextArea - An Rx string that specifies the Context area (not needed if the statement is running in the global scope).

Examples

Example 1

Replace the CR and LF characters to an HTML line break (<br />)

REPLACE B"\r\n" TO P"<br />"

Original text

The first row of text.
The second row.
And a little more text.

The text after applying the statement above

The first row of text.<br />The second row.<br />And a little more text.

Example 2

And now - remove all HTML tags from the text. To do this, we need to specify the desired tags using a regular expression. And by the way, this is one of the three possible ways in the Rx language to remove something from the text.

REPLACE R"<[^<]*?>" TO P""

Original text

<p>The <b>text</p> example with <i>HTML</i> tags.</p>

The text after applying the statement above

The text example with HTML tags.

Example 3

And finally, let's slightly tweak the text we got in the previous example so that its meaning matches its essence.

REPLACE P"with" TO P"without"

Original text

The text example with HTML tags.

The text after applying the statement above

The text example without HTML tags.

Related

Wiki: REPLICATE statement
Wiki: Rx text transformation script language

MongoDB Logo MongoDB