Eyeon:Script/Reference/Libraries/string/gsub
From VFXPedia
Contents |
String : string.gsub
Arguments
string.gsub(s, pattern, replace ,n )
- s (required, string)
The string to be searched
- pattern (required, string)
The pattern to be searched for
- replace (required, string or function)
The string to replace 'pattern' with, or a function to be called with pattern as its argument.
- n (optional, number)
A numeric value which represents the maximum number of substitutions to make.
Returns
Returns a copy of 's' where all copies of 'pattern' are replaced by the string specified by the 'replace' argument. Also returns a second value that indicates the total number of replacements made.
Remarks
The string.gsub function is extremely flexible, and also quite complex. In its simplest form it can be used to find and replace a arbitrary pattern in a given string. More complex examples of its use take advantage of the ability to execute a provided function against each of the captured patterns.
The replace argument can be either a string or a function. If the argument is a string then the value of that string is used to replace any instance of the pattern found in argument 's'. Any sequence in replace of the form %n with n being a value between 1 and 9 stands for the value of the n'th captured substring.
If the 'replace' argument is a function, then this function is called every time a match for 'pattern' is found, with all captured substrings passed as arguments in order (see examples below). If the value returned by the function is a string then that string is used as the replacement string, otherwise the replacement string is an empty string.
The last optional parameter 'n' is a number that limits the maximum number of substitutions to occur. For instance when n is 1 only the first pattern matched will be replaced.
Patterns
Character Class:
A character class is used to represent a set of characters. The following combinations are allowed in describing a character class:
- x
(where x is any 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.
- %l
- represents all lower case letters.
- %p
- represents all punctuation characters.
- %s
- represents all space characters.
- %u
- represents all upper case 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. We recommend that any punctuation character (even the non magic) should be preceded by a % when used to represent itself in a pattern.
char-set
- represents the class which is the union of all characters in char-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 a char-set. All other characters in char-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 lower case letters plus the -character.
The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z]or [a-%%] have no meaning.
^bchar-set
- represents the complement of char-set, where char-set is interpreted as above. For all classes represented by single letters (%a, %c, ...), the corresponding upper-case letter represents the complement of the class. For instance, %S represents all non-space characters.
The definitions of letter, space, etc. depend on the current locale. In particular, the class [a-z] may not be equivalent to %l. The second form should be preferred for portability.
Pattern Item:
a pattern item may be
- a single character class, which matches any single character in the class;
- a single character class followed by *, which matches 0 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;
- a single character class followed by +, which matches 1 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;
- a single character class followed by -, which also matches 0 or more repetitions of characters in the class. Unlike *, these repetition items will always match the shortest possible sequence;
- a single character class followed by ?, which matches 0 or 1 occurrence of a character in the class;
- %n, for n between 1 and 9; such item matches a sub-string equal to the n-th captured string (see below);
- %bxy, where x and y are two distinct characters; such item matches strings that start with x, end with y, and where the x and y are balanced. This means that, if one reads the string from left to right, counting +1 for an x and -1 for a y, the ending y is the first y where the count reaches 0. For instance, the item %b()matches expressions with balanced parentheses.
Pattern:
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.
Captures:
A pattern may contain sub-patterns enclosed in parentheses, they describe captures. When a match succeeds, the sub-strings 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.
A pattern cannot contain embedded zeros. Use %z instead.
Requirements
- eyeonScript 5.0
- Fusion 5.0
Examples
-- captures each word in 's' and replaces with -- two copies of the word x = string.gsub("hello world", "(%w+)", "%1 %1") -- x = "hello hello world world" -- as above, but 'n' limits the replacement to -- the first word only x = string.gsub("hello world", "(%w+)", "%1 %1", 1) -- x="hello hello world" -- captures a pattern of two words and reverses -- the words x = string.gsub("hello world from Lua","(%w+)%s*(%w+)", "%2 %1") -- x="world hello Lua from" -- captures words starting with $ and passes the word to -- the function getenv, which returns the value of the -- environment variable named x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) -- x="home = /home/roberto, user = roberto" (for instance) -- captures all text found between two $ symbols, and -- executes them as lua code x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", dostring) -- x="4+5 = 9" -- builds table 't' -- captures words prefaced by the $ symbol and replaces -- them with the same index value from table 't' -- ( $name becomes t["name"] ) local t = {name="lua", version="5.0"} x = string.gsub("$name - $version", "%$(%w+)", function (v) return %t[v] end) -- x="lua - 5.0" -- creates an empty table -- inserts each word from 's' into table t t = {n=0} string.gsub("first second word", "(%w+)", function (w) table.insert(%t, w) end) -- t = { "first", "second", "word"; n=3 }