[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Is there something like Regexp#match_all

Andi Schacke

12/11/2006 9:27:00 PM

Hi,

is there a possibility to return all the matches of a regexp? or in
other words is there a way to apply a block to all matches at once?
Something like String#gsub but where I can specifiy what to do with the
matches...

Thanks
Andi

--
Posted via http://www.ruby-....

5 Answers

Austin Ziegler

12/11/2006 9:36:00 PM

0

On 12/11/06, Andi Schacke <memberships.andi@gmail.com> wrote:
> is there a possibility to return all the matches of a regexp? or in
> other words is there a way to apply a block to all matches at once?
> Something like String#gsub but where I can specifiy what to do with the
> matches...

These things are actually two different things.

1. Look at String#scan
2. Look at String#gsub with the block form and use Regexp::last_match
(it's a thread-local value, IIRC).

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Robert Klemme

12/11/2006 10:36:00 PM

0

On 11.12.2006 22:26, Andi Schacke wrote:
> is there a possibility to return all the matches of a regexp? or in
> other words is there a way to apply a block to all matches at once?
> Something like String#gsub but where I can specifiy what to do with the
> matches...

What exactly do you want to do with matches?

robert

Andi Schacke

12/11/2006 10:51:00 PM

0

Robert Klemme wrote:
> On 11.12.2006 22:26, Andi Schacke wrote:
>> is there a possibility to return all the matches of a regexp? or in
>> other words is there a way to apply a block to all matches at once?
>> Something like String#gsub but where I can specifiy what to do with the
>> matches...
>
> What exactly do you want to do with matches?
>
> robert

I'd like to build a small web-tool for myself to highlight all the
matches of a regexp against a specified string (e.g. the matches should
be in a different color). But I think I found a solution:

source_string.gsub(regexp) {|match| "<span
style=\"color:red;\">#{match}</span>"}

--
Posted via http://www.ruby-....

James Gray

12/11/2006 10:56:00 PM

0

On Dec 11, 2006, at 4:50 PM, Andi Schacke wrote:

> Robert Klemme wrote:
>> On 11.12.2006 22:26, Andi Schacke wrote:
>>> is there a possibility to return all the matches of a regexp? or in
>>> other words is there a way to apply a block to all matches at once?
>>> Something like String#gsub but where I can specifiy what to do
>>> with the
>>> matches...
>>
>> What exactly do you want to do with matches?
>>
>> robert
>
> I'd like to build a small web-tool for myself to highlight all the
> matches of a regexp against a specified string (e.g. the matches
> should
> be in a different color). But I think I found a solution:
>
> source_string.gsub(regexp) {|match| "<span
> style=\"color:red;\">#{match}</span>"}

You don't really need the block form for such a simple replacement.
This is the same thing:

source_string.gsub(regexp, '<span color="red">\&</span>')

James Edward Gray II

Robert Klemme

12/12/2006 10:12:00 AM

0

On 11.12.2006 23:56, James Edward Gray II wrote:
> On Dec 11, 2006, at 4:50 PM, Andi Schacke wrote:
>> I'd like to build a small web-tool for myself to highlight all the
>> matches of a regexp against a specified string (e.g. the matches should
>> be in a different color). But I think I found a solution:
>>
>> source_string.gsub(regexp) {|match| "<span
>> style=\"color:red;\">#{match}</span>"}
>
> You don't really need the block form for such a simple replacement.
> This is the same thing:
>
> source_string.gsub(regexp, '<span color="red">\&</span>')

.... and in fact more efficient IIRC.

robert