From: Florian Gilcher Date: 2008-11-19T19:58:07+09:00 Subject: Re: RegExp problem Actually the correct regexp is: delimiter = Regexp.escape(delimiter) /#{delimiter}[^#{delimiter}]*#{delimiter}/ Read: The delimiter - an unspecified number of non-delimiter- characters - the delimiter. Otherwise, you too heavily rely on the behaviour of the library, when it coms to the dot. Regards, Florian Gilcher On Nov 19, 2008, at 11:17 AM, Jesús Gabriel y Galán wrote: > On Wed, Nov 19, 2008 at 10:55 AM, Jf Rejza > wrote: >> Hy, >> >> I would like to know how to extract a string delimited by too >> identical >> characters from an another string(of any length). >> >> ex string="rzerze@foo@rezrzgrtez" how to get (or match) the foo >> string > > irb(main):016:0> string="rzerze@foo@rezrzgrtez" > => "rzerze@foo@rezrzgrtez" > irb(main):017:0> (string.match /@(.*?)@/)[1] > => "foo" > > I don't quite understand the rest of your requirement. You mean > delimited > by a string, i.e. the @ above is a variable, or by two of any > character present > in a string? If it's the former: > > irb(main):018:0> delimiter = "@" > => "@" > irb(main):019:0> (string.match /#{delimiter}(.*?)#{delimiter}/)[1] > => "foo" > > if it's the latter, something like this might help: > > irb(main):024:0> re = Regexp.new("([#{delimiter}])(.*)?\\1") > => /([@abcde])(.*)?\1/ > irb(main):025:0> string.match(re)[2] > => "rze@foo@rezrzgrt" > > It found the first 'e' as the delimiter, don't know why it took the > last 'e' as the other part of the delimiter, since I used a non-greedy > group for the middle part. Any ideas, someone? > > Hope this helps, > > Jesus. >