Search code examples
lualua-patterns

I can not make the variable "v" receive any characters while the variable "k" receives only alphanumeric


Could someone help me with this?

I can not make the variable "v" receive any characters while the variable "k" receives only alphanumeric.

t = {}     
for k, v in string.gmatch(decrypt, "(%w+)=([^']*)") do
  t[k] = v
print(k,v)
end 

I have a file with the following content:

email=mbw@iue.com
ip=192.168.100.1
mac=af:45:t6:45:67

Solution

  • (maybe oversimplifying, if so, sorry...)
    If you are trying to break the lines in your file at the '=' an assign them as key, value pairs in t:

    --
    -- PART I - read from a file
    --
    local file = "pattern.dat"                     -- our data file
    local t = {}                                   -- hold file values
    for l in io.lines(file) do                     -- get one line at a time
        local k, v = string.match(l, "(.+)=(.+)")  -- key, value delimited by '=''
        t[k] = v                                   -- save in table
        print(k,t[k])
    end
    
    print("\n\n")
    
    --
    -- PART II - read from data string
    --
    local data = "email=mbw@iue.com/ip=192.168.100.1/mac=af:45:t6:45:67"
    data = data .. "/"                              -- need a trailing '/'
    t = {}                                          -- hold data values
    for l in string.gmatch(data, "(.-)/") do        -- get one 'line' at a time
        local k,v = string.match(l, "(.+)=(.+)")    -- key, value delimited by '=''
        t[k] = v
        print(k,t[k])
    end
    

    NOTE about the "^" anchor (from reference manual entry for 'gmatch'):

    For this function, a caret '^' at the start of a pattern does not work as an anchor, as this would prevent the iteration. http://www.lua.org/manual/5.3/manual.html#pdf-string.gmatch