preg_match()

preg_match()

am 09.09.2009 18:18:23 von Jan Reiter

------=_NextPart_000_0153_01CA3179.EAEE73A0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

Good Afternoon.



This shouldn't be too complicated, but I can't come up with a solution for
the problem right now. It's about RegEx in PHP (5.2)



Is there a way to capture ALL sub elements of an expression like
preg_match('@a(?[0-9])*b@' ,"a2345678b" , $matches); ??

This would produce (below) whereas I'd like to get all the elements (2-8) as
an array entry each . ([1]=>2, [2]=>3 .)



Result:

array(2) {

[0]=>

array(1) {

[0]=>

string(9) "a2345678b"

}

[1]=>

array(1) {

[0]=>

string(1) "8"

}

}



preg_match_all doesn't do the trick.



The Expression I want to use is



@prim_states[\s]*\((?[0-9\s]*)(prim_state[\s]*\((?[a-fA-F0-9\s]*)
tex_idxs[\s]*\(([0-9\s]*)\)([0-9\s]*)\)[\s]*)*\)@



as I'm trying to parse a 3d Model File, that contains data in the form of



prim_states ( 51

prim_state ( 00000000 2

tex_idxs ( 1 2 ) 0 3 0 0 1

)

prim_state ( 00000000 3

tex_idxs ( 1 2 ) 0 0 1 0 1

)

prim_state ( 00000000 3

tex_idxs ( 1 2 ) 0 4 1 0 1

)

[.]

)



Thank You!



Regards,

Jan








------=_NextPart_000_0153_01CA3179.EAEE73A0--

Re: preg_match()

am 09.09.2009 18:21:03 von Ashley Sheridan

On Wed, 2009-09-09 at 18:18 +0200, Jan Reiter wrote:
> Good Afternoon.
>
>
>
> This shouldn't be too complicated, but I can't come up with a solution for
> the problem right now. It's about RegEx in PHP (5.2)
>
>
>
> Is there a way to capture ALL sub elements of an expression like
> preg_match('@a(?[0-9])*b@' ,"a2345678b" , $matches); ??
>
> This would produce (below) whereas I'd like to get all the elements (2-8) as
> an array entry each . ([1]=>2, [2]=>3 .)
>
>
>
> Result:
>
> array(2) {
>
> [0]=>
>
> array(1) {
>
> [0]=>
>
> string(9) "a2345678b"
>
> }
>
> [1]=>
>
> array(1) {
>
> [0]=>
>
> string(1) "8"
>
> }
>
> }
>
>
>
> preg_match_all doesn't do the trick.
>
>
>
> The Expression I want to use is
>
>
>
> @prim_states[\s]*\((?[0-9\s]*)(prim_state[\s]*\((?[a-fA-F0-9\s]*)
> tex_idxs[\s]*\(([0-9\s]*)\)([0-9\s]*)\)[\s]*)*\)@
>
>
>
> as I'm trying to parse a 3d Model File, that contains data in the form of
>
>
>
> prim_states ( 51
>
> prim_state ( 00000000 2
>
> tex_idxs ( 1 2 ) 0 3 0 0 1
>
> )
>
> prim_state ( 00000000 3
>
> tex_idxs ( 1 2 ) 0 0 1 0 1
>
> )
>
> prim_state ( 00000000 3
>
> tex_idxs ( 1 2 ) 0 4 1 0 1
>
> )
>
> [.]
>
> )
>
>
>
> Thank You!
>
>
>
> Regards,
>
> Jan
>
>
If your example is indicative of what you need, then couldn't you just
loop through all the elements inside of the match as it will be a
contiguous string of numbers.

Thanks,
Ash
http://www.ashleysheridan.co.uk




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: preg_match()

am 09.09.2009 18:39:32 von Jan Reiter

Not quite, I'm sorry. As the outer expression prim_states( [...] ) =
captures,
the repetitive elements inside prim_state( [...] ) overwrite each other =
in
the matches array.=20
Of course I could get the first entry in matches, and search it again =
with
preg_match_all(), but that is what I'm trying to avoid. But maybe that's
what I have to after all if there is no other way.

Thanks & Regards,
Jan

-----Original Message-----
From: Ashley Sheridan [mailto:ash@ashleysheridan.co.uk]=20
Sent: Wednesday, September 09, 2009 6:21 PM
To: Jan Reiter
Cc: php-general@lists.php.net
Subject: Re: [PHP] preg_match()

