How can I extract the words from this string " !!one!! **two** @@three@@" using Regex in Ruby -
in irb, can this:
c = /(\b\w+\b)\w*(\b\w+\b)\w*(\b\w+\b)\w*/.match(" !!one** *two* @@three@@ ")
and this:
=> matchdata "one** *two* @@three@@ " 1:"one" 2:"two" 3:"three"
but assuming don't know number of words in advance, how can still extract words out of string". example, might " !!one** *two* @@three@@ " in 1 instance, might " !!five** *six* " in instance.
thanks.
> " !!one** *two* @@three@@ ".scan(/\w+/) => ["one", "two", "three"]
also, scan
can return array of arrays in case of using ()
.
> "our fifty users left 500 posts month.".scan(/([a-z]+|\d+)\s+(posts|users)/i) => [["fifty", "users"], ["500", "posts"]]
Comments
Post a Comment