This string library provides generic functions for string manipulation, such as, finding and extracting substrings, and pattern matching. The patterns are described in the subsequent sections.

When indexing a string, the first character is at position 1. indexes can be negative and are interpreted as indexing backwards, from the end of the string. Therefore, the last character is at position -1.

string.find(s, pattern [, init [, plain]])

This function looks for the first match of the pattern in the string s. If it finds a match, then find returns the indexes of s where this occurrence starts and ends. Otherwise, it returns nil.

A third, optional numerical argument init specifies where to start the search. Its default value is 1 and may be negative. A value of true as a fourth optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered "magic".

You must specify init if plain is specified. If the pattern has captures, then in a successful match the captured values are also returned, after the two indexes.

local x, y = string.find('hello world', 'world')

local a, b, c, d = string.find('my street number is 54a', '(%d+)(%a+)')

string.match(s, pattern [, init])

Looks for the first match of pattern in the string s. If it finds one, then match returns the captures from the pattern. Otherwise it returns nil. If pattern specifies no captures, then the whole match is returned. A third optional numerical argument init specifies where to start the search. Its default value is 1 and may be negative.

local x, y = string.match('my street number is 54a', '(%d+)(%a+)')

string.gmatch(s, pattern)

Returns an iterator function, in each call it returns the next captures from pattern over string s. If pattern specifies no captures, then the whole match is produced in each call.

The example collects all pairs key=value from the given string into a dictionary table.

local t = {}
local s = 'from=world, to=moon'
for k, v in string.gmatch(s, '(%w+)=(%w+)') do
    t[k] = v

string.sub(s, i [, j])

Returns the substring of s that starts at i and continues until j. i and j may be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length). In particular, the call string.sub(s,1,j) returns a prefix of s with length j, and string.sub(s, -i) returns a suffix of s with length i.

local x = string.sub('hello world', 3)

string.gsub(s, pattern, repl [, n])

Returns a copy of s in which all occurrences of the pattern have been replaced by a replacement string specified by repl. It also returns a second value containing the total number of substitutions made. The optional last parameter n limits the maximum number of substitutions to occur. For instance, when n is 1 only the first occurrence of pattern is replaced.

The string repl is used for replacement. The character % works as an escape character: 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 the following example). The sequence %0 stands for the whole match. The sequence %% stands for a single %.

local x = string.gsub('hello world', '(%w+)', '%1 %1')

local x = string.gsub('hello world', '%w+', '%0 %0', 1)

local x = string.gsub('hello world from Lua', '(%w+)%s*(%w+)', '%2 %1')


Receives a string and returns its length. The empty string '' has length 0.

local x = string.len('hello world')

string.rep(s, n)

Returns a string that is the concatenation of n copies of the string s.

