Pattern builder for Lua

View the Project on GitHub daelvn/lbuilder


lbuilder:particle:literal (l)

Matches the string passed literally. Use lbuilder.l to create one.

-- Anonymous literal
local literal = lbuilder.l "Hello"
-- Named literal
local namedLiteral = lbuilder.l ("example:hello", "Hello")
-- Short naming
local shortNamedLiteral = lbuilder.l "Hello" ["example:hello"]

Since 1.1, this will escape all magic characters

As of 1.3, all particles can be named with indexing

lbuilder:particle:normal (n)

Matches the string passed as a pattern. Use lbuilder.n to create one.

lbuilder:particle:set (s)

Creates a set of characters, supports ranges. Use lbuilder.s to create one.

-- Anonymous sets
local vowels   = lbuilder.s "aeiou"
local alphabet = lbuilder.s "a-z"
-- Named sets
local _alphabet = lbuilder.s ("alphabet", "a-z")
-- Negated sets
local notAlphabet = lbuilder.s "a-z" (true)

lbuilder:particle:capture (c/C)

Creates a capture, supports ranges. Use lbuilder.c to create one.

-- Anonymous captures
local cvowels = lbuilder.c (vowels)
-- Named captures
local calphabet = lbuilder.c ("alphabet", alphabet)

You can optionally use lbuilder.C to create a capture of a particle.


+ operator

l + l

Joins two literals or normals. l+l => ll

s + s

Joins two sets. s+s => [ss]

/ operator


Repeats the particle n times. p/3 => ppp


Repeats the literal no more than n times. p/-3 => ppp[^p]


Match the set once or more, as much as possible. p/'+' => p+

Only works for literals and sets.


Match the set zero or more, as less as possible. p/'-' => p- Only works for literals and sets.


Optionally match the set. p/'?' => p? Only works for literals and sets.


Match the set zero or more, as much as possible. p/'*' => p*

Only works for literals and sets

- operator


Negates a set. -s => [^s]

# operator


Creates a pattern from the literal. #l => P (l)