Get part of a string

2

I'm fetching data from a file in ics format. The problem is that the file may change several times. I have this code to by in each variable each die of a certain line of the file. Example:

     for line in f:lines() do 
       f line:sub(1,5) == "RRULE" then --objet
            rule = line

            freq = string.match(rule,"FREQ=(.*);")
            until_ = string.match(rule,"UNTIL=(.*)")
            interval = string.match(rule,"INTERVAL=(.*)")
            count = string.match(rule,"COUNT=(.*)")
        end
      end

And here are the various examples of the file line I can get:

RRULE:FREQ=DAILY;INTERVAL=10;COUNT=5

RRULE:FREQ=DAILY;INTERVAL=2

RRULE:FREQ=DAILY;UNTIL=19971224T000000Z

How can I get to by in each different variable?

    
asked by anonymous 17.08.2015 / 17:33

1 answer

1

The problem with your code is the use of .* , which is a greedy pattern, that is, get everything from that point on.

Here is a robust way to grab all the fields on a line, whatever the order in which they appear:

for line in f:lines() do 
    if line:sub(1,5) == "RRULE" then
        local t={}
        line=line..";"
        for k,v in line:gmatch("(%w-)=(.-);") do
            t[k]=v
        end
        -- check that we have parsed the line correctly
        print(line)
        for k,v in pairs(t) do
            print(k,v)
        end
    end
end
    
19.08.2015 / 04:32