ToddT
ToddT

Reputation: 3258

Ruby regex to match string between variable and two digits

I'm not getting what I want from my regex.

I'm looking thru @my_string which contains:

"Black Multi/Wide Calf":[{"large":"http://ecx.images-willy.com/images
/I/41suNF66r4L.jpg","variant":"MAIN","hiRes":"http://ecx.images-willy.com
/images/I/51knTtAU6mL._UL1000_.jpg","thumb":"http://ecx.images-willy.com
/images/I/41suNF66r4L._US40_.jpg","main":{"http://ecx.images-willy.com/images
/I/51knTtAU6mL._UY500_.jpg":["500","500""]}}],"Dark Brown":
[{"large":"http://ecx.images......

And I have a variable which is:

@color = "Black Multi"

And my regex looks like:

/^#{@color}(.*)\d+(}\])$/i.match(@my_string)

I want the string that starts with "Black Multi" and ends with }]:

Black Multi/Wide Calf":[{"large":"http://ecx.images-willy.com/images
/I/41suNF66r4L.jpg","variant":"MAIN","hiRes":"http://ecx.images-willy.com
/images/I/51knTtAU6mL._UL1000_.jpg","thumb":"http://ecx.images-willy.com
/images/I/41suNF66r4L._US40_.jpg","main":{"http://ecx.images-willy.com/images
/I/51knTtAU6mL._UY500_.jpg":["500","500""]}}]

I'm getting nil with what I have. where did I jack this up?

Upvotes: 0

Views: 81

Answers (2)

Cary Swoveland
Cary Swoveland

Reputation: 110685

You need to add the "multiline" flag (/m) to the regex:

str[/Black Multi.*?\}\]/m]
  #=> "Black Multi/Wide Calf\"......\"500\"\"]}}]" 

Upvotes: 1

the Tin Man
the Tin Man

Reputation: 160551

It looks like your string is a JSON-encoded object. Don't try to parse it using regex. Instead parse it using a JSON parser, then access its contents like normal.

require 'json'

my_string = '{"Black Multi/Wide Calf":[{"large":"http://ecx.images-willy.com/images/I/41suNF66r4L.jpg","variant":"MAIN","hiRes":"http://ecx.images-willy.com/images/I/51knTtAU6mL._UL1000_.jpg","thumb":"http://ecx.images-willy.com/images/I/41suNF66r4L._US40_.jpg","main":{"http://ecx.images-willy.com/images/I/51knTtAU6mL._UY500_.jpg":["500","500"]}}]}'
obj = JSON[my_string]
# => {"Black Multi/Wide Calf"=>
#      [{"large"=>"http://ecx.images-willy.com/images/I/41suNF66r4L.jpg",
#        "variant"=>"MAIN",
#        "hiRes"=>
#         "http://ecx.images-willy.com/images/I/51knTtAU6mL._UL1000_.jpg",
#        "thumb"=>
#         "http://ecx.images-willy.com/images/I/41suNF66r4L._US40_.jpg",
#        "main"=>
#         {"http://ecx.images-willy.com/images/I/51knTtAU6mL._UY500_.jpg"=>
#           ["500", "500"]}}]}

Because it's now a regular object, in this case a hash, it's easy to access its key/value pairs:

obj["Black Multi/Wide Calf"] # => [{"large"=>"http://ecx.images-willy.com/images/I/41suNF66r4L.jpg", "variant"=>"MAIN", "hiRes"=>"http://ecx.images-willy.com/images/I/51knTtAU6mL._UL1000_.jpg", "thumb"=>"http://ecx.images-willy.com/images/I/41suNF66r4L._US40_.jpg", "main"=>{"http://ecx.images-willy.com/images/I/51knTtAU6mL._UY500_.jpg"=>["500", "500"]}}]

And it's easy to drill down:

obj["Black Multi/Wide Calf"][0]['large'] # => "http://ecx.images-willy.com/images/I/41suNF66r4L.jpg"

Upvotes: 3

Related Questions