Documentation ¶
Index ¶
- func Find(text, expr string) (captures []int)
- func FindAll(text, expr string, limit int) (captures [][]int)
- func Gmatch(text, expr string, iter func([]string))
- func Gsub(text, expr string, replacer Replacer) (repl string, count int)
- func GsubAll(text, expr string, replacer Replacer, limit int) (repl string, count int)
- func GsubFunc(text, expr string, replace func(string) string) (string, int)
- func GsubFuncAll(text, expr string, replace func(string) string, limit int) (string, int)
- func GsubMap(text, expr string, vars map[string]string) (string, int)
- func GsubMapAll(text, expr string, vars map[string]string, limit int) (string, int)
- func GsubStr(text, expr, replace string) (repl string, count int)
- func GsubStrAll(text, expr, replace string, limit int) (repl string, count int)
- func Match(text, expr string) (captures []string)
- func MatchAll(text, expr string, limit int) (captures [][]string)
- type Replacer
- type String
- func (str String) Find(text string) []int
- func (str String) FindAll(text string, limit int) [][]int
- func (str String) Gmatch(text string, iter func([]string))
- func (str String) Gsub(text string, replacer Replacer) (string, int)
- func (str String) GsubAll(text string, replacer Replacer, limit int) (repl string, count int)
- func (str String) GsubExpr(text, replace string) (repl string, count int)
- func (str String) Match(text string) (captures []string)
- func (str String) MatchAll(text string, limit int) (captures [][]string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Find ¶
Find finds the first instance of pattern in string and returns its start and end position. If the pattern specified captures, they are returned as well as a slice of strings. If no captures were specified the entire match is returned as a single capture in the slice. If no matches were made, start and end are returned as -1, -1, and captures is nil.
func FindAll ¶
FindAll finds the first instance of pattern in string and returns its start and end position. If the pattern specified captures, they are returned as well as a slice of strings. If no captures were specified the entire match is returned as a single capture in the slice. If no matches were made, start and end are returned as -1, -1, and captures is nil.
func Gmatch ¶
Gmatch returns an iterator function that, each time it is called, returns the next captures from pattern over the string s. If pattern specifies no captures, then the whole match is produced in each call.
As an example, the following loop will iterate over all the words from string s, printing one per line:
s = "hello world from Lua" for w in string.gmatch(s, "%a+") do print(w) end
The next example collects all pairs key=value from the given string into a table:
t = {} s = "from=world, to=Lua" for k, v in string.gmatch(s, "(%w+)=(%w+)") do t[k] = v end
For this function, a caret '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration.
func Gsub ¶
Gsub returns a copy of text in which all (or the upto limit if > 0) occurrences of the pattern have been replaced by the specified replacer. The name gsub comes from global substitution.
Gsub returns the string with replacements and the number of replacements that occurred. If no matches were made, then text is return unmodified with 0 to indicate that no replacements were made.
func GsubAll ¶
GsubAll returns a copy of text in which all (or the upto limit if > 0) occurrences of the pattern have been replaced by the specified replacer. The name gsub comes from global substitution.
Gsub returns the string with replacements and the number of replacements that occurred. If no matches were made, then text is return unmodified with 0 to indicate that no replacements were made.
func GsubFunc ¶
GsubFunc is just like Gsub except that replace is called to retrieve replacement values. The function replace is called every time a match occurs, with all captured substrings passed as arguments, in order.
func GsubFuncAll ¶
GsubFuncAll is just like GsubFunc except that upto limit matches are replaced.
func GsubMap ¶
GsubMap is just like Gsub except that values are retrieved from the vars map. For every match, the table is queried using the first capture as the key.
func GsubMapAll ¶
GsubMapAll is just like GsubMax upto limit matches are replaced.
func GsubStr ¶
GsubStr returns a copy of text in which all (or the upto limit if > 0) occurrences of the pattern have been replaced by the specified replacement value. The name gsub comes from global substitution.
The character '%' works as an escape character: any sequence in replace of the form %d, with 1 <= d <= 9, stands for the value of the d-th capture substring. The sequence %0 stands for the whole match. The sequence %% stands for a single escaped %.
func GsubStrAll ¶
GsubStrAll returns a copy of text in which all (or the upto limit if > 0) occurrences of the pattern have been replaced by the specified replacement value. The name gsub comes from global substitution.
The character '%' works as an escape character: any sequence in replace of the form %d, with 1 <= d <= 9, stands for the value of the d-th capture substring. The sequence %0 stands for the whole match. The sequence %% stands for a single escaped %.
Types ¶
type String ¶
type String string
String is a wrapper type that implements various string operations.
func (String) Find ¶
Find returns the start and end position of the string text found in str.
If str contains a pattern and a match is made with text, the captures are returns as a slice of strings; otherwise if no match or no captures then captures will be nil.
func (String) FindAll ¶
FindAll returns the start and end position of the string text found in str.
If str contains a pattern and a match is made with text, the captures are returns as a slice of strings; otherwise if no match or no captures then captures will be nil.
func (String) Gmatch ¶
Gmatch returns an iterator function that, each time it is called, returns the next captures from pattern over the string s. If pattern specifies no captures, then the whole match is produced in each call.
As an example, the following loop will iterate over all the words from string s, printing one per line:
s = "hello world from Lua" for w in string.gmatch(s, "%a+") do print(w) end
The next example collects all pairs key=value from the given string into a table:
t = {} s = "from=world, to=Lua" for k, v in string.gmatch(s, "(%w+)=(%w+)") do t[k] = v end
For this function, a caret '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration.
func (String) Gsub ¶
Gsub returns a copy of text in which all (or the upto limit if > 0) occurrences of the pattern have been replaced by the specified replacer. The name gsub comes from global substitution.
Gsub returns the string with replacements and the number of replacements that occurred. If no matches were made, then text is return unmodified with 0 to indicate that no replacements were made.
func (String) GsubAll ¶
GsubAll returns a copy of text in which all (or the upto limit if > 0) occurrences of the pattern have been replaced by the specified replacer. The name gsub comes from global substitution.
Gsub returns the string with replacements and the number of replacements that occurred. If no matches were made, then text is return unmodified with 0 to indicate that no replacements were made.
func (String) GsubExpr ¶
GsubStr returns a copy of text in which all (or the upto limit if > 0) occurrences of the pattern have been replaced by the specified replacement value. The name gsub comes from global substitution.
The character '%' works as an escape character: any sequence in replace of the form %d, with 1 <= d <= 9, stands for the value of the d-th capture substring. The sequence %0 stands for the whole match. The sequence %% stands for a single escaped %.