Search code examples
lualua-patterns

Make a pattern in lua that match one parenthese and string


I want to make a pattern that matches strings like (figure.

I tried

string.find("See this example (figure 1), "%(%figure$")

But it doesn't work.


Solution

  • Your %(%figure$ pattern is invalid, it throws

    missing '[' after '%f' in pattern
    

    because %f defines a frontier pattern.

    You may use

    string.match("See this example (figure 1)", "%((figure%s*%d+)%)")
    

    See Lua demo online

    Details

    • %( - a ( char
    • (figure%s*%d+) - Capturing group (this value will be the output of string.match): figure, zero or more whitespaces (%s*) and then 1+ digits (%d+)
    • %) - a ) char