[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Regexp captures

Peña, Botp

1/31/2007 2:11:00 AM

On Behalf Of El Gato:
# (c = result.match(/id: (\d+)/)) ? c.captures.first :
# result.scan(/error: (.*)/).to_s

why not just build the regex and then capture/match.

irb(main):067:0> re = /(id|error): ((\d+)|(.*))/
=> /(id|error): ((\d+)|(.*))/
irb(main):068:0> result = "id: 1234"
=> "id: 1234"
irb(main):069:0> c = result.match(re)[2].to_s
=> "1234"
irb(main):070:0> result = "error: this is an error"
=> "error: this is an error"
irb(main):071:0> c = result.match(re)[2].to_s
=> "this is an error"

kind regards -botp