List Info

Thread: PHP Array regexp.




PHP Array regexp.
country flaguser name
United States
2007-07-31 09:24:00
Hello,

i need regexpresion that will match from this string:
$ARRAY_NAME['exp_one']['exp_two'] = 'text';

matches:
 1 - exp_one
 2 - exp_two
 3 - text

but sometimes in string can be 3 dimension array and regexp
should NOT
match this:

$ARRAY_NAME['exp_one']['exp_two']['exp_three'] = 'text';

And another thing with quotes, it could be ' or " :

$ARRAY_NAME['exp_one']['exp_two'] = "text";
$ARRAY_NAME["exp_one"]['exp_two'] = 'text';
$ARRAY_NAME["exp_one"]["exp_two"] =
"text";

any idea how build regexp for this?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: PHP Array regexp.
country flaguser name
United States
2007-07-31 10:22:20
Try this

$ARRAY_NAMEs*[(['"])(.*?)1]s*[(["'])(.*)?3
]s*=s*(["'])(.*)
5s*;

It will place the desired values in groups 2 4 and 6 instead
of 1,2,3
because the quotes need to be in a capture group for
back-reference
matching.

On Jul 31, 8:24 am, KooT <Rafal...gmail.com> wrote:
> Hello,
>
> i need regexpresion that will match from this string:
> $ARRAY_NAME['exp_one']['exp_two'] = 'text';
>
> matches:
>  1 - exp_one
>  2 - exp_two
>  3 - text
>
> but sometimes in string can be 3 dimension array and
regexp should NOT
> match this:
>
> $ARRAY_NAME['exp_one']['exp_two']['exp_three'] =
'text';
>
> And another thing with quotes, it could be ' or "
:
>
> $ARRAY_NAME['exp_one']['exp_two'] = "text";
> $ARRAY_NAME["exp_one"]['exp_two'] = 'text';
> $ARRAY_NAME["exp_one"]["exp_two"] =
"text";
>
> any idea how build regexp for this?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: PHP Array regexp.
country flaguser name
United States
2007-07-31 14:48:02
There was a mistake in my last post.  Here's the right
expression:
$ARRAY_NAMEs*[(['"])(.*?)1]s*[(["'])(.*?)3
]s*=s*(["'])(.*)
5s*;

On Jul 31, 9:22 am, russ <black.russ...gmail.com> wrote:
> Try this
>
>
$ARRAY_NAMEs*[(['"])(.*?)1]s*[(["'])(.*)?3
]s*=s*(["'])(.*)
> 5s*;
>
> It will place the desired values in groups 2 4 and 6
instead of 1,2,3
> because the quotes need to be in a capture group for
back-reference
> matching.
>
> On Jul 31, 8:24 am, KooT <Rafal...gmail.com> wrote:
>
> > Hello,
>
> > i need regexpresion that will match from this
string:
> > $ARRAY_NAME['exp_one']['exp_two'] = 'text';
>
> > matches:
> >  1 - exp_one
> >  2 - exp_two
> >  3 - text
>
> > but sometimes in string can be 3 dimension array
and regexp should NOT
> > match this:
>
> > $ARRAY_NAME['exp_one']['exp_two']['exp_three'] =
'text';
>
> > And another thing with quotes, it could be ' or
" :
>
> > $ARRAY_NAME['exp_one']['exp_two'] =
"text";
> > $ARRAY_NAME["exp_one"]['exp_two'] =
'text';
> >
$ARRAY_NAME["exp_one"]["exp_two"] =
"text";
>
> > any idea how build regexp for this?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: PHP Array regexp.
country flaguser name
United States
2007-07-31 16:02:26
On 31 Lip, 21:48, russ <black.russ...gmail.com> wrote:
> There was a mistake in my last post.  Here's the right
expression:
>
$ARRAY_NAMEs*[(['"])(.*?)1]s*[(["'])(.*?)3
]s*=s*(["'])(.*)
> 5s*;
>
Thank You very much for Your help. I have wrote some test
program i
perl, and its not working:

#!/usr/bin/perl

$string = "$TEST['blada']['marmolada'] = 'test'";
$string_false =
"$DUPA['blada']['marmolada']['czekolada'] =
'test'";

$string =~
/$TESTs*[(['"])(.*?)1]s*[(["'])(.*?)3]s*
=s*(["'])
(.*)5s*;/;
print( "1) Match 1: $1, match 2: $2, match 3:
$3n");

$string_false =~
/$TESTs*[(['"])(.*?)1]s*[(["'])(.*?)3]s*
=
s*(["'])(.*)s5s*;/;
print( "2) Match 1: $1, match 2: $2, match 3:
$3n");


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: PHP Array regexp.
country flaguser name
United States
2007-07-31 16:22:42
On 31 Lip, 21:48, russ <black.russ...gmail.com> wrote:
> There was a mistake in my last post.  Here's the right
expression:
>
$ARRAY_NAMEs*[(['"])(.*?)1]s*[(["'])(.*?)3
]s*=s*(["'])(.*)
> 5s*;
>

Thanks a lot!
Its working!

Sample perl program to test it:

#!/usr/bin/perl

$string = "$TEST['blada']['marmolada'] =
'test';";
$string_false =
"$TEST['blada2']['marmolada2']['czekolada2'] =
'test';";


if($string =~
/$TESTs*[(['"])(.*?)1]s*[(["'])(.*?)3]s*
=
s*(["'])(.*)5s*;/)
{
        print( "1) Tested string: $stringn" );
        print( "1) Match 1: $1, match 2: $2, match 3:
$3, match 4: $4,
match 5: $5, match 6: $6n");
}
else
{
        print( "1) Not matchedn" );
}

if($string_false =~
/$TESTs*[(['"])(.*?)1]s*[(["'])(.*?)3]s*
=
s*(["'])(.*)s5s*;/)
{
        print( "2) Tested string: $string_falsen"
);
        print( "2) Match 1: $1, match 2: $2, match 3:
$3, match 4: $4,
match 5: $5, match 6: $6n");
}
else
{
        print( "2) Not matchedn");
}
--
RK


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: PHP Array regexp.
country flaguser name
United States
2007-08-01 01:55:59
On 31 Lip, 21:48, russ <black.russ...gmail.com> wrote:
> There was a mistake in my last post.  Here's the right
expression:
>
$ARRAY_NAMEs*[(['"])(.*?)1]s*[(["'])(.*?)3
]s*=s*(["'])(.*)
> 5s*;


Strange. In perl its work fine but in PHP not.

string in $line:

$SERVICE_LANG['IMPREZY']['err_opis']['b']='Opisz
wydarzenie';

preg_match('/$SERVICE_LANGs*[(['"])(.*?)1]s*[([
"'])(.*?)3]
s*=s*(["'])(.*)5s*;/',$line,$matches)

matches array:

[0] => $SERVICE_LANG['IMPREZY']['err_opis']['b']='Opisz
wydarzenie';
[1] => '
[2] => IMPREZY
[3] => '
[4] => err_opis']['b
[5] => '
[6] => Opisz wydarzenie


4 element seems to be wrong :(


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: PHP Array regexp.
country flaguser name
United States
2007-08-01 09:29:39
Hi,

I changed the expression a bit.
#!/usr/bin/perl

$string = "$TEST['blada']['marmolada'] =
'test';";
$string_false =
"$TEST['blada2']['marmolada2']['czekolada2'] =
'test';";

if($string =~
/$TESTs*[['"]([^'"]+)['"]]s*[['"](
[^'"]+)['"]]s*
=s*['"]([^'"]+)['"];/)

{
        print( "1) Tested string: $stringn" );
        print( "1) Match 1: $1, match 2: $2, match 3:
$3, match 4: $4,
match 5: $5, match 6: $6n");
}

else
{
        print( "1) Not matchedn" );
}

if($string_false =~
/$TESTs*[['"]([^'"]+)['"]]s*[['"](
[^'"]+)['"]
]s*[['"]([^'"]+)['"]]s*=s*['"]([^
'"]+)['"];/)
{
        print( "2) Tested string: $string_falsen"
);
        print( "2) Match 1: $1, match 2: $2, match 3:
$3, match 4: $4,
match 5: $5, match 6: $6n");
}

else
{
        print( "2) Not matchedn");
}

The output reads:
1) Tested string: $TEST['blada']['marmolada'] = 'test';
1) Match 1: blada, match 2: marmolada, match 3: test, match
4: ,
match 5: , match 6:
2) Tested string:
$TEST['blada2']['marmolada2']['czekolada2'] =
'test';
2) Match 1: blada2, match 2: marmolada2, match 3:
czekolada2, match 4:
test,
match 5: , match 6:

To match each exp_one, I have used: ['exp_one']
s*[['"]([^'"]+)['"]]
s*
To match 'text', I have used:      
['"]([^'"]+)['"]

I avoid using the .* to do a non-greedy match in your
expression.
Do let me know if this works in PHP.

Thanks
Syd
P.S. Must read "Mastering Regular Expressions - Jeffrey
Friedl"

On Aug 1, 11:55 am, KooT <Rafal...gmail.com> wrote:
> On 31 Lip, 21:48, russ <black.russ...gmail.com> wrote:
>
> > There was a mistake in my last post.  Here's the
right expression:
> >
$ARRAY_NAMEs*[(['"])(.*?)1]s*[(["'])(.*?)3
]s*=s*(["'])(.*)
> > 5s*;
>
> Strange. In perl its work fine but in PHP not.
>
> string in $line:
>
> $SERVICE_LANG['IMPREZY']['err_opis']['b']='Opisz
wydarzenie';
>
>
preg_match('/$SERVICE_LANGs*[(['"])(.*?)1]s*[([
"'])(.*?)3]
> s*=s*(["'])(.*)5s*;/',$line,$matches)
>
> matches array:
>
> [0] =>
$SERVICE_LANG['IMPREZY']['err_opis']['b']='Opisz
wydarzenie';
> [1] => '
> [2] => IMPREZY
> [3] => '
> [4] => err_opis']['b
> [5] => '
> [6] => Opisz wydarzenie
>
> 4 element seems to be wrong :(


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: PHP Array regexp.
country flaguser name
United States
2007-08-06 08:03:27


On 1 Sie, 16:29, "sydc...gmail.com"
<sydc...gmail.com> wrote:
[...]
> Do let me know if this works in PHP.

Yes, its working. Thank You a lot for Your help! 

> P.S. Must read "Mastering Regular Expressions -
Jeffrey Friedl"

I will read it, thanks!

--
RK


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: PHP Array regexp.
country flaguser name
United States
2007-08-07 14:39:31
> To match 'text', I have used:      
['"]([^'"]+)['"]

Helo again 

What if in text is 'sdsd'sdsdsd', or
"erer"rerer" ?

$ARRAY['one']['two'] = 'www'wwww';
$ARRAY['one']['two'] = "www"wwww";

What is the best regexp for this?

--
RK


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: PHP Array regexp.
country flaguser name
United States
2007-08-07 15:08:44
There are a couple of adjustments you can make.  The first
one is to
change the last ['"] to be a back reference, otherwise
you won't match
['this"]correctly']

Here's the Java regex way to do back references

(['"])(.*?)1

Now to handle escaped quotes, you can do a negative
look-behind like
this (Again, the is a Java regex, perl probably has a
different
syntax)

(['"])(.*?)1(?<!\['"])

This will handle

["stuff"like'this"]

But it won't handle
["this\"]
because of the two back slashes.

You're beginning to reach the limits of what a regex can do
at this
point.


On Aug 7, 1:39 pm, KooT <Rafal...gmail.com> wrote:
> > To match 'text', I have used:      
['"]([^'"]+)['"]
>
> Helo again 
>
> What if in text is 'sdsd'sdsdsd', or
"erer"rerer" ?
>
> $ARRAY['one']['two'] = 'www'wwww';
> $ARRAY['one']['two'] = "www"wwww";
>
> What is the best regexp for this?
>
> --
> RK


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


[1-10] [11-12]

about | contact  Other archives ( Real Estate discussion Medical topics )