Forum: Ruby Problem with reqular expression

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Faa86d6ab4a5d5d80cfb11197941e70d?d=identicon&s=25 Ilyas (Guest)
on 2006-05-23 12:19
I have this string:

Color1  R=144 G=248 B=255

Need regular expression for get R,G,B
How I can simplify it:

lines.gsub!(/Color1  R=(\d+) G=(\d+) B=(\d+)/) {
      puts $1
      puts $2
      puts $3
}
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-05-23 12:32
(Received via mailing list)
Hi --

On Tue, 23 May 2006, Ilyas wrote:

>      puts $3
> }

lines.scan(/\d{3}/) would get you all three.


David
76006c10b0773e69a6fcd70614ac258f?d=identicon&s=25 Joost Diepenmaat (Guest)
on 2006-05-23 12:35
(Received via mailing list)
On Tue, May 23, 2006 at 07:19:50PM +0900, Ilyas wrote:
> }
>

Erm... "/Color1  R=(\d+) G=(\d+) B=(\d+)/" seems to me about
as simple as you can get. I do wonder why you're using an in-place
substitution, though.

Joost.
927d7f10fcbcb6b90a0d53047f14ec7b?d=identicon&s=25 Regg Mr (regg)
on 2006-05-23 12:41
Ilyas wrote:
> I have this string:
>
> Color1  R=144 G=248 B=255
>
> Need regular expression for get R,G,B
> How I can simplify it:
>
> lines.gsub!(/Color1  R=(\d+) G=(\d+) B=(\d+)/) {
>       puts $1
>       puts $2
>       puts $3
> }


 lines.gsub!(/R=(\d+) G=(\d+) B=(\d+)/) {puts $1; puts $2; puts $3}

=>144
=>248
=>255

You didn't use semi-colons within your block.
Faa86d6ab4a5d5d80cfb11197941e70d?d=identicon&s=25 Ilyas (Guest)
on 2006-05-23 14:20
Regg Mr wrote:
> Ilyas wrote:
>> I have this string:
>>
>> Color1  R=144 G=248 B=255
>>
>> Need regular expression for get R,G,B
>> How I can simplify it:
>>
>> lines.gsub!(/Color1  R=(\d+) G=(\d+) B=(\d+)/) {
>>       puts $1
>>       puts $2
>>       puts $3
>> }
>
>
>  lines.gsub!(/R=(\d+) G=(\d+) B=(\d+)/) {puts $1; puts $2; puts $3}
>
> =>144
> =>248
> =>255
>
> You didn't use semi-colons within your block.

It's all what I wanted:

def get_color(rgb_string, color)
    @R, @G, @B = 0, 0, 0
    @R, @G, @B = Regexp.new(color + '\s+R=(\d+)\s+G=(\d+)\s+B=(\d+)').
                 match(rgb_string).captures
    sprintf("#%x%x%x", @R, @G, @B)
end

Thanks for Pena, Botp because your idea
This topic is locked and can not be replied to.