[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Regex expert needed



On Tue, May 03, 2005 at 09:50:08AM +0100, michael wrote:
> On Mon, 2005-05-02 at 16:08 +0100, Alan Chandler wrote:
> 
> > For instance I want the parse this string
> > 
> > Form = 'This is a bad bad thing, but not as bad as it would be if \"x = 5\", 
> > so thats it',Connection=5
> > 
> > Into the strings
> > 
> > Form
> > 
> > =
> > 
> > 'This is a bad bad thing, but not as bad as it would be if \"x=5\", so thats 
> > it'
> > 
> > ,
> > 
> > Connection
> > 
> > =
> > 
> > 5
> > 
> > using the php preg_split() finction.  So, I first want to find a regex that 
> > finds the commas I have split out (bit not the common in the quoted string, 
> > and then for each substring put that through another version of preg_split() 
> > which finds the = as delimeters.  Again in the quoted string, I want to 
> > ignore the x=5 equal sign.
> > 
> > I actually did this in the end by the brute force method of scanning the 
> > string character by character in php and using logic to determine where the 
> > boundaries are.  Its just a regex ought to be much more elegant.
> 
> Would be yet harder still if you correct your grammar and put the
> missing apostrophe in penultimate word, viz "that's"

If the commas separating the key=value pairs were ampersands instead, 
you could use

	parse_str($string)

as long as there were no ampersands within the value strings.  Could 
you get the original string coming in with ampersands?

Or if there were no commas inside the value strings, you could use

	str_replace(',',&,$string)

but - at least in your example - there are.

and you'd need a 

	stripslashes($string)

to remove those backslashes afterwards.


David

-- 
David Jardine

"Running Debian GNU/Linux and
loving every minute of it."  -L. von Sacher-M.(1835-1895)



Reply to: