Search code examples
lualua-patterns

What is multi line pattern in Lua?


I want match multi line

/add val1::val2

and work via this pattern

^/add +(.+)::+(.+)$

but not work in multi line for example:

/add line1
line2
::
linexx
lineYY

tested this pattern but not work

^/add ([%w%s]*)::([%w%s]*)

Solution

  • . matches any character, including \n. Since you don't want to match multi-line strings, exclude \n explicitly, i.e, use [^\n] to replace .:

    ^/add +([^\n]+)::+([^\n]+)$
    

    Test:

    local str1 = '/add val1::val2'
    local str2 = [[/add line1
    line2
    ::
    linexx
    lineYY]]
    
    local pattern = '^/add +([^\n]+)::+([^\n]+)$'
    
    print(str1:match(pattern))
    print(str2:match(pattern))