RE: [PHP-DEV] regex operators
| From: | David Sklar | Date: | Tue, 14 Oct 2003 15:13:47 +0000 |
| Subject: | RE: [PHP-DEV] regex operators | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-4829@lists.php.net to get a copy of this message | ||
One thing that I suppose you get by using "case preg_match()" like George
does below (as opposed to my preg_case operator or Hartmut's callback) is
you can deal with arbitrary arguments to the preg_match() (or other)
function -- storing captured subpatterns from the regex, for example.
David
On Tuesday, October 14, 2003 11:04 AM, mailto:george@omniti.com wrote:
> Aesthetics aside, how is that different in effect from:
>
> switch(true) {
> case preg_match('/foo.*?bar/i', $data):
> /** stuff **/
> break;
> case preg_match('/baz/', $data);
> /** other stuff **/
> break;
> default:
> break;
> }
>
> ?
>
> On Tuesday, October 14, 2003, at 10:58 AM, Andrey Hristov wrote:
>
>> Hi David,
>> 2 weeks ago Hartmut Holzgraefe had similar idea :
>> switch ($data, "preg_match") {
>> case '/foo.*?bar/i' : /* computation here */
>> break;
>> }
>>
>>
>> The second argument is a callback.
>>
>> Andrey
>>
>> David Sklar wrote:
>>
>>> I was thinking about adding one or two regex-related features to
>>> the engine:
>>>
>>> 1. "preg_case": this would behave just like case but instead of
>>> doing an equality comparison, would match against a regular
>>> expression, e.g.
>>>
>>> switch($data) {
>>> preg_case '/^\d{5}(-\d{4})?$/':
>>> print "US Postal Code";
>>> break;
>>> preg_case '/^[a-z]\d[a-z] ?\d[a-z]\d$/i';
>>> print "Canadian Postal Code";
>>> break;
>>> default:
>>> print "something else!";
>>> }
>>>
>>> Where should any captured subpatterns go?
>>>
>>> 2. A regex match operator that returns an array containing
>>> subpatterns. If there is no match against the regex, then an empty
>>> array (or just false?) would be returned.
>>>
>>> if ($data =~ '/^(\d{5})(-\d{4})?$/') {
>>> print "The whole postal code is $data[0].";
>>> print "The first five digits is $data[1].";
>>> if ($data[2]) { print "The ZIP+4 is $data[2].";}
>>> }
>>>
>>> Some issues with adding these features:
>>>
>>> - It creates an engine dependency on the PCRE library.
>>> - There would have to be some new opcodes and parser tokens
>>> - Ideally the code that implements these operators could share as
>>> much as possible with what's already been done in the PCRE
>>> extension -- is that possible?
>>>
>>> Comments?
>>>
>>> Thanks,
>>> David
>>>
>>>
>>
>> --
>> PHP Internals - PHP Runtime Development Mailing List
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
> -- George Schlossnagle
> -- Principal Consultant
> -- OmniTI Computer Consulting, Inc.
> -- +1.410.872.4910 x202
> -- 1024D/1100A5A0 1370 F70A 9365 96C9 2F5E 56C2 B2B9 262F 1100 A5A0