STRREPLACE

Purpose:

Replaces all instances of a pattern in a string with a new pattern.

Syntax:

STRREPLACE("srcstr", "patstr", "repstr", caseflag, maxmatches, wildcards)

"srcstr"

-

A string. The source string.

"patstr"

-

A string. The pattern to search for.

"repstr"

-

A string. The replacement pattern.

caseflag

-

Optional. An integer, the case sensitivity flag.

0:

case not significant (default)

1:

case significant

maxmatches

-

Optional. An integer, the maximum number of matches to process. Defaults to -1, process all matches.

wildcards

-

Optional. An integer, process * (any sequence) and ? (single character) as wildcard characters or use regular expressions (regex) in "patstr".

0:

no wildcards and no regular expressions (default)

1:

support * and ? wildcards

2:

support regular expressions (regex)

Returns:

A string, the result of replacing patstr with repstr in srcstr.

Example:

strreplace("This is the source string", "source", "resulting")

 

returns "This is the resulting string".

Example:

strreplace("aaa AAA", "AAA", "new text here")

 

returns "aaa new text here" since STRREPLACE defaults to case sensitive replacement.

Example:

strreplace("aaa AAA", "AAA", "new text here", 0)

 

returns "new text here new text here" since case insensitive replacement is specified.

Example:

strreplace("abc日本語def", "c日", "X本XX");

 

returns "abX本XX本語def".

Example:

strreplace("abc日本語def", "c?", "X本XX", 1, -1, 1);

 

returns "abX本XX本語def", same as above, except the ? wildcard is used.

Example:

strreplace("abc日本語def", "c.", "X本XX", 1, -1, 2);

 

returns "abX本XX本語def", same as above, except the . regular expression is used.

Example:

strreplace("a1b2c3", "[0-9]", "N", 1, -1, 2) == "aNbNcN");

 

returns "aNbNcN", since all the digits are replaced with N.

Example:

strrep("John Smith", "(\w+) (\w+)", "$2 $1", 1, -1, 2)

 

returns "Smith John". When wildcard is 2, the replacement string "repstr", supports backreferences and substitution tokens.

Remarks:

If no match is found, the original string is returned.

 

The pattern and replacement strings can be of different lengths.

 

STRREPLACE defaults to case sensitive string replacement.

 

STRREPLACE can be abbreviated STRREP.

 

When wildcard is set to 1, use * in "patstr" to match any character sequence and ? to match a single character. To search for a literal * or ? character instead of treating them as wildcards, escape them by prefixing a backslash: \* or \?.

 

When wildcard is set to 2, the search pattern "patstr" is interpreted a regular expression. Regular expressions (regex) provide far more flexibility than simple wildcards. A regular expression can indicate character classes (match a character from a set), quantifiers (how many times a pattern repeats), anchors (position in the string) and logical operators. For example, the pattern:

 

[0-9]{3}

 

matches exactly 3 digits.

 

a = strrep("abc123xy4567", "[0-9]{3}", "FOUR", 1, -1, 2)

 

returns "abcFOURxyFOUR7" by replacing the 3 digit sequences with "FOUR".

 

Below is a summary of regular expression patterns when wildcards is 2:

 

Regex Quick Summary

 

Category

Character

Description / Matches

Character Matches

.

Any single character except a newline (\n).

Character Matches

\d

Any digit character (equivalent to [0-9]).

Character Matches

\w

Any alphanumeric "word" character (letters, numbers, underscore).

Character Matches

\s

Any whitespace character (spaces, tabs, line breaks).

Character Classes

[abc]

Any single character inside the brackets (a, b, or c).

Character Classes

[^abc]

Any single character NOT inside the brackets.

Quantifiers

*

0 or more repetitions of the preceding token.

Quantifiers

+

1 or more repetitions of the preceding token.

Quantifiers

?

0 or 1 repetition (makes the preceding token optional).

Quantifiers

{n,m}

Between n and m repetitions of the token.

Anchors

^

The start of the string or line.

Anchors

$

The end of the string or line.

Anchors

\b

A word boundary (the edge position between a \w and a non-\w character).

Logic / Groups

|

Alternation operator (acts like a logical OR statement).

Logic / Groups

(...)

Capturing group. Isolates tokens and remembers the match context.

 

Common Regex Patterns

Digits only: ^[0-9]+$

Uppercase letters: ^[A-Z]+$

Alphanumeric: ^[A-Za-z0-9]+$

Floating‑point number: ^[+-]?\d+(\.\d+)?$

Email (simple): ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$

 

Backreferences and Substitution Tokens

When wildcard is 2, the replacement string "repstr", supports backreferences and substitution tokens.

 

Token

Meaning

Example call

Result

$0

Whole match

strrep("hello","(\w+)","[$0]",1,-1,2)

"[hello]"

$1 .. $9

Numbered capture group

strrep("John Smith","(\w+) (\w+)","$2 $1",1,-1,2)

"Smith John"

$10, $11 …

Multi-digit group number

strrep("abcdefghijk","(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)","$11",1,-1,2)

"k"

${name}

Named capture group

strrep("hello world","(?<a>\w+) (?<b>\w+)","${b} ${a}",1,-1,2)

"world hello"

\$

Literal dollar sign

strrep("100","[0-9]+","\$$0",1,-1,2)

"$100"

\\

Literal backslash

strrep("hello","\w+","\\",1,-1,2)

"\"

 

See FILESTRREP to replace a string in a list of files.

See Also:

FILESTRREP

STRCAT

STRCMP

STREXTRACT

STRFIND

STRGET

STRIDXGET

STRMATCH

STRREPEAT

STRSORT

STRTRIM