in reply to Sorting words, keeping only certain words

If you have them in an array, then all it would seem you would need to do would be:

  1. filter out any words that didn't start with a letter (/^\w/)
  2. take the above array, then filter for words not ending with a letter (/\w$/)
  3. take the resultant array, and filter any words containing characters other than word characters (\w) or hyphen (! /\w(\w\-)+\w$/ maybe)
  4. take resulting array, and filter out words containing hyphen 2 or more {2,} times (! /\-{2,}/ maybe).
I would suggest multiple uses of grep as a possibility (but that is just me-I am sure others will come up with better solutions).

I thought up about 6 lines of code to do each step, but thought the code (untested and untried) was not so good, so I removed it from being visible....

Update: Sounds like, if you're using grep or a match, you need to look at anchoring the match you're trying to find if it matches beginning with a hyphen by either doing /^\w/ to match beginning with a word character, or /^\-/ to match against a leading dash.

  • Comment on Re: Sorting words, keeping only words that start with a letter and contain only letter characters, and hypens, and end in a letter

Replies are listed 'Best First'.
Re: Re: Sorting words, keeping only words that start with a letter and contain only letter characters, and hypens, and end in a letter
by Anonymous Monk on Nov 10, 2002 at 22:45 UTC
    see, i'm new to this and i don't understand how to check if a word begins with a hyphen.. i've got my sort down to the point where it just keeps words with letters and hyphens. The making sure there's a letter, hyphen then letter is my problem.