Why is this regular expression (pat) not working? According to documentation, scan takes both string and regexp.

irb(main):001:0> pat = '\d+ (\w+) \(\d+\)'
=> "\\d+ (\\w+) \\(\\d+\\)"
irb(main):002:0> string = 'dfasdf 1 up (2009) sdfasdf 2 walle (2008) adfasdf'
=> "dfasdf 1 up (2009) sdfasdf 2 walle (2008) adfasdf"
irb(main):003:0> string.scan(pat)
=> []

Thanks.

Recommended Answers

All 2 Replies

pat = '(\d+)'
string = 'dfasdf 1 up (2009) sdfasdf 2 walle (2008) adfasdf'
p string.scan(/#{pat}/)
=>[["1"], ["2009"], ["2"], ["2008"]]

nice work.

pat = '(\d+)'
string = 'dfasdf 1 up (2009) sdfasdf 2 walle (2008) adfasdf'
p string.scan(/#{pat}/)
=>[["1"], ["2009"], ["2"], ["2008"]]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.