Ruby version of preg_match_all?

  • Thread starter David Heinemeier Hansson
  • Start date
D

David Heinemeier Hansson

I'm having a hard time finding a way of archiving the functionality of
PHP's preg_match_all[1] in Ruby. Am I just not looking in the right
places? I have a string like "SomethingCool SomethingWild Something
AnotherCool" and want to use a regular expression, like /(\w+Cool)/, to
get all the *Cool words back in an array (["SomethingCool",
"AnotherCool"]).

[1] http://dk2.php.net/preg_match_all


/ David
 
M

Michael Neumann

I'm having a hard time finding a way of archiving the functionality of
PHP's preg_match_all[1] in Ruby. Am I just not looking in the right
places? I have a string like "SomethingCool SomethingWild Something
AnotherCool" and want to use a regular expression, like /(\w+Cool)/, to
get all the *Cool words back in an array (["SomethingCool",
"AnotherCool"]).

I guess String#scan will do it:

"SomethingCool SomethingWild".scan(/(\w+Cool)/)


Regards,

Michael
 
B

Bill Kelly

Hi,

From: "David Heinemeier Hansson said:
I'm having a hard time finding a way of archiving the functionality of
PHP's preg_match_all[1] in Ruby. Am I just not looking in the right
places? I have a string like "SomethingCool SomethingWild Something
AnotherCool" and want to use a regular expression, like /(\w+Cool)/, to
get all the *Cool words back in an array (["SomethingCool",
"AnotherCool"]).

String#scan might be what you're looking for:

irb> "cool wild anothercool".scan(/\b\w*cool\b/)
["cool", "anothercool"]


Hope this helps,

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top