Skip to content

Instantly share code, notes, and snippets.

@spr2-dev
Last active March 9, 2024 19:54
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spr2-dev/46ca9f4a6f933fa266bccd87fd15d09a to your computer and use it in GitHub Desktop.
Save spr2-dev/46ca9f4a6f933fa266bccd87fd15d09a 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. Positional characters
  3. Common matching characters
  4. Ranges, character sets and logic
  5. Quantifiers
  6. Capture groups
  7. Examples

1. What's a pattern ?

A pattern is a string that contains some characters that are recognized and treated by a "regex engine". Patterns are used to match, i.e. check if a "subject string" comply to the pattern.

2. Positional characters

In patterns, there are two positional characters : ^ and $. ^ asserts the start of the subject string. $ asserts the end of the subject string.

3. Common matching characters

  • . : Any character
  • %w : Any character that is alphanumeric (from A to Z and from 0 to 9)
  • %W : Any character that is NOT alphanumeric
  • %d : Any character that is a digit (from 0 to 9)
  • %D : Any character that is NOT a number
  • %s : Any character that is whitespace (" ", tab, line breaks)
  • %S : Any character that is NOT whitespace

4. Ranges, character sets and logic

Inside of a pattern, you can add "ranges" in brackets.

  • [ab] will search for a character that is either "a" OR "b"
  • [^ab] will search for a character that is neither "a" nor "b"
  • [a-i] will search for a character that is between "a" and "i" in the alphabet
  • [6-8] will search for a character that is between "6" and "8"
  • [a-zA-Z0-9] will search for : A character that is between "a" and "z" OR a character that is between "A" and "Z" OR a character that is between "0" and "9" The previous pattern is the same as %w. If you want to match the [ or ] char literally, put a "%" before it.

5. Quantifiers

Quantifiers are special characters that goes after any char, common matching char, range or charset.

  • * will match the previous char 0 to n times.
  • + will match the previous char 1 to n times.
  • ? will match the previous char 0 or 1 times.

6. 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.

7. 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+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment