Finding if a string start with 0 or more spaces followed by a string that might contain metacharacters

52 Views Asked by At

I need to find if a string start with 0 or more spaces followed by a comment string I don't know in advance, so I though of just constructing the pattern:

local pattern = "^(%s" .. comment_string .. ")"
if str:find(pattern) then
-- ...

The problem is that comment_string contains metacharacters most of the time (i.e. for lua I get "--" but I need "%-%-" for the pattern to work). I tried a bunch of things but I can't find a way to make it work. Any idea?

1

There are 1 best solutions below

2
noobyy On
local str = "--test"
local pattern = "^%-%-%s*(.*)$"
local _, _, contents = str:find(pattern)
print(contents)