# regex operators

- Thread: https://externals.io/message/4825
- Original archive: https://news-web.php.net/php.internals/4825
- Started by David Sklar on 2003-10-14T14:47:19+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #4825 — David Sklar — 2003-10-14T14:47:19+00:00

URL: https://externals.io/message/4825#4825

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

---

## #4826 — Andrey Hristov — 2003-10-14T14:58:04+00:00

URL: https://externals.io/message/4825#4826 · In reply to: #4825

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

---

## #4827 — Hartmut Holzgraefe — 2003-10-14T15:04:03+00:00

URL: https://externals.io/message/4825#4827 · In reply to: #4825

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!";
> }
> 

i've started to play with a more general way to handle this:

   switch(mixed data [, callback compare_function])
   {
      ...

"compare_function" would default to the functionality of the
"==" operator to show the current behavior

"compare_function" will be called with two parameters:
the switch expression and the case value and should
return true if both match, false otherwise

as a special case "compare_function" should also accept
"==="

this way we do not have to add new keywords and are not
limited to preg expresions here,
possible uses not possible now would include:

switch($foobar, "===") {
   case false: ...
   case 0: ...
   case '': ...

switch($foobar, "preg_match") {
   case '/^\d{5}$/':  ...
   case '/(foo)?bar/': ...

switch($foobar, "fnmatch") {
   case '*.gif': ...
   case '*.jpg': ...

switch($foober, "my_custom_function") {
  ...

switch($foobar, array($this, "compare_function") {
  ...




-- 
Hartmut Holzgraefe  <hartmut@php.net

---

## #4834 — Derick Rethans — 2003-10-14T16:45:53+00:00

URL: https://externals.io/message/4825#4834 · In reply to: #4833

> This should also be BC if you just pass one string.

http://docs.php.net/en/function.preg-grep.html

we already have that too.

Derick

-- 
"Interpreting what the GPL actually means is a job best left to those
                    that read the future by examining animal entrails."
-------------------------------------------------------------------------
 Derick Rethans                                 http://derickrethans.nl/ 
 International PHP Magazine                          http://php-mag.net/

---

## #4833 — Curt Zirzow — 2003-10-14T16:50:39+00:00

URL: https://externals.io/message/4825#4833 · In reply to: #4825

* Thus wrote David Sklar (sklar@sklar.com):
> 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!";
> }
[...]
> 
> 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?

I'm thinking a more viable solution would be to do something like:

mixed preg_match ( mixed pattern, string subject [, array matches [, int flags]]) 

if pattern is an array, preg_match will cycle through each one till
a match is made; returns the index of the array passed (that
matched) or false if not found.

This should also be BC if you just pass one string.


Curt
-- 
"I used to think I was indecisive, but now I'm not so sure."

---

## #4842 — netcat — 2003-10-14T18:24:50+00:00

URL: https://externals.io/message/4825#4842 · In reply to: #4827

Hartmut Holzgraefe wrote:

> David Sklar wrote:
>
> > […]
>
> i've started to play with a more general way to handle this:
>
>   switch(mixed data [, callback compare_function]) 

seems the sanest thing up until now.

slighly O.T.: would be and even better if lambda functions were 
available here

>
>   {
>      ...
>
> "compare_function" would default to the functionality of the
> "==" operator to show the current behavior
>
> "compare_function" will be called with two parameters:
> the switch expression and the case value and should
> return true if both match, false otherwise
>
> as a special case "compare_function" should also accept
> "==="
>
> this way we do not have to add new keywords and are not
> limited to preg expresions here,
> possible uses not possible now would include:
>
> switch($foobar, "===") {
>   case false: ...
>   case 0: ...
>   case '': ...
>
> switch($foobar, "preg_match") {
>   case '/^\d{5}$/':  ...
>   case '/(foo)?bar/': ...
>
> switch($foobar, "fnmatch") {
>   case '*.gif': ...
>   case '*.jpg': ...
>
> switch($foober, "my_custom_function") {
>  ...
>
> switch($foobar, array($this, "compare_function") {
>  ...
>
>
>
>


-- 
NetCat



--------------------------------------------------------
FREE 10MB email + Antivirus + AntiSpam + POP3 + more....
Get it at http://www.doal.co.il:81/free/?c=both

---

## #4854 — Hartmut Holzgraefe — 2003-10-15T08:12:48+00:00

URL: https://externals.io/message/4825#4854 · In reply to: #4842

netcat wrote:
> > […]
> 
> 
> seems the sanest thing up until now.
> 
> slighly O.T.: would be and even better if lambda functions were 
> available here


http://php.net/create-function

-- 
Hartmut Holzgraefe  <hartmut@php.net

---

## #4859 — netcat — 2003-10-15T11:32:33+00:00

URL: https://externals.io/message/4825#4859 · In reply to: #4854

Hartmut Holzgraefe wrote:

> netcat wrote:
>
> > […]
>
>
>
> http://php.net/create-function
>
Thanks for the link.

It's just feels a little awkward to see something like:
switch($x,create_function('$a,$b','return $a=="MY$b;"')) {
}
Again, it's a feeling, maybe it's just me.
It doesn't feel right to me that inside definition of new function
i have to escape quotes (that's because create_function
is a function and not special construction on the other side if
it would be a construct it can't return a value as in 
$x=create_function(...) ).
Anyway, i have nothing better to suggest (yet) ....

I hope one of you have some suggestion that will make create_function
more usable.

-- 
NetCat



------------------------------------------------------
SPAM-Free 10mb Free email + Antivirus + POP3 + more...
Get it at http://www.doal.co.il:81/free/?c=all-spam

---

## #4860 — Kouber Saparev — 2003-10-15T12:35:16+00:00

URL: https://externals.io/message/4825#4860 · In reply to: #4833

"Curt Zirzow" <php-dev@zirzow.dyndns.org> wrote in message
news:20031014165039.GP51024@bagend.shire...
> * Thus wrote David Sklar (sklar@sklar.com):
> > […]
engine:
> > […]
> [...]
> > […]
as
> > […]
>
> I'm thinking a more viable solution would be to do something like:
>
> mixed preg_match ( mixed pattern, string subject [, array matches [, int
flags]])
>
> if pattern is an array, preg_match will cycle through each one till
> a match is made; returns the index of the array passed (that
> matched) or false if not found.

It's the same if you have the array with patterns and you make a simple
foreach() cycle. I don't think that functions with internal loops should be
created, when we have a clear solution without it.

>
> This should also be BC if you just pass one string.
>
>
> Curt
> --
> "I used to think I was indecisive, but now I'm not so sure."
