strings

package
v0.0.0-...-4ee7f28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2018 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Find

func Find(text, expr string) (captures []int)

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

func FindAll(text, expr string, limit int) (captures [][]int)

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

func Gmatch(text, expr string, iter func([]string))

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

func Gsub(text, expr string, replacer Replacer) (repl string, count int)

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

func GsubAll(text, expr string, replacer Replacer, limit int) (repl string, count int)

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

func GsubFunc(text, expr string, replace func(string) string) (string, int)

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

func GsubFuncAll(text, expr string, replace func(string) string, limit int) (string, int)

GsubFuncAll is just like GsubFunc except that upto limit matches are replaced.

func GsubMap

func GsubMap(text, expr string, vars map[string]string) (string, int)

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

func GsubMapAll(text, expr string, vars map[string]string, limit int) (string, int)

GsubMapAll is just like GsubMax upto limit matches are replaced.

func GsubStr

func GsubStr(text, expr, replace string) (repl string, count int)

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

func GsubStrAll(text, expr, replace string, limit int) (repl string, count int)

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

func Match

func Match(text, expr string) (captures []string)

Match returns the first match found in str. If str is a pattern that specifies captures, the captures are returned as well; otherwise "" if no match was made and nil if no captures captured.

func MatchAll

func MatchAll(text, expr string, limit int) (captures [][]string)

MatchAll returns the first match found in str. If str is a pattern that specifies captures, the captures are returned as well; otherwise "" if no match was made and nil if no captures captured.

Types

type Replacer

type Replacer interface {
	Replace(string) string
}

type String

type String string

String is a wrapper type that implements various string operations.

func (String) Find

func (str String) Find(text string) []int

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

func (str String) FindAll(text string, limit int) [][]int

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

func (str String) Gmatch(text string, iter func([]string))

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

func (str String) Gsub(text string, replacer Replacer) (string, int)

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

func (str String) GsubAll(text string, replacer Replacer, limit int) (repl string, count int)

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

func (str String) GsubExpr(text, replace string) (repl string, count int)

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 (String) Match

func (str String) Match(text string) (captures []string)

Match returns the first match found in str. If str is a pattern that specifies captures, the captures are returned as well; otherwise "" if no match was made and nil if no captures captured.

func (String) MatchAll

func (str String) MatchAll(text string, limit int) (captures [][]string)

MatchAll returns the first match found in str. If str is a pattern that specifies captures, the captures are returned as well; otherwise "" if no match was made and nil if no captures captured.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL