Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [newbie] How do I match a regex multiple times?

Reply
Thread Tools

[newbie] How do I match a regex multiple times?

 
 
Robo
Guest
Posts: n/a
 
      04-03-2004
When I use String#match, it only seems to match the regex pattern once. How
do I tell it to match the pattern as many times as possible?

e.g. puts 'robo ruler ruby'.match(/r\w+/)

That only return 'robo', but I would like it to return [robo, ruler, ruby]

I tried using the /g modifier, but Ruby doesn't seem to know about that.

I'm doing this 'cos I'm trying to extract all the function names from a PHP
file, but I shouldn't have to tell it to search it line by line do I?

Robo


 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      04-03-2004
>>>>> "R" == Robo <> writes:

R> e.g. puts 'robo ruler ruby'.match(/r\w+/)

String#scan

svg% ri String#scan
------------------------------------------------------------ String#scan
str.scan(pattern) => array
str.scan(pattern) {|match, ...| block } => str
------------------------------------------------------------------------
Both forms iterate through _str_, matching the pattern (which may
be a +Regexp+ or a +String+). For each match, a result is generated
and either added to the result array or passed to the block. If the
pattern contains no groups, each individual result consists of the
matched string, +$&+. If the pattern contains groups, each
individual result is itself an array containing one entry per
group.

a = "cruel world"
a.scan(/\w+/) #=> ["cruel", "world"]
a.scan(/.../) #=> ["cru", "el ", "wor"]
a.scan(/(...)/) #=> [["cru"], ["el "], ["wor"]]
a.scan(/(..)(..)/) #=> [["cr", "ue"], ["l ", "wo"]]

And the block form:

a.scan(/\w+/) {|w| print "<<#{w}>> " }
print "\n"
a.scan(/(.)(.)/) {|a,b| print b, a }
print "\n"

_produces:_

<<cruel>> <<world>>
rceu lowlr

svg%


Guy Decoux





 
Reply With Quote
 
 
 
 
Bil Kleb
Guest
Posts: n/a
 
      04-03-2004
Robo wrote:
> When I use String#match, it only seems to match the regex pattern once. How
> do I tell it to match the pattern as many times as possible?


puts 'robo ruler ruby'.scan(/r\w+/)
robo
ruler
ruby

Regards,
--
Bil Kleb, Hampton, Virginia

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Multiple regex match idiom Hrvoje Niksic Python 3 05-10-2007 10:07 AM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments