From: Relm Date: 2006-09-30T15:55:23+09:00 Subject: Re: Regexp help On Fri, 29 Sep 2006, Vincent Fourmond wrote: > Hello > > > I have a string of the form > > > > 2h 3m > > > > or > > > > 3m 2h > > [....] > > Is there a smart regexp one liner that could produce > > > > [2, 3] > > If you want to get [2,3] in both cases, that will be really difficult. > As far as I know, you can only do that in C#, which has named capturing > groups. In all the other languages I know, the capturing groups are > numbered when they are found... That rules it out. irb> a => ["2h 3m", "3m 2h", "2h 3minutes", "2hour 3min", "2"] irb> re => /(?=.*\b(\d+)(?=h|\b))(?=.*\b(\d+)m|)/ irb> a.map {|x| x.match(re).captures} => [["2", "3"], ["2", "3"], ["2", "3"], ["2", "3"], ["2", nil]] -- Relm