[PHP] sorting an array of regexes
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? -- Seks, seksi, seksolatki... news:pl.soc.seks.moderowana http://hyperreal.info { iWanToDie } WiNoNa) ( http://szatanowskie-ladacznice.0-700.pl foReVeR( * ) Poznaj jej zwiewne ksztaty... http://www.opera.com 007 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array of regexes
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)); } function re_sort($a_pat, $b_pat) { $a = re_len($a_pat); $b = re_len($b_pat); if ($a == $b) { return 0; } return ($a $b ) ? -1 : 1; } usort($array, 're_sort'); BTW, re_len() will bomb on certain oddball patterns with strange ranges. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sorting an array of regexes
-Oorspronkelijk bericht- Van: Eugene Lee [mailto:[EMAIL PROTECTED] 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 ;) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array of regexes
Eugene Lee wrote: 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. Thank you, that will certainly work :8]. Does anyone have any thoughts how to make it faster? It is not VERY critical, because the calculation will be done only once, at initialization, but...well, you know :8]. * * * How about...if I count the number of ']' in string and then add it to the strlen - (the number of ']' x 2)? There are no [ nor ] in these except in the character range parts. Does that look faster than applying regular expression? function re_len($pat) { return strlen(preg_replace('/\[[^]]+]/', '_', $pat)); } function re_sort($a_pat, $b_pat) { $a = re_len($a_pat); $b = re_len($b_pat); if ($a == $b) { return 0; } return ($a $b ) ? -1 : 1; } usort($array, 're_sort'); BTW, re_len() will bomb on certain oddball patterns with strange ranges. Like? Sorry, I can't think of anything right now... -- Seks, seksi, seksolatki... news:pl.soc.seks.moderowana http://hyperreal.info { iWanToDie } WiNoNa) ( http://szatanowskie-ladacznice.0-700.pl foReVeR( * ) Poznaj jej zwiewne ksztaty... http://www.opera.com 007 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array of regexes
* Thus wrote Adam i Agnieszka Gasiorowski FNORD ([EMAIL PROTECTED]): Eugene Lee wrote: 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. Thank you, that will certainly work :8]. Does anyone have any thoughts how to make it faster? It is not VERY critical, because the calculation will be done only once, at initialization, but...well, you know :8]. Do these change all the time or are they rather static? I would suggest using this routine to generate the list to be used in your program, instead of doing this on the fly. Curt -- My PHP key is worn out PHP List stats since 1997: http://zirzow.dyndns.org/html/mlists/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array of regexes
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. :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting an array of regexes
Curt Zirzow wrote: * Thus wrote Adam i Agnieszka Gasiorowski FNORD ([EMAIL PROTECTED]): Eugene Lee wrote: 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. Thank you, that will certainly work :8]. Does anyone have any thoughts how to make it faster? It is not VERY critical, because the calculation will be done only once, at initialization, but...well, you know :8]. Do these change all the time or are they rather static? I would suggest using this routine to generate the list to be used in your program, instead of doing this on the fly. Good idea... -- Seks, seksi, seksolatki... news:pl.soc.seks.moderowana http://hyperreal.info { iWanToDie } WiNoNa) ( http://szatanowskie-ladacznice.0-700.pl foReVeR( * ) Poznaj jej zwiewne ksztaty... http://www.opera.com 007 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php