Forum: Ruby Re: 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.
Peña, Botp (Guest)
on 2006-05-23 14:50
(Received via mailing list)
fr Ilyas:
# 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
# }

irb(main):026:0> s = "Color1  R=144 G=248 B=255"
=> "Color1  R=144 G=248 B=255"
irb(main):030:0> r,g,b=(/R=(\d+)\s+G=(\d+)\s+B=(\d+)/).match(s).captures
=> ["144", "248", "255"]
irb(main):031:0> r
=> "144"
irb(main):032:0> g
=> "248"
irb(main):033:0> b
=> "255"
This topic is locked and can not be replied to.