Find and replace using regular expression

About regular expressions

Regular expression syntax




int s.replacerx(pattern [replaceto|rr] [flags])



s - str variable.

pattern - regular expression that matches the substring to find. String.

replaceto - replacement string. Default: "". Some sequences of characters in replaceto have special meaning:

$n, $nn, ${n} n-th or nn-th captured submatch (1 to 99).
$0, ${0} entire match.
$$ $
$` part of s that precedes match
$' part of s that follows match


rr - address of variable of type REPLACERX.


1 Case insensitive.
2 Whole word. This adds \b to the beginning and end of pattern.
4 Single replacement.
8 Multiline. If this flag is set (or (?m) is used in pattern), ^ and $ match beginning and end of line. Default: ^ and $ match beginning and end of whole string.
32 QM 2.3.0. Convert pattern from UTF-8 to ANSI. Used when QM is running in Unicode mode (ignored otherwise). Set this flag if pattern contains non ASCII characters, but s is ANSI (not UTF-8). It is needed because these characters in pattern normally consist of 2 or 3 bytes, whereas characters in s consist of 1 byte.
pcre flags  



Finds parts of s that match pattern and replaces them with replaceto.


If flag 4 is not set, replaces all found matches, and returns the number of replacements. If flag 4 is set, replaces only the first found match, and returns 0-based character index of it in s, or -1 if not found.



 Replace two consecutive identical words with single
str s="Is is the cost of of gasoline going up up?"
out s.replacerx("([a-z]+) \1" "$1" 1|2)
out s
 Is the cost of gasoline going up?