lpeg.match
Matches a pattern against a string
Prototype
lpeg.match (pattern, subject [, init])
Description
The matching function. It attempts to match the given pattern against the subject string. If the match succeeds, returns the index in the subject of the first character after the match, or the values of captured values (if the pattern captured any value).
An optional numeric argument init makes the match starts at that position in the subject string. As usual in Lua libraries, a negative value counts from the end.
Unlike typical pattern-matching functions, match works only in anchored mode; that is, it tries to match the pattern with a prefix of the given subject string (at position init), not with an arbitrary substring of the subject. So, if we want to find a pattern anywhere in a string, we must either write a loop in Lua or write a pattern that matches anywhere. This second approach is easy and quite efficient.
An optional numeric argument init makes the match starts at that position in the subject string. As usual in Lua libraries, a negative value counts from the end.
Unlike typical pattern-matching functions, match works only in anchored mode; that is, it tries to match the pattern with a prefix of the given subject string (at position init), not with an arbitrary substring of the subject. So, if we want to find a pattern anywhere in a string, we must either write a loop in Lua or write a pattern that matches anywhere. This second approach is easy and quite efficient.
Lua functions
- lpeg.B - Matches patt n characters behind the current position, consuming no input
- lpeg.C - Creates a simple capture
- lpeg.Carg - Creates an argument capture
- lpeg.Cb - Creates a back capture
- lpeg.Cc - Creates a constant capture
- lpeg.Cf - Creates a fold capture
- lpeg.Cg - Creates a group capture
- lpeg.Cmt - Creates a match-time capture
- lpeg.Cp - Creates a position capture
- lpeg.Cs - Creates a substitution capture
- lpeg.Ct - Creates a table capture
- lpeg.locale - Returns a table of patterns matching the current locale
- lpeg.P - Converts a value into a pattern
- lpeg.print - Outputs debugging information to stdout
- lpeg.R - Returns a pattern that matches a range of characters
- lpeg.S - Returns a pattern that matches a set of characters
- lpeg.setmaxstack - Sets the maximum size for the backtrack stack
- lpeg.type - Tests if a value is a pattern
- lpeg.V - Creates a non-terminal variable for a grammar
- lpeg.version - Returns the LPeg version