...

View Full Version : How to create an optimized regular expression?



ca_redwards
11-08-2004, 08:16 PM
In another thread, someone had asked for a script to validate an input against the list of 140 standard named colors. Initially, I just stuck the whole pipe-delimited list into a regular expression
/^(...|...|...|...|...|...|...)$/ and declared it done. But that seemed rather bulky, to say the least. As an alternative, I alphabetized the list, then used parenthesized subexpressions for common prefixes to make the whole regular expression more compact
/^(dark(...|...|...)|light(...|...|...)|...|...)$/. Later, I recognized that many colors rhyme, so I produced an even tighter regular expression by grouping colors with the same ending
/^(...|(...|...|...)blue|(...|...|...)green|...|...)$/. I expect that there are even tighter expressions possible with a combination of rhyming and alliterative techniques.

I welcome your function that accepts an array of words and returns the tightest possible regular expression. Do you have one or know where I can get it?

Willy Duitt
11-08-2004, 08:29 PM
removed as lack of feedback indicated that supplied script was not what the O/P was looking for and is therefore clearly redundant...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum