[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby version of preg_match_all?

David Heinemeier Hansson

9/19/2003 1:27:00 PM

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...


/ David


2 Answers

Michael Neumann

9/19/2003 1:35:00 PM

0

On Fri, Sep 19, 2003 at 10:26:57PM +0900, David Heinemeier Hansson wrote:
> 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

Bill Kelly

9/19/2003 2:39:00 PM

0

Hi,

From: "David Heinemeier Hansson" <david@loudthinking.com>
>
> 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