alexnewby
alexnewby

Reputation: 61

Create a regex that returns an array

I am working in Ruby. I need to create a regex that takes in a string, I suppose, and returns an array with only the words that start with "un" and end with "ing". I have no clue how to do it :/

def words_starting_with_un_and_ending_with_ing(text)
  !!text.capitalize.scan(/\A+UN\Z+ING/)
end

Upvotes: 0

Views: 716

Answers (1)

Sagar Pandya
Sagar Pandya

Reputation: 9497

Something like this:

def uning string
  string.scan(/\b[Uu]n[a-z]*ing\b/)
end

See String#scan for more info. For a nice interactive introduction to Regex take a look at RegexOne.

Upvotes: 5

Related Questions