NYCPHP Meetup

NYPHP.org

[nycphp-talk] quick regex question

Hans Zaunere hans at nyphp.org
Thu Sep 4 10:20:16 EDT 2003



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 (

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

> 
> David
> 
> 
>>Given:
>>
>>$swipe = 'A405E,;095612813=1227?2003/08/20 10:51:15 4A4';
>>
>>preg_match('/\;(.+)\=/s',$swipe,$match);
>>
>>This fills $match as:
>>
>>array(2) {
>>  [0]=>
>>  string(11) ";095612813="
>>  [1]=>
>>  string(9) "095612813"
>>}
>>
>>
>>Can I modify my subpattern somehow, so that only the second
>>element of the array is returned?  I don't need the whole string,
>>just the matched pattern itself (ie, I don't need the ';' and '='
>>chars in the result).
>>
>>Hopefully I can get my work done and give my take on the
>>fundamentals!  Thanks,
>>
>>H
>>
>>_______________________________________________
>>talk mailing list
>>talk at lists.nyphp.org
>>http://lists.nyphp.org/mailman/listinfo/talk
> 
> 
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk




More information about the talk mailing list