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
}
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
}
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.
Hi –
On Tue, 23 May 2006, Ilyas wrote:
puts $3
}
lines.scan(/\d{3}/) would get you all three.
David
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.
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
=>255You 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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs