preg_match

preg_match

am 14.04.2008 11:50:22 von Janice

I want to be able to extract key/value pairs from a string but I am not
succeeding. Experimenting and googling for a few hours didn't get me
anywhere so I'm hoping for help here. My input string could look
something like this:

some_var=yellow another_var = "blue and red" third_var= 'pink'

The values could be enclosed in single quotes, double quotes or no
quotes at all as you can see. Is it possible to make a regular
expression to extract these and if so, how?

Thanks
/MB

Re: preg_match

am 14.04.2008 11:58:53 von Captain Paralytic

On 14 Apr, 09:50, MB wrote:
> I want to be able to extract key/value pairs from a string but I am not
> succeeding. Experimenting and googling for a few hours didn't get me
> anywhere so I'm hoping for help here. My input string could look
> something like this:
>
> some_var=yellow another_var = "blue and red" third_var= 'pink'
>
> The values could be enclosed in single quotes, double quotes or no
> quotes at all as you can see. Is it possible to make a regular
> expression to extract these and if so, how?
>
> Thanks
> /MB

Why not start a bit further back. How dod you end up with such a
string in the first place?

Re: preg_match

am 14.04.2008 12:04:30 von Janice

Captain Paralytic skrev:
> On 14 Apr, 09:50, MB wrote:
>> I want to be able to extract key/value pairs from a string but I am not
>> succeeding. Experimenting and googling for a few hours didn't get me
>> anywhere so I'm hoping for help here. My input string could look
>> something like this:
>>
>> some_var=yellow another_var = "blue and red" third_var= 'pink'
>>
>> The values could be enclosed in single quotes, double quotes or no
>> quotes at all as you can see. Is it possible to make a regular
>> expression to extract these and if so, how?
>>
>> Thanks
>> /MB
>
> Why not start a bit further back. How dod you end up with such a
> string in the first place?

That's something I can't do anything about unfortunatly. I have to live
with how this string looks and just have to solve it somehow.

/MB

Re: preg_match

am 14.04.2008 12:38:26 von pritaeas

"MB" wrote in message
news:yNFMj.6040$R_4.4506@newsb.telia.net...
>I want to be able to extract key/value pairs from a string but I am not
>succeeding. Experimenting and googling for a few hours didn't get me
>anywhere so I'm hoping for help here. My input string could look something
>like this:
>
> some_var=yellow another_var = "blue and red" third_var= 'pink'
>
> The values could be enclosed in single quotes, double quotes or no quotes
> at all as you can see. Is it possible to make a regular expression to
> extract these and if so, how?
>
> Thanks
> /MB

Looks like HTML attributes, so here's a link:

http://haacked.com/archive/2004/10/25/usingregularexpression stomatchhtml.aspx

Re: preg_match

am 14.04.2008 13:14:55 von Alexey Kulentsov

MB wrote:
> I want to be able to extract key/value pairs from a string but I am not
> succeeding. Experimenting and googling for a few hours didn't get me
> anywhere so I'm hoping for help here. My input string could look
> something like this:
>
> some_var=yellow another_var = "blue and red" third_var= 'pink'
>
> The values could be enclosed in single quotes, double quotes or no
> quotes at all as you can see. Is it possible to make a regular
> expression to extract these and if so, how?
>



$a='some_var=yellow another_var = "blue and red" third_var= \'pink\'';

// simple regexp without quoting
preg_match_all('/(\w+) *=
*(\w+|(?:(["\'])([^"\']*)\\3))/',$a,$res,PREG_SET_ORDER);

foreach($res as $r)
{
echo $r[1].' = '.(isset($r[4])?$r[4]:$r[2])."\n";
}

?>

Re: preg_match

am 14.04.2008 13:52:03 von Janice

Alexey Kulentsov skrev:
> MB wrote:
>> I want to be able to extract key/value pairs from a string but I am
>> not succeeding. Experimenting and googling for a few hours didn't get
>> me anywhere so I'm hoping for help here. My input string could look
>> something like this:
>>
>> some_var=yellow another_var = "blue and red" third_var= 'pink'
>>
>> The values could be enclosed in single quotes, double quotes or no
>> quotes at all as you can see. Is it possible to make a regular
>> expression to extract these and if so, how?
>>
>
> >
>
> $a='some_var=yellow another_var = "blue and red" third_var= \'pink\'';
>
> // simple regexp without quoting
> preg_match_all('/(\w+) *=
> *(\w+|(?:(["\'])([^"\']*)\\3))/',$a,$res,PREG_SET_ORDER);
>
> foreach($res as $r)
> {
> echo $r[1].' = '.(isset($r[4])?$r[4]:$r[2])."\n";
> }
>
> ?>

That one works great. Just what i needed. Thanks!

/MB

Re: preg_match

am 14.04.2008 13:53:17 von Janice

pritaeas skrev:
> "MB" wrote in message
> news:yNFMj.6040$R_4.4506@newsb.telia.net...
>> I want to be able to extract key/value pairs from a string but I am not
>> succeeding. Experimenting and googling for a few hours didn't get me
>> anywhere so I'm hoping for help here. My input string could look something
>> like this:
>>
>> some_var=yellow another_var = "blue and red" third_var= 'pink'
>>
>> The values could be enclosed in single quotes, double quotes or no quotes
>> at all as you can see. Is it possible to make a regular expression to
>> extract these and if so, how?
>>
>> Thanks
>> /MB
>
> Looks like HTML attributes, so here's a link:

I should have thought of that. :-)

> http://haacked.com/archive/2004/10/25/usingregularexpression stomatchhtml.aspx

I got that one to work well with some small adjustments. Thanks!

/MB