Re: [PHP] [pcre] backreferences to all matches of a repeated subexpression

2007-08-07 Thread Richard Lynch
On Wed, August 1, 2007 10:05 pm, Jack Bates wrote:
> I'm trying to pull all the components out of strings structured like:
> word followed by any number of ( dot word or square bracketed string )
>
> This is an example: foo.bar[ab.cd].baz
>
> From the above example, I want: array('foo', 'bar', 'ab.cd', 'baz');

preg_match_all('|([^\\[\\[\\.]+)|', $text, $matches);
var_dump($matches);

should get you going down the right path...

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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



Re: [PHP] [pcre] backreferences to all matches of a repeated subexpression

2007-08-02 Thread Steffen Ebermann
On Wed, Aug 01, 2007 at 08:05:09PM -0700, Jack Bates wrote:
> I'm not sure how to get an array of all subexpression matches, in the
> order matches appear in the subject, instead of the order expressions
> appear in the pattern.

This problem gave me a headache. My only idea is to use preg_split()
instead.

  

Seems to work as intended. Hope I could help.

Regards
  Steffen

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



[PHP] [pcre] backreferences to all matches of a repeated subexpression

2007-08-01 Thread Jack Bates
I'm trying to pull all the components out of strings structured like:
word followed by any number of ( dot word or square bracketed string )

This is an example: foo.bar[ab.cd].baz

>From the above example, I want: array('foo', 'bar', 'ab.cd', 'baz');

A regular expression to match these strings, including parenthesis
around the parts I want to pull out:

preg_match('/(^\w+)(?:\.(\w+)|\[([^]]+)\])*/', $subject, $matches);

However applied to the above example:

$matches[1] = 'foo'; // First subexpression
$matches[2] = 'baz'; // Last value of second subexpression
$matches[3] = 'ab.cd'; // Last value of third subexpression

I'm not sure how to get an array of all subexpression matches, in the
order matches appear in the subject, instead of the order expressions
appear in the pattern.

I think I might be able to do something using the Perl (?{code})
construction, e.g. ...(?:\.(\w+)(?{$parts[] = \2})|\[([^]]+)(?{$parts[]
= \3})\])*... but I haven't thought about it too much because PHP
doesn't support this construction.

Any ideas much appreciated. Thanks, Jack

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