ruby - Strip words beginning with a specific letter from a sentence using regex -
I'm not sure how to use regular expressions in a function so that I can start all the words in one sentence. Special Letter I know that I can do this:
word = ~ / ^ # {letter} /
to check that the word letter Begins with, but how do I talk to the word? Do I have to convert the string to an array and then have to walk through each word again, or is there a fast way to use regedx? I am using Ruby so that it will look:
<<> mailing_words = array.new sentance.split (""). Words | Match_words.push (word) if word = ~ / ^ # {letter} / end
Scan can be a good tool for this:
#! / Usr / bin / ruby1.8 s = "I think Paris is a beautiful place in the spring" p s.scan (/ \ b [this] [[:: alpha:]] * * / i) # = & gt; ; ["I", "think", "in", "the", "is"]
-
\ b
means the word boundary . ' -
[: alpha:]
means upper or lowercase alpha (az).
Comments
Post a Comment