the string lib implements string formatting and regular expression matching routines.
format(
formatstr, ...)
;
Returns a string formatted according formatstr and the optional parameters following it. The format string follows the same rules as the printf family of standard C functions( the "*" is not supported).
eg. sq> print(format("%s %d 0x%02X\n","this is a test :",123,10)); this is a test : 123 0x0A
lstrip(
str)
;
Strips white-space-only characters that might appear at the beginning of the given string and returns the new stripped string.
regexp(
pattern)
;
compiles a regular expression pattern and returns it as a new regexp class instance.
\ | Quote the next metacharacter |
^ | Match the beginning of the string |
. | Match any character |
$ | Match the end of the string |
| | Alternation |
(subexp) | Grouping (creates a capture) |
(?:subexp) | No Capture Grouping (no capture) |
[] | Character class |
GREEDY CLOSURES.
* | Match 0 or more times |
+ | Match 1 or more times |
? | Match 1 or 0 times |
{n} | Match exactly n times |
{n,} | Match at least n times |
{n,m} | Match at least n but not more than m times |
ESCAPE CHARACTERS.
\t | tab (HT, TAB) |
\n | newline (LF, NL) |
\r | return (CR) |
\f | form feed (FF) |
PREDEFINED CLASSES.
\l | lowercase next char |
\u | uppercase next char |
\a | letters |
\A | non letters |
\w | alphanumeric [_0-9a-zA-Z] |
\W | non alphanumeric [^_0-9a-zA-Z] |
\s | space |
\S | non space |
\d | digits |
\D | non nondigits |
\x | exadecimal digits |
\X | non exadecimal digits |
\c | control charactrs |
\C | non control charactrs |
\p | punctation |
\P | non punctation |
\b | word boundary |
\B | non word boundary |
rstrip(
str)
;
Strips white-space-only characters that might appear at the end of the given string and returns the new stripped string.
split(
str, separators)
;
returns an array of strings split at each point where a separator character occurs in str. The separator is not returned as part of any array element. the parameter separators is a string that specifies the characters as to be used for the splitting.
eg. local a = split("1.2-3;4/5",".-/;"); // the result will be [1,2,3,4,5]
strip(
str)
;
Strips white-space-only characters that might appear at the beginning or end of the given string and returns the new stripped string.
The regexp object rapresent a precompiled regular experssion pattern. The object is created trough the function regexp().
capture(
str, [start])
;
returns an array of tables containing two indexs("begin" and "end")of the first match of the regular expression in the string str. An array entry is created for each captured sub expressions. If no match occurs returns null. The search starts from the index start of the string, if start is omitted the search starts from the beginning of the string.
the first element of the returned array(index 0) always contains the complete match.
local ex = regexp(@"(\d+) ([a-zA-Z]+)(\p)"); local string = "stuff 123 Test;"; local res = ex.capture(string); foreach(i,val in res) { print(format("match number[%02d] %s\n", i,string.slice(val.begin,val.end))); //prints "Test" } ... will print match number[00] 123 Test; match number[01] 123 match number[02] Test match number[03] ;
match(
str)
;
returns a true if the regular expression matches the string str, otherwise returns false.
search(
str, [start])
;
returns a table containing two indexs("begin" and "end") of the first match of the regular expression in the string str, otherwise if no match occurs returns null. The search starts from the index start of the string, if start is omitted the search starts from the beginning of the string.
local ex = regexp("[a-zA-Z]+"); local string = "123 Test;"; local res = ex.search(string); print(string.slice(res.begin,res.end)); //prints "Test"