in reply to Sorting words, keeping only certain words
I'm sure at least one of the solutions offered thus far works but I'm offering the following regex because it matches exactly what you want rather than trying to exclude things you don't want.
my @sorted = sort grep /^[a-z](?:[^-]|-(?!-))*(?<=[a-z])$/i, @words;
Here's the explanation:
/^[a-z] # Start with a letter. (?: # Start grouping. [^-] # Anything other than a hyphen | # Or -(?!-) # A hyphen not followed by another hyphen )* # End group. Match 0 or more of those groups. (?<=[a-z])$ # The end, as long as it is preceded by a letter. /ix # Match case insensitively and allow comments.
I almost posted this with a different regex:
but that one requires that the words are at least two characters (actually letters due to other constraints) long, so I changed it to a positive look-behind assertion./^[a-z](?:[^-]|-(?!-))*[a-z]$/i
-sauoq "My two cents aren't worth a dime.";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Sorting words while excluding some.
by BrowserUk (Patriarch) on Nov 11, 2002 at 01:32 UTC |