local x = string.rep('hello',3


Returns a string that is the string s reversed.

local x = string.reverse('hello world')


Receives a string and returns a copy of this string with all uppercase letters changed to lowercase. All other characters are left unchanged.

local x = string.lower('hElLo World')


Receives a string and returns a copy of this string with all lowercase letters changed to uppercase. All other characters are left unchanged.

local x = string.upper('hElLo World')

string.format(formatstring, e1, e2, ...)

Returns a formatted version of all arguments following the formatstring. The format string follows the same rules as the printf() family of standard C functions and must be a string. The only differences are that the options/modifiers *, l, L, n, p, and h are not supported and there is an extra option q. The q option formats a string in a form suitable to be safely read back by the Lua interpreter. The options c, d, E, e, f, g, G, i, o, u, x, and X all expect a number as argument, whereas q and s expect a string.


  • %d, %i: Signed integer
  • %u: Unsigned integer
  • %f, %g, %G: Floating point
  • %e, %E: Scientific notation
  • %o: Octal integer
  • %x, %X: Hexadecimal integer
  • %c: Character
  • %s: String
  • %q: Safe string
local x = string.format('d=%d e=%e f=%f g=%g', 1.23, 1.23, 1.23, 1.23)


Patterns are similar to regular expressions. A character class is used to represent a set of characters.

The following combinations are allowed in describing a character class:

Pattern Description


Represents the character x itself (if x is not one of the magic characters ^$()%.[]*+-?)


(a dot) represents all characters


Represents all letters


Represents all control characters


Represents all digits


Represents all lowercase letters


Represents all punctuation characters


Represents all space characters


Represents all uppercase letters


Represents all alphanumeric characters


Represents all hexadecimal digits


Represents the character with representation 0


Represents the character x (where x is any non-alphanumeric character). 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.


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. The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.

Example of character sets:

  • [%w_] (or [_%w]): All alphanumeric characters plus the underscore.
  • [0-7]: Represents the octal digits.
  • [0-7%l%-] represents the octal digits plus the lowercase letters plus the `-´ character.


Represents the complement of set, where set is interpreted as above.

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

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 substring 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.

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.


If you want to process unicode characters, you can use the library unicode.utf8 which contains the similar functions like the string library, but with Unicode support. There is another library called unicode.ascii that has the same functionality like the library string.

XML Parsing

The library lxp contains several features to process XML data. The official reference to the functions are available at LuaExpat.

Here are the supported methods:

Methods Description, [separator])

The parser is created by a call to the function, which returns the created parser or raises a Lua error. It receives the callbacks table and optionally the parser separator character used in the namespace expanded element names.


Closes the parser, freeing all memory used by it. A call to parser:close() without a previous call to parser:parse() could result in an error.


Returns the base for resolving relative URIs.


Returns the callbacks table.


Parse some more of the document. The string s contains part (or perhaps all) of the document. When called without arguments the document is closed (but the parser still has to be closed). The function returns a non nil value when the parser has been successful, and when the parser finds an error it returns five results: nil, msg, line, col, and pos, which are the error message, the line number, column number and absolute position of the error in the XML document.


Returns three results: the current parsing line, column, and absolute position.


Sets the base to be used for resolving relative URIs in system identifiers.


Set the encoding to be used by the parser. There are four built-in encodings, passed as strings: "US-ASCII", "UTF-8", "UTF-16", and "ISO-8859-1".


Abort the parser and prevent it from parsing any further through the data it was last passed. Use to halt parsing the document when an error is discovered inside a callback, for example. The parser object cannot accept more data after this call.

SQL Parsing

The self developed library sqlparsing contains many functions to process SQL statements. For more information about the functions and their usage, see SQL Preprocessor.

Internet Access

Through the library socket, you can open HTTP, ftp, and smtp connections. More information is available at

Math Library

The math library is an interface to the standard C math library and provides the following functions and values:

Library Description


Absolute value of x


Arc cosine of x (in radians)


Arc sine of x (in radians)


Arc tangent of x (in radians)


Arc tangent of y/x (in radians), but uses the signs of both operands to find the quadrant of the result


Smallest integer larger than or equal to x


Cosine of x (assumed to be in radians)


Hyperbolic cosine of x


Angle x (given in radians) in degrees


Return natural exponential function of x


Largest integer smaller than or equal to x




Returns m and n so that x=m2^n, n is an integer and the absolute value of m is in the range [0.5;1) (or zero if x is zero)


Value HUGE_VAL which is larger than any other numerical value


Returns m2^n (n should be an integer)


Natural logarithm of x


Base-10 logarithm of x

math.max(x, ...)

Maximum of values

math.min(x, ...)

Minimum of values


Returns two numbers, the integral part of x and the fractional part of x


Value of π


Returns value x^y


Angle x (given in degrees) in radians


Interface to random generator function rand from ANSI C (no guarantees can be given for statistical properties). When called without arguments, returns a pseudorandom real number in the range [0;1). If integer m is specified, then the range is [1;m]. If called with m and n, then the range is [m;n].


Sets x as seed for random generator


Sine of x (assumed to be in radians)


Hyperbolic sine of x


Square root of x


Tangent of x


Hyperbolic tangent of x

Table Library

This library provides generic functions for table manipulation. Most functions in the table library assume that the table represents an array or a list. For these functions, the length of a table means the result of the length operator (#table). The functions and their descriptions are as follows:

  • table.insert(table, [pos,] value): Inserts element value at position pos in table, shifting up other elements if necessary. The default value for pos is n+1, where n is the length of the table, so that a call table.insert(t,x) inserts x at the end of table t.
  • table.remove(table [, pos]): Removes from table the element at position pos, shifting down other elements if necessary. Returns the value of the removed element. The default value for pos is n, where n is the length of the table, so that a call table.remove(t) removes the last element of table t.
  • table.concat(table [, sep [, i [, j]]]): Returns table[i]..sep..table[i+1] ... sep..table[j]. The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table. If i is greater than j, returns the empty string.
  • table.sort(table [, comp]): Sorts table elements in a given order, in-place, from table[1] to table[n], where n is the length of the table. If comp is given, then it must be a function that receives two table elements, and returns true when the first is less than the second (so that not comp(a[i+1],a[i]) will be true after the sort). If comp is not given, then the standard Lua operator < is used instead. The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.

System Tables

The following system tables show the existing database scripts:


For more information, see Metadata System Tables.