NYCPHP Meetup

NYPHP.org

[nycphp-talk] quick regex question

Hans Zaunere hans at nyphp.org
Thu Sep 4 10:23:04 EDT 2003


Shoot, sorry folks; Mozilla got a little too eager to send the message....

Anyway...

David Sklar wrote:
> preg_match() always returns the text that matched the entire pattern as the
> first element of the $match array. So the only way to have a match array
> that contains just the numbers that match your current parenthesized
> subpattern is to have an entire pattern that matches just those numbers from
> the string. Which I don't think you can do.
> 
> Why do you want to do this?

That's a good question  :)  I guess I just want to be sure that the second element of the array will always be what I need.  I came across this in the PHP manual (http://us3.php.net/manual/en/pcre.pattern.syntax.php):

"An assertion subpattern is matched in the normal way, except
that  it  does not cause the current matching position to be
changed. Lookahead assertions start with  (?=  for  positive
assertions and (?! for negative assertions. For example,

\w+(?=;)

matches a word followed by a semicolon, but does not include
the semicolon in the match, and...."

Which basically does what I need, although maybe for the wrong reasons.  As long as I can be sure the second array is the matched pattern, I have no problem with it.  Although, I for large strings I could see this behavior as annoying; ie, having the first element a 10k string that's always ignored.

H




More information about the talk mailing list