PDA

View Full Version : string.find Help



harleydude
October 12th, 2006, 02:34 PM
I have been trying to figure this out for a couple of days now.

I am trying to get particular field out of the following strings

Content-Type: text/plain; charset=iso-8859-1
Content-Type: text/plain
Content-Type: text/plain;

All I want is to determine if the content type is text/plain or text/html and return what is found. Can someone help me determine what pattern string to provide?

Thanks

theguywiththefunnyhair
October 12th, 2006, 03:22 PM
Hi harleydude,
try


search_in = "yourstring"
_,_, type, found = string.find(search_in, "^Content-Type: text/(%a-);(.-)$")
if not type then
_,_, type = string.find(search_in, "^Content-Type: text/(%a-)$")
end


the first search should return type and found for the 1st and last string patterns you provided and the second will give the type for the middle one

i didnt test it but i think it should work

Cheers

Dan

harleydude
October 12th, 2006, 03:32 PM
Thanks, but it returns nil for both type and found.

Rick

harleydude
October 12th, 2006, 03:33 PM
Here is the code I used

local search_in = "Content-Type: text/plain; charset=iso-8859-1"
local _,_, type, found = string.find(search_in, "^Content-Type: text/(%a-);(.-)$")
if not type then
local _,_, type = string.find(search_in, "^Content-Type: text/(%a-)$")
end
print (type, found)

harleydude
October 12th, 2006, 04:56 PM
Finally got it.

local search_in = "Content-Type: text/plain; charset=iso-8859-1"
local _,_, type, found = string.find(search_in, ":%s?(.+);(.-)")
if not type then
_,_, type = string.find(search_in, ":%s?(.+)")
end

theguywiththefunnyhair
October 12th, 2006, 11:02 PM
s w e e t !