Filter URL to get some part

Hi

I am trying to parse a URL to remove all chars before the last slash.

For example, I have this url :

file:///Users/bob/Library/Application%20Support/Corona%20Simulator/Insight_1.3.7-32A8603CB6D6FF75EE2DD19496F67C8E/Caches/about_seasons_13_mybestnews.html

I want to keep :

/about_seasons_13_mybestnews.html

I have made a small parser : 

utils.cleanUrl = function ( urlAbsolute, myChar ) local lg = (string.len(urlAbsolute) -1 ) \* -1 local i = string.find(urlAbsolute, myChar, -35, true) local urlFiltered if i then urlFiltered = string.sub(urlAbsolute, i+1) else urlFiltered = urlAbsolute end return urlFiltered end

There is no bug but if the length of the URL changes too much, it does not work because of the -35 limits. I have put this limit because if I take too much of the string, lua returns the first slash encountered, which is not always the last one.

I can make a work around by parsing the string from the end one char by char but I am sure there is a regular expression to get that result.

Does a regExp expert online ?

Little bit cheating but you could just find a generic split function and use that. If you are doing hundreds of these at the same time it may be better to find a proper regex. 

[lua]

local function split(inputstr, sep)

    if sep == nil then

            sep = “%s”

    end

    local t={} ; local i=1

    for str in string.gmatch(inputstr, “([^”…sep…"]+)") do

            t[i] = str

            i = i + 1

    end

    return t

end

local url = “file:///Users/bob/Library/Application%20Support/Corona%20Simulator/Insight_1.3.7-32A8603CB6D6FF75EE2DD19496F67C8E/Caches/about_seasons_13_mybestnews.html”

local urlParts = split(url, “/”)

print(urlParts[#urlParts])

[/lua]

thanks a lot !

I have also find this : 

function string:split( inSplitPattern, outResults ) if not outResults then outResults = { } end local theStart = 1 local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) while theSplitStart do table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) ) theStart = theSplitEnd + 1 theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) end table.insert( outResults, string.sub( self, theStart ) ) return outResults end local test = urlAbsolue:split("/") print("r\> "..tostring(test[#test]))

wich works great.

(from : http://lua-users.org/wiki/SplitJoin GavinKistner)

Thanks again.

Little bit cheating but you could just find a generic split function and use that. If you are doing hundreds of these at the same time it may be better to find a proper regex. 

[lua]

local function split(inputstr, sep)

    if sep == nil then

            sep = “%s”

    end

    local t={} ; local i=1

    for str in string.gmatch(inputstr, “([^”…sep…"]+)") do

            t[i] = str

            i = i + 1

    end

    return t

end

local url = “file:///Users/bob/Library/Application%20Support/Corona%20Simulator/Insight_1.3.7-32A8603CB6D6FF75EE2DD19496F67C8E/Caches/about_seasons_13_mybestnews.html”

local urlParts = split(url, “/”)

print(urlParts[#urlParts])

[/lua]

thanks a lot !

I have also find this : 

function string:split( inSplitPattern, outResults ) if not outResults then outResults = { } end local theStart = 1 local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) while theSplitStart do table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) ) theStart = theSplitEnd + 1 theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) end table.insert( outResults, string.sub( self, theStart ) ) return outResults end local test = urlAbsolue:split("/") print("r\> "..tostring(test[#test]))

wich works great.

(from : http://lua-users.org/wiki/SplitJoin GavinKistner)

Thanks again.