Problem with reqular expression

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
}

···

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

Hi --

···

On Tue, 23 May 2006, 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.scan(/\d{3}/) would get you all three.

David

--
David A. Black (dblack@wobblini.net)
* Ruby Power and Light, LLC (http://www.rubypowerandlight.com)
   > Ruby and Rails consultancy and training
* Author of "Ruby for Rails" from Manning Publications!
   > Ruby for Rails

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.

···

On Tue, May 23, 2006 at 07:19:50PM +0900, 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
}

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.

···

--
Posted via http://www.ruby-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/\.