Skip to content

Instantly share code, notes, and snippets.

@veechs
Forked from spr2-dev/pattern_matching.md
Last active April 18, 2025 03:31
Show Gist options
  • Save veechs/bc40f1f39b30cb1251825f031cd6d978 to your computer and use it in GitHub Desktop.
Save veechs/bc40f1f39b30cb1251825f031cd6d978 to your computer and use it in GitHub Desktop.
A quick beginner guide on Lua pattern matching

Lua Pattern Matching Cheat Sheet

Table Of Contents

  1. What's a pattern?
  2. Escaping
  3. Positional anchors
  4. Character classes
  5. Sets (character ranges)
  6. Quantifiers
  7. Capture groups
  8. Examples
  9. Testing

1. What's a pattern?

A pattern is describes the structure of a string you want to match. They are smilar, but not identitical to regular expressions.

2. Escaping

To match one of the magic characters . ^ $ ( ) % [ ] * + - ?, it must be escaped by prefixing with %. For example, %% will match "%".

3. Positional anchors

Attach the pattern to the ^ beginning or $ end o of the string.

4. Character classes

These match any character within their class.

Class Represents Inverse*
. Any character
%w Alphanumerics (from A to Z, a to z, and from 0 to 9) %W
%a Letters (A to Z and a to z) %A
%l Lowercase letters (a to z) %L
%u Uppercase letters (A to Z) %U
%d Digits (from 0 to 9) %D
%p Punctuation %P
%s Whitespace (" ", tab, line breaks) %S

* The inverse matches any character not in the class. For example, %D matches anything that is not a digit.

5. Sets (character ranges)

Brackets allow you to create a custom character range or set. Examples:

Example Meaning
[ab] "a" OR "b"
[^ab] Anything but "a" or "b"
[a-i] Between "a" and "i" in the alphabet
[6-8] Between "6" and "8"
[a-zA-Z0-9] Between "a" and "z" or between "A" and "Z" or between "0" and "9" (the same as %w)

To match [ or ] literally, prefix it with %.

6. Quantifiers

Specify repetion by placing after any character, class, or range.

Quantifier Matches previous item...
? 0 or 1 times
* 0 to n (infinite) times
+ 1 to n times
- 0 or n times, ungreedy (matches shortest possible sequence)

7. Capture groups

Without capture groups, the whole matching subject string is returned. Sometimes only certain parts are required. Capture groups defines what to return. Multiple capture groups, multiple values are returned. To make a capture group, enclose what you want to return with parentheses. If you want to match the ( or ) char literally, put a "%" before it.

8. Examples

Match a string that...

  • contains "apples" anywhere in it : apples
  • contains "apples" at the start of the string : ^apples
  • contains "apples" at the end of the string : apples$
  • has any 3 characters between "app" and "les" : app...les
  • contains a letter or a digit at any position : %w
  • contains a digit at the end of the string : %d$
  • contains either "duck" or "luck" at any position : [dl]uck
  • does not contain a number between 0 and 4 at the start of the string : ^[^0-4]
  • contains "]" at the end of the string : %]$
  • is either "apple" or "apples" : apples?
  • is "a", or "aa", or "aaa", or "aaaa"... : a+

9. Testing

Use this Lua Pattern Tester to verify your pattern matches an example string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment