Regular Expression

Given: the context,

sentence = “It’s my book, I’ve missed it yesterday.”

When: this action occurred,

words = sentence.scan(#…re)

Then: expecting the output,

words #=>[“It’s”, “my”, “book”, “I’ve”, “missed”, “it”, “yesterday”]

Selvag R. wrote in post #1146016:

Given: the context,

sentence = “It’s my book, I’ve missed it yesterday.”

When: this action occurred,

words = sentence.scan(#…re)

Then: expecting the output,

words #=>[“It’s”, “my”, “book”, “I’ve”, “missed”, “it”, “yesterday”]

words = sentence.sub(/[[.|,]]/, ‘’).split(’ ')

words #=>[“It’s”, “my”, “book”, “I’ve”, “missed”, “it”, “yesterday”]

sentence.scan(/[\w’]+/)
#=> [“It’s”, “my”, “book”, “I’ve”, “missed”, “it”, “yesterday”]

or also interesting:
sentence.gsub(/’\w+/,"'s" => " is", “'ve” => " have").scan(/\w+/)
#=> [“It”, “is”, “my”, “book”, “I”, “have”, “missed”, “it”, “yesterday”]

Hans M. wrote in post #1146044:

sentence.scan(/[\w’]+/)
#=> [“It’s”, “my”, “book”, “I’ve”, “missed”, “it”, “yesterday”]

or also interesting:
sentence.gsub(/’\w+/,"'s" => " is", “'ve” => " have").scan(/\w+/)
#=> [“It”, “is”, “my”, “book”, “I”, “have”, “missed”, “it”, “yesterday”]

Your reply is very useful today and you’ve shown a way of arguments to
gsub().

Thank you.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs