A Wildstar LUA Library for python-like Regular Expressions
##Usage
local RegExp = Apollo.GetPackage("RegExpUtils").tPackage
local regex = RegExp.compile("\\w+")
for match in regex:finditer("Hello, World!") do
print(match:group(0))
end
##Reference
###RegExp.compile(regex, [flags]) Creates a new Regular Expresion object with the regex and optional flags.
#####Returns: RegExp object Example:
local regex = RegExp.compile("\\w+")
###RegExp.match(regex, str, [pos], [flags]) Returns all group matches a string contains of the regular expression
#####Parameters:
name | type | required | description |
---|---|---|---|
regex | string or RegExp object | Yes | Regular Expression to match |
str | string | Yes | String to test |
pos | number | No | Starting position in string (default: 1) |
flags | string | No | Regular Expression flags to use |
Note: If position is negative, the the match starts that number of characters from the end of the string
#####Returns: RegExpMatch object Example:
local regex = RegExp.compile("([0-9]+)")
local matches = RegExp.match(regex, "TestString 123124122 12312312")
print(matches:group(1))
Results:
123124122
###RegExp.search(regex, str, [pos], [flags]) Tests if a string contains a Regular Expression match
#####Parameters:
name | type | required | description |
---|---|---|---|
regex | string or RegExp object | Yes | Regular Expression to match |
str | string | Yes | String to test |
pos | number | No | Starting position in string (default: 1) |
flags | string | No | Regular Expression flags to use |
Note: If position is negative, the the match starts that number of characters from the end of the string
#####Returns: RegExpMatch object or nil Example:
local regex = RegExp.compile("[0-9]+")
local test1 = RegExp.search(regex, "TestString")
local test2 = RegExp.search(regex, "123124122")
print(tostring(test1))
print(tostring(test2))
Result:
[RegExpMatch object]
nil
###RegExp.sub(regex, repl, str, [count], [flags]) Performs pattern substitution of a string using a regular expressions. This will also perform group replacement and insertion
#####Parameters:
name | type | required | description |
---|---|---|---|
regex | string or RegExp object | Yes | Regular Expression to match |
repl | string | Yes | String replacement pattern |
str | string | Yes | String to perform replacements |
count | number | No | The number of replacements to make (default: all) |
flags | string | No | Regular Expression flags to use |
Note: If position is negative, the the match starts that number of characters from the end of the string
###RegExp.findall(regex, str, [pos], [flags]) Finds all matches of a specific Regular Expression contains in a string.
#####Parameters:
name | type | required | description |
---|---|---|---|
regex | string or RegExp object | Yes | Regular Expression to match |
str | string | Yes | String to test |
pos | number | No | Starting position in string (default: 1) |
flags | string | No | Regular Expression flags to use |
Note: If position is negative, the the match starts that number of characters from the end of the string
#####Returns: table of strings or nil
###RegExp.finditer(regex, str, [pos], [flags]) Finds the first match of a specific Regular Expression contains in a string and returns an iterator object that can be iterated through all matches
#####Parameters:
name | type | required | description |
---|---|---|---|
regex | string or RegExp object | Yes | Regular Expression to match |
str | string | Yes | String to test |
pos | number | No | Starting position in string (default: 1) |
flags | string | No | Regular Expression flags to use |
Note: If position is negative, the the match starts that number of characters from the end of the string
#####Returns: RegExpMatch object, RegExp object, match or nil
###RegExpMatch:group(...) Returns a match or group of matches that matched a submatch of a regular expression.
#####Returns: strings or nil
Example:
/(a)(b)(c)/
matching "abc", then
group(0,1,2,3)
returns "abc", "a", "b", "c"group()
is equivalent togroup(0)
###RegExpMatch:span(groupId) Returns the begining and end positions of a match within a string based on the "groupId".
#####Returns: numbers(begining,end) or nil
##Regular Expression Syntax or-exp:
- pair-exp
- or-exp "|" pair-exp
pair-exp:
- repeat-exp_opt
- pair-exp repeat-exp
repeat-exp:
- primary-exp
- repeat-exp repeater
- repeat-exp repeater "?"
primary-exp:
- "(?:" or-exp ")"
- "(?P<" identifier ">" or-exp ")"
- "(?P=" name ")"
- "(?=" or-exp ")"
- "(?!" or-exp ")"
- "(?<=" or-exp ")"
- "(?<!" or-exp ")"
- "(?(" name ")" pair-exp "|" pair-exp ")"
- "(?(" name ")" pair-exp ")"
- "(" or-exp ")"
- char-class
- non-terminal
- terminal-str
repeater:
- "*"
- "+"
- "?"
- "{" number_opt "," number_opt "}"
- "{" number "}"
char-class:
- "[^" _user-char-class_ "]"
- "[" user-char-class "]"
user-char-class:
- user-char-range
- user-char-class user-char-range
user-char-range:
- user-char "-" user-char_opt
- user-char
user-char:
- class-escape-sequence
- CHARACTER OTHER THAN ", ]"
class-escape-sequence:
- term-escape-sequence
- "\b"
terminal-str:
- terminal
- terminal-str terminal
terminal:
- term-escape-sequence
- CHARACTER OTHER THAN "^, $, , |, [, ], {, }, (, ), *, +, ?"
term-escape-sequence:
- "\a"
- "\f"
- "\n"
- "\r"
- "\t"
- "\v"
- "\"
- "" ascii-puncuation-char
- "\x" hex-number
non-terminal:
- "^"
- "$"
- "."
- "\d"
- "\D"
- "\s"
- "\S"
- "\w"
- "\W"
- "\A"
- "\b"
- "\B"
- "\Z"
- "" number
name:
- identifier
- number
number:
- STRING THAT MATCHES REGEX
/[0-9]+/
identifier:
- STRING THAT MATCHES REGEX
/[A-Za-z_][A-Za-z_0-9]*/
ascii-puncuation-char:
- CHAR THAT MATCHES REGEX
/[!-~]/
and also/[^A-Za-z0-9]/
hex-number:
- STRING THAT MATCHES REGEX
/[0-9A-Fa-f]{1,2}/
Licensed under CC Attribution-NonCommercial-ShareAlike 4.0