Name
PatternReplaceStr -- modify string contents using pattern matching (V5.0)
Synopsis
r$, n = PatternReplaceStr(s$, pat$, repl[, n, encoding])
Function
PatternReplaceStr() can be used to modify a string's contents using pattern matching. This function is powerful and can be used for all kinds of string operations. It returns a copy of s$ in which all occurrences of the pattern pat$ have been replaced by a replacement string specified by repl. PatternReplaceStr() also returns, as a second value, the total number of substitutions made.

The third argument repl can be either a string or a callback function. If repl is a string, then its value is used for replacement. Any sequence in repl of the form %n, with n between 1 and 9, stands for the value of the n-th captured substring (see below). If repl is a function, then this function is called every time a match occurs, with all captured substrings passed as arguments, in order; if the pattern specifies no captures, then the whole match is passed as a sole argument. If the value returned by this function is a string, then it is used as the replacement string; otherwise, the replacement string is the empty string.

The optional argument n limits the maximum number of substitutions to occur. For instance, when n is 1 only the first occurrence of pat$ is replaced.

The pattern specified in pat$ is made up of a sequence of pattern items. A pattern item is usually a character class which in turn represents a set of characters. The following combinations are allowed in describing a character class:

x (where x is not one of the magic characters ^$()%.[]*+-?)
Represents the character x itself.
. (a dot)
Represents all characters.
%a
Represents all letters.
%c
Represents all control characters.
%d
Represents all digits.
%g
Represents all characters that have a graphical representation. (V7.0)
%l
Represents all lowercase letters.
%p
Represents all punctuation characters.
%s
Represents all space characters.
%u
Represents all uppercase letters.
%w
Represents all alphanumeric characters.
%x
Represents all hexadecimal digits.
%z
Represents the character with representation 0.
%x (where x is any non-alphanumeric character)
Represents the character x. This is the standard way to escape the magic characters. Any punctuation character (even the non magic) can be preceded by a % when used to represent itself in a pattern.
[set]
Represents the class which is the union of all characters in set. A range of characters may be specified by separating the end characters of the range with a -. All classes %x described above may also be used as components in set. All other characters in set represent themselves. For example, [%w_] (or [_%w]) represents all alphanumeric characters plus the underscore, [0-7] represents the octal digits, and [0-7%l%-] represents the octal digits plus the lowercase letters plus the - character. The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.
[^set]
Represents the complement of set, where set is interpreted as above.

For all classes represented by single letters (%a, %c, etc.), the corresponding uppercase letter represents the complement of the class. For instance, %S represents all non-space characters.

The following items are valid pattern items:

A pattern is a sequence of pattern items. A ^ at the beginning of a pattern anchors the match at the beginning of the subject string. A $ at the end of a pattern anchors the match at the end of the subject string. At other positions, ^ and $ have no special meaning and represent themselves.

A pattern may contain sub-patterns enclosed in parentheses; they describe captures. When a match succeeds, the substrings of the subject string that match captures are stored (captured) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern (a*(.)%w(%s*)), the part of the string matching a*(.)%w(%s*) is stored as the first capture (and therefore has number 1); the character matching . is captured with number 2, and the part matching %s* has number 3.

As a special case, the empty capture () captures the current string position (a number). For instance, if we apply the pattern ()aa() on the string "flaaap", there will be two captures: 3 and 5.

A pattern cannot contain embedded zeros. Use %z instead.

The optional encoding parameter can be used to set the character encoding to use. This defaults to the default string encoding set using SetDefaultEncoding(). See Character encodings for details.

Inputs
s$
string to modify
pat$
pattern according to which the string should be modified
repl
replacement string or callback function to handle replacements (see above)
n
optional: maximum number of substitutions to make (defaults to the length of s$ plus 1)
encoding
optional: character encoding to use (defaults to default string encoding) (V7.0)
Results
r$
resulting string
n
number of substitutions made
Example
s$ = PatternReplaceStr("Hello World", "(%w+)", "%1 %1")
The code above returns "Hello Hello World World"


s$ = PatternReplaceStr("Hello World from Hollywood", "(%w+)%s*(%w+)",
     "%2 %1")
The code above returns "World Hello Hollywood from"


s$ = PatternReplaceStr("home = $HOME, user = $USER", "%$(%w+)", GetEnv)
The code above returns "home = /home/andreas, user = andreas" (on Linux).


Local t = {name = "Hollywood", version="5.0"}
s$ = PatternReplaceStr("$name_$version.jpg", "%$(%w+)", Function(v)
     Return(t[v]) EndFunction)
The code above returns "Hollywood_5.0.jpg"

Show TOC