Re: sorting an array of regexes
| From: | Eugene Lee | Date: | Tue, 18 Nov 2003 19:46:13 +0000 |
| Subject: | Re: sorting an array of regexes | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-170151@lists.php.net to get a copy of this message | ||
On Tue, Nov 18, 2003 at 01:52:39PM +0100, Wouter van Vliet wrote:
: Eugene Lee suggested:
: > On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka
: > Gasiorowski FNORD wrote:
: > :
: > : There is an array of regexes, for example
: > :
: > : $array = array('moon', '[wh]ood', '[^as]eed' ...
: > : (about 300 entries).
: > :
: > : I want to sort it comparing to the
: > : character lenght of a regex. For example
: > : [wh]ood is 4 characters, moon is 4 characters.
: > : There are only letters of the alphabet and
: > : letter ranges present in those regexes. I
: > : want the "longest" ones first.
: > :
: > : How would you write the sorting function?
: >
: > This might be the most functionally correct, although it's definitely
: > not the fastest route.
: >
: > function re_len($pat)
: > {
: > return strlen(preg_replace('/\[[^]]+]/', '_', $pat));
:
: I think you meant:
:
: /\[[^\]]+]/
:
: as regex ;) Not sure, but I think one more block-bracked needed to be
: escaped ;)
Nope. My pattern is legitimate. Within a range, if the first character
is a closing-square-bracket ']', it is treated as the literal character
and not as the end of range. If the range starts with a negation '^',
then the same rule applies to the second character.
This is also a sad indication that I really know my regular expressions,
or I need a vacation. :-)