String
libraryRegular expressions, text replacement, Base64 and XML formatting for strings. Not to be confused with Lua's built-in lowercase `string` library. Patterns here are Java regular expressions, not Lua patterns, and a backslash must be doubled in Lua source: write `"\\d+"` for `\d+`.
- Kind Library, a global in every script
- Category Utilities
- Functions 7
Functions
-
regexstring Returns the first match of a regular expression, or the text of one of its capture groups. -
regexArraytable Returns the first match of a regular expression together with its capture groups, as a list: item 1 is the whole match, items 2 and up are the groups in order. -
regexMatchboolean Tells whether the whole text matches the regular expression. -
replacestring Replaces every occurrence of a piece of text. -
encodestring Encodes text as standard Base64 with padding. -
decodestring Decodes standard Base64 (with or without padding) into text, read in the JVM default charset (UTF-8 on current Java versions). -
formatXmlstring Re-indents XML text.
regex()
Section titled “regex()”String.regex(lineLua: string, patternLua: string)→stringString.regex(lineLua: string, patternLua: string, groupLua: number)→string Returns the first match of a regular expression, or the text of one of its capture groups. When there is no match, or the requested group did not take part in the match, the result is the text `Not found`, not `nil`; compare against that text or use `String.regexMatch`. Group 0 is the whole match, group 1 the first set of parentheses. A group number beyond the number of groups throws `No group n`, and an invalid expression throws the pattern error.
Parameters
-
lineLuastring - Text to search
-
patternLuastring - Java regular expression
-
groupLuanumber optional - Capture group to return: 0 is the whole match, 1 the first group
Returns
Example runs in CI
print(String.regex("order-1234", "\\d+"))print(String.regexMatch("abc", "a.c"))print(String.replace("a-b-c", "-", "+"))print(String.decode(String.encode("hello")))regexArray()
Section titled “regexArray()”String.regexArray(lineLua: string, patternLua: string)→table Returns the first match of a regular expression together with its capture groups, as a list: item 1 is the whole match, items 2 and up are the groups in order. Only the first match in the text is used, not every match. The list is empty when nothing matches, and a group that did not take part in the match leaves a hole (`nil`) in the list.
Parameters
-
lineLuastring - Text to search
-
patternLuastring - Java regular expression
Returns
Example runs in CI
local parts = String.regexArray("john@example.nl", "(\\w+)@(\\w+)\\.(\\w+)")print(parts[1], parts[2], parts[3], parts[4])regexMatch()
Section titled “regexMatch()”String.regexMatch(lineLua: string, patternLua: string)→boolean Tells whether the whole text matches the regular expression. It is not enough that a part matches: `abcd` does not match `a.c`, use `.*c.*` for that.
Parameters
-
lineLuastring - Text to test
-
patternLuastring - Java regular expression the whole text must match
Returns
replace()
Section titled “replace()”String.replace(string: string, replace: string, with: string)→string Replaces every occurrence of a piece of text. The search text is literal, not a pattern, so `.` only matches a dot. All three arguments are required: a missing replacement is inserted as the text `nil`.
Parameters
-
stringstring - Text to change
-
replacestring - Text to look for (literal)
-
withstring - Text to put in its place, may be empty
Returns
encode()
Section titled “encode()”String.encode(value: string)→string Encodes text as standard Base64 with padding. The text is converted to bytes as UTF-8 first.
Parameters
-
valuestring - Text to encode
Returns
decode()
Section titled “decode()”String.decode(value: string)→string Decodes standard Base64 (with or without padding) into text, read in the JVM default charset (UTF-8 on current Java versions). Throws on characters that are not Base64, such as spaces or line breaks.
Parameters
-
valuestring - Base64 text to decode
Returns
formatXml()
Section titled “formatXml()”String.formatXml(xml: string)→stringString.formatXml(xml: string, indent: number)→string Re-indents XML text. Without a second argument it indents with two spaces. Throws a parser error when the text is not well-formed XML. To work with the content of XML, use `Xml`.
Parameters
-
xmlstring - Well-formed XML text
-
indentnumber optional - Indentation: 1 tabs, 2 two spaces, 3 three spaces, 4 four spaces, any other number gives compact output on one line
Returns
Example runs in CI
print(String.formatXml("<a><b>1</b></a>"))print(String.formatXml("<a><b>1</b></a>", 0))