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"]] 

http://ruby-doc.org/core/classes/string.html#m000812


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -

PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea -