Skip to content

regex

Built-in library for regular expressions

local Regex = require("@lune/regex")
local re = Regex.new("(\\w+), (\\w+)!")
if re:isMatch("hello, world!") then
print("Matched!")
end
local caps = re:captures("hello, world! hello, again!")
print(#caps) -- 2
print(caps:get(1).text) -- "hello"
print(caps:get(2).text) -- "world"
print(caps:get(3)) -- nil
function regex.new(pattern: string): Regex

Creates a new Regex from a given string pattern.

Errors

This constructor throws an error if the given pattern is invalid.

Parameters

  • pattern - The string pattern to use

Returns

  • Regex - The new Regex object
function Regex.isMatch(self: Regex, text: string): boolean

Check if the given text matches the regular expression.

This method may be slightly more efficient than calling find if you only need to know if the text matches the pattern.

Parameters

  • text - The text to search

Returns

  • boolean - Whether the text matches the pattern
function Regex.find(self: Regex, text: string): RegexMatch?

Finds the first match in the given text.

Returns nil if no match was found.

Parameters

  • text - The text to search

Returns

  • RegexMatch? - The match object
function Regex.captures(self: Regex, text: string): RegexCaptures?

Finds the first match in the given text, and returns its capture groups as a RegexCaptures object.

Returns nil if no match was found.

Parameters

  • text - The text to search

Returns

  • RegexCaptures? - The captures object
function Regex.split(self: Regex, text: string): { string }

Splits the given text using the regular expression.

Parameters

  • text - The text to split

Returns

  • { string } - The split text
function Regex.replace(self: Regex, haystack: string, replacer: string): string

Replaces the first match in the given text with the given replacer string.

Parameters

  • haystack - The text to search
  • replacer - The string to replace matches with

Returns

  • string - The text with the first match replaced
function Regex.replaceAll(self: Regex, haystack: string, replacer: string): string

Replaces all matches in the given text with the given replacer string.

Parameters

  • haystack - The text to search
  • replacer - The string to replace matches with

Returns

  • string - The text with all matches replaced

A match from a regular expression.

Contains the following values:

  • start – The start index of the match in the original string.
  • finish – The end index of the match in the original string.
  • text – The text that was matched.
  • len – The length of the text that was matched.

Capture groups from a single match of a regular expression.

The length operator (#) returns the number of numbered and named capture groups, not including the full match, which can be retrieved using get(0).

get: (self: RegexCaptures, index: number) -> RegexMatch?

Returns the match for the capture group at the given index, if one exists.

Index 0 is the full match, and index 1 is the first capture group.

Parameters

  • index - The index of the capture group to get

Returns

  • RegexMatch - The match, if one exists
group: (self: RegexCaptures, group: string) -> RegexMatch?

Returns the match for the given named match group, if one exists.

Parameters

  • group - The name of the group to get

Returns

  • RegexMatch - The match, if one exists
format: (self: RegexCaptures, format: string) -> string

Formats the captures using the given format string.

Example usage

local regex = require("@lune/regex")
local re = regex.new("(?<day>[0-9]{2})-(?<month>[0-9]{2})-(?<year>[0-9]{4})")
local caps = re:captures("On 14-03-2010, I became a Tenneessee lamb.");
assert(caps ~= nil, "Example pattern should match example text")
local formatted = caps:format("year=$year, month=$month, day=$day")
print(formatted) -- "year=2010, month=03, day=14"

Parameters

  • format - The format string to use

Returns

  • string - The formatted string