ruby - Not sure why regex is not working -
i have following regex:
a=/item\/([0-9]+)\)/.match("item/123) , item/245)") i'm trying pull out item ids of links in string, like:
[123,245] but returns
<matchdata "item/123)" 1:"123"> (ie first). how can make return both ids (either part of 2 matchdata or via other method)? think need specify greedy not sure.
you below using scan:
if pattern contains no groups, each individual result consists of matched string, $&. if pattern contains groups, each individual result array containing 1 entry per group.
"item/123) , item/245)".scan(/item\/([0-9]+)\)/).flatten # => ["123", "245"] s = "item/123) , item/245)" s.scan(/item\/([0-9]+)\)/).flatten.map(&:to_i) # them integers # => [123, 245] as have created single capturing group, got 1 result :
a = /item\/([0-9]+)\)/.match("item/123) , item/245)") a.captures # => ["123"] look method captures .
Comments
Post a Comment