On Wed, 2009-09-09 at 18:18 +0200, Jan Reiter wrote:
> Good Afternoon.
>=20
> =20
>=20
> This shouldn't be too complicated, but I can't come up with a solution =
for
> the problem right now. It's about RegEx in PHP (5.2)
>=20
> =20
>=20
> Is there a way to capture ALL sub elements of an expression like
> preg_match('@a(?[0-9])*b@' ,"a2345678b" , $matches); ??
>=20
> This would produce (below) whereas I'd like to get all the elements =
(2-8)
as
> an array entry each . ([1]=3D>2, [2]=3D>3 .)
>=20
> =20
>=20
> Result:
>=20
> array(2) {
>=20
> [0]=3D>
>=20
> array(1) {
>=20
> [0]=3D>
>=20
> string(9) "a2345678b"
>=20
> }
>=20
> [1]=3D>
>=20
> array(1) {
>=20
> [0]=3D>
>=20
> string(1) "8"
>=20
> }
>=20
> }
>=20
> =20
>=20
> preg_match_all doesn't do the trick.=20
>=20
> =20
>=20
> The Expression I want to use is
>=20
> =20
>=20
>
@prim_states[\s]*\((?[0-9\s]*)(prim_state[\s]*\((?[a-fA-F0-9\s=
]*)
> tex_idxs[\s]*\(([0-9\s]*)\)([0-9\s]*)\)[\s]*)*\)@
>=20
> =20
>=20
> as I'm trying to parse a 3d Model File, that contains data in the form =
of
>=20
> =20
>=20
> prim_states ( 51
>=20
> prim_state ( 00000000 2
>=20
> tex_idxs ( 1 2 ) 0 3 0 =
0 1
>=20
> )
>=20
> prim_state ( 00000000 3
>=20
> tex_idxs ( 1 2 ) 0 0 1 =
0 1
>=20
> )
>=20
> prim_state ( 00000000 3
>=20
> tex_idxs ( 1 2 ) 0 4 1 =
0 1
>=20
> )
>=20
> [.]
>=20
> )
>=20
> =20
>=20
> Thank You!
>=20
> =20
>=20
> Regards,
>=20
> Jan
>=20
> =20
If your example is indicative of what you need, then couldn't you just
loop through all the elements inside of the match as it will be a
contiguous string of numbers.

Thanks,
Ash
http://www.ashleysheridan.co.uk




--=20
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Eingehende eMail ist virenfrei.
Von AVG überprüft - www.avg.de=20
Version: 8.5.409 / Virendatenbank: 270.13.71/2336 - Ausgabedatum: =
09/08/09
20:45:00=20


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: preg_match()

am 09.09.2009 19:56:50 von Shawn McKenzie

Jan Reiter wrote:
> Good Afternoon.
>
>
>
> This shouldn't be too complicated, but I can't come up with a solution for
> the problem right now. It's about RegEx in PHP (5.2)
>
>
>
> Is there a way to capture ALL sub elements of an expression like
> preg_match('@a(?[0-9])*b@' ,"a2345678b" , $matches); ??
>
> This would produce (below) whereas I'd like to get all the elements (2-8) as
> an array entry each . ([1]=>2, [2]=>3 .)
>

AFAIK, you would need to know how many digits are there and use that
many capture groups (), because you only have one capture group which
will be overwritten each iteration. An alternative for your simple
example above would be to do:

preg_match('@a([0-9]+)b@' ,"a2345678b" , $matches);

--then--

print_r(str_split($matches[1]));

To do this multiple times in a large string you would need:

preg_match_all('@a([0-9]+)b@' ,"a2345678b" , $matches);

foreach($matches[1] as $match) {
print_r(str_split($match));
}

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: preg_match()

am 09.09.2009 20:08:07 von Jan Reiter

Thanks! Thats about what I'm doing right now.=20

function parse_prim_states($in)
{
preg_match('@prim_states[\s]*\((?[0-9\s]*)@' ,$in ,
$matches);
$this->num_prim_states =3D (int)$matches['number'];
=09
=09
preg_match_all('@prim_state[\s]*\((?[a-fA-F0-9\s]*)tex_idxs[\s]*\(=
(?<
texidx>[0-9\s]*)\)(?[0-9\s]*)\)@' ,$in , $matches);

for($i=3D0;$i<$this->num_prim_states;$i++)
{
=09
$this->add_prim_state($matches['flag1'][$i],$matches['flag2' ][$i],$matche=
s['
texid'][$i]);
}
=09
unset($matches);

return true;
}

Regards

-----Original Message-----
From: Shawn McKenzie [mailto:nospam@mckenzies.net]=20
Sent: Wednesday, September 09, 2009 7:57 PM
To: Jan Reiter
Cc: php-general@lists.php.net
Subject: [PHP] Re: preg_match()

Jan Reiter wrote:
> Good Afternoon.
>=20
> =20
>=20
> This shouldn't be too complicated, but I can't come up with a solution =
for
> the problem right now. It's about RegEx in PHP (5.2)
>=20
> =20
>=20
> Is there a way to capture ALL sub elements of an expression like
> preg_match('@a(?[0-9])*b@' ,"a2345678b" , $matches); ??
>=20
> This would produce (below) whereas I'd like to get all the elements =
(2-8)
as
> an array entry each . ([1]=3D>2, [2]=3D>3 .)
>=20

AFAIK, you would need to know how many digits are there and use that
many capture groups (), because you only have one capture group which
will be overwritten each iteration. An alternative for your simple
example above would be to do:

preg_match('@a([0-9]+)b@' ,"a2345678b" , $matches);

--then--

print_r(str_split($matches[1]));

To do this multiple times in a large string you would need:

preg_match_all('@a([0-9]+)b@' ,"a2345678b" , $matches);

foreach($matches[1] as $match) {
print_r(str_split($match));
}

--=20
Thanks!
-Shawn
http://www.spidean.com

--=20
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Eingehende eMail ist virenfrei.
Von AVG überprüft - www.avg.de=20
Version: 8.5.409 / Virendatenbank: 270.13.71/2336 - Ausgabedatum: =
09/09/09
06:53:00=20


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php