Menu

REPLICATE statement

Rx (9)
Oleg Osepyants

REPLICATE statement

This statement replicates the specified number of times the substrings found in the text or text fragment. In this case, inside the replicated substring, it's possible to perform the substitution of the replica number instead of the substring specified by the third parameter of this statement.

Usage

REPLICATE <ReplicaString> <ReplicasNumber> TIMES
REPLICATE <ReplicaString> <ReplicasNumber> TIMES INSIDE OF <ContextArea>
REPLICATE <ReplicaString> <ReplicasNumber> TIMES OUTSIDE OF <ContextArea>
REPLICATE <ReplicaString> <ReplicasNumber> TIMES WITH <IndexString> AS INDEX
REPLICATE <ReplicaString> <ReplicasNumber> TIMES WITH <IndexString> AS INDEX INSIDE OF <ContextArea>
REPLICATE <ReplicaString> <ReplicasNumber> TIMES WITH <IndexString> AS INDEX OUTSIDE OF <ContextArea>

Parameters

  • ReplicaString - An Rx string that specifies the substrings in the text to be replicated.
  • ReplicasNumber - An integer value specifying the number of replicas. This parameter can be a zero - in this case the statement will work in the same way as [REPLACE statement] P"MySubstring" TO P"" (that is, it will remove all found substrings from the text). You will get an error if you pass a negative value here.
  • IndexString - An Rx string specifies the substrings within the replicated text fragment that will be replaced by the copy number. This is an optional parameter.
  • ContextArea - An Rx string that specifies the Context area (not needed if the statement is running in the global scope).

Examples

Example 1

The most common use case for this statement is if you need to create multiple numbered strings from the same pattern, but also produce wildcards in the output to replace them later with the [REPLACE statement] statement. Unclear? Ok, let's look at this with an example.

REPLICATE R"\r\n.*?;" 4 TIMES WITH P"{NUM}" AS INDEX

Original text

I sure that:
{NUM}. My [Item{NUM}] is the best [Item{NUM}];
And in general - I'm the coolest.

The text after applying the statement above

I sure that:
1. My [Item1] is the best [Item1];
2. My [Item2] is the best [Item2];
3. My [Item3] is the best [Item3];
4. My [Item4] is the best [Item4];
And in general - I'm the coolest.

And now you can use the [REPLACE statement] statement to replace the [Item1] ... [Item4] substrings with what you like best about yourself and use it for affirmation ;)

Example 2

Another way to delete in the Rx language. Remove all HTML tags using the REPLICATE statement.

REPLICATE R"<[^<]*?>" 0 TIMES

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.

As you can see, we simply specified to replicate the found fragments 0 times, and this led to their removal.


Related

Wiki: REPLACE statement
Wiki: Rx text transformation script language

MongoDB Logo MongoDB