Search code examples
regexstringluaopenwrtluci

How can i extract values except double quotes in lua


this is a string

"'"id"':001 (it is visualized "id":001)

I want to capture only the values in lua. if there is not double quotes, i can extract values only. (use something:gmatch((%a+)%sd:%s(%d+)))

Is there anyone who solve this problem?


Solution

  • You may use a "(%w+)"%s*:%s*(%d+) pattern:

    local example = [[ "id":001 "id2":002 ]]
    for i,y in example:gmatch([["(%w+)"%s*:%s*(%d+)]]) do
      print(i, y)
    end
    

    See the Lua demo, output:

    id  001
    id2 002
    

    The "(%w+)"%s*:%s*(%d+) pattern matches

    • " - a double quote
    • (%w+) - Group 1: one or more alphanumeric chars (use [%w_]+ to also match _)
    • " - a "
    • %s*:%s* - a colon enclosed with 0+ whitespaces
    • (%d+) - Group 2: one or more digits