Reputation: 3113
So here's a trivial example, say I have an equation like
local equation = "((5*2)+3)-(3^5)"
And I want to capture "((5*2)+3)"
At first I tried
equation:match("%((.*)%)")
But *
is greedy, so it captured the whole equation.
Then I tried
equation:match("%((.-)%)")
But -
is lazy, and it captured "((5*2)"
Obviously I'm going about this wrong. How should I capture the brackets with a string pattern?
Upvotes: 2
Views: 175
Reputation: 122383
What you are looking for is %b
pattern, which matches balanced strings. In this example, you can use %b()
to match a string that starts with (
, and ends with the corresponding )
:
equation:match("%b()")
Upvotes: 5