var1: val1 var2: val2

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

var1: val1 var2: val2

Post by nwp »

I need a regex To make an array of vars and vals from "var1: val1 var2: val2 :........."
I was trying this

Code: Select all

<?php
$string = "var1: val1 var2: val2";
header("Content-Type: text/plain");
preg_match("/^(\w+):[\s]*(\w+)/", $string, $matches);
print_r($matches);
?>
Browser wrote:Array
(
[0] => var1: val1
[1] => var1
[2] => val1
)
But What about the var2 and val2 and others !
How can I do it ??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg_match_all().

edit: also be aware of the characters \w matches. It's probably more than you think.
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

I tried preg_match_all() But it didn't worked also

Code: Select all

<?php
$string = "var1: val1 var2: val2";
header("Content-Type: text/plain");
preg_match_all("/^(\w+):[\s]*(\w+)/", $string, $matches, PREG_SET_ORDER);
print_r($matches);
?>
Browser wrote:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => var1: val1
            [1] => var1
            [2] => val1
        )

)
Its Not Matching Var2 or var3
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your pattern requires adjustment.
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

feyd wrote:Your pattern requires adjustment.
sorry i didn't understand. please explain . and tell me a solution.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You must adjust the pattern so it is able to find more than one solution. Currently, your pattern will only allow a single solution given the test string.
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

feyd wrote:You must adjust the pattern so it is able to find more than one solution. Currently, your pattern will only allow a single solution given the test string.
But How ??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Try stuff. Do you understand what each of the metacharacters do in the pattern?
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

feyd wrote:Try stuff. Do you understand what each of the metacharacters do in the pattern?
No Please show me an example how i can solve my problem
wildwobby
Forum Commoner
Posts: 66
Joined: Sat Jul 01, 2006 8:35 pm

Post by wildwobby »

im a n00b, but I think the ^ indicates it is at the start of a string, therfore only the first one will match
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It appears you're trying to parse JSON. If you are, there are libraries available for this.
nwp
Forum Contributor
Posts: 105
Joined: Sun Feb 04, 2007 12:25 pm

Post by nwp »

d11wtq wrote:It appears you're trying to parse JSON. If you are, there are libraries available for this.
Really ??
Actually I was going to build it by myself cause i was thinking that there are no librery for jason in php .
So Would You So an Example ??
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

check the bottom of http://json.org/ for an exhaustive list

also, as of php 5.2:
http://php.net/json
Post Reply