Need help with string patterns

can someone explain how these work and give some examples? its a little bit hard to understand

That’s pretty simple.
There is a tutorial for it:

Etc. %d is a class that supports any numeric sequence.
If I do %d* this means I will get something like 0424124

string.match(“hello14214”,“%d”) → 1
But if we use the modifier “*” then it will return 14214.
That’s called regex in other programming languages, is very common, and is used for parsing usually.

"[%d%._]*" -- supports characters: . 1 2 3 4 5 6 7 8 9 0 _
-- / + quantifier
local str = "foobar"
print(str:match("o+")) -- output: oo

-- / - quantifier
local str = "12345"
print(str:match("1234-")) -- output: 123

-- / * quantifier
local str = "1234567890" 
print(str:match("%d*")) -- output: 1234567890

-- / ? quantifier
local str = "color"
print(str:match("colou?r")) -- output: color

The + Quantifier

+ matches 1 or more occurrences of the preceding pattern.

local str = "foobar"
print(str:match("o+")) -- output: oo

Why?

"o+" means: match one or more "o" characters in a row.
The string "foobar" contains "oo".

The - Quantifier (non-greedy matching)

- matches 0 or more occurrences of the preceding pattern,
but as few as possible (non-greedy).

local str = "12345"
print(str:match("1234-")) -- output: 123

Why?

"1234-" means:

Match "1234"
Then match as few characters as possible to still complete a match.

But here’s the trick:

Lua patterns interpret "-" as a quantifier only when used after a pattern, such as:

"%d-" → digits, few as possible
"a-" → “a”, few as possible

In "1234-", the - is applied to the character 4.
So "4-" means:

Match the character "4" zero or one times—but prefer zero (minimize).

Thus "123" is enough to complete the pattern:

The * Quantifier

* matches 0 or more occurrences of the preceding pattern (greedy).

local str = "1234567890" 
print(str:match("%d*")) -- output: 1234567890

Why?

"%d" means any digit.
"%d*" means: match zero or more digits.

  • Lua patterns are greedy → they match as many digits as possible.

The ? Quantifier

? matches 0 or 1 occurrences of the preceding pattern.

local str = "color"
print(str:match("colou?r")) -- output: color

Why?

"u?" means match "u" zero or one time.
"color" has no "u" in that position.

The pattern colou?r can match:

"color" (no “u”)
"colour" (with “u”)

Why would you need to match 0 patterns?

Its very useful if you’re trying to match any amount of whitespace. The pattern that trims strings (cuts off whitespace from both sides) is a good example of it

^%s*(.-)%s*$

Here’s a rundown of each sequence here

^ - string start anchor
%s - match whitespace
  * - match zero or more patterns
( - open capture
  .- - lazy quantifier
) - close capture
%s - match whitespace
  * - match zero or more patterns
$ - string end anchor
1 Like