Lua Regex / Pattern Matching Link to heading
In Lua, Regex is handled through string patterns
The pattern '[_%a][_%w]*'
matches a sequence that: starts with letter or underscore, followed by zero or more underscores or alphanumeric characters.
Character Classes Link to heading
Char | Desc |
---|---|
. | all characters |
%a | letters |
%c | control characters |
%d | digits |
%l | lower case letters |
%p | punctuation characters |
%s | space characters |
%u | upper case letters |
%w | alphanumeric characters |
%x | hexadecimal digits |
%z | the character with representation 0 |
Example Link to heading
-- Ex. 2: Matches a Markdown main header
-- 1. Match # at the beginning of string (^)
-- 2. Followed by white space
-- 3. Followed by any number (*) of:
-- letters, digits, punctuation characters, or white spaces ('% ')
-- 4. New line
local markdown_title = string.match(content, '^# [% %a%x%p]*\n')