Problem with reqular expression


#1

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
}


#2

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.


#3

Hi –

On Tue, 23 May 2006, Ilyas wrote:

 puts $3

}

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

David


#4

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.


#5

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