From: "Jesús Gabriel y Galán" Date: 2009-10-26T22:52:17+09:00 Subject: Re: Try to copy text between two know string On Mon, Oct 26, 2009 at 8:33 AM, Ahmad Azizan wrote: > Hello, > > I have a sample text, like this; > > eg; > def >   test = 100; >   puts test >   (1..10).each { |i| puts i } > end > > how can I copy the text in range between 'def' and 'end', which are > test = 100; > puts test > (1..10).each { |i| puts i } > > into a variable. > I've tried using ~/def ... ~/end but not matched This is one way that solves what you said. The problem with regexes is how it will handle the cases you didn't say :-) (for example, what do you want to match when there are more "ends" and so on). irb(main):002:0> text =< "def\n test = 100;\n puts test\n (1..10).each { |i| puts i }\nend\n" irb(main):011:0> m = text.match /def(.*)end/m => # irb(main):012:0> m[1] => "\n test = 100;\n puts test\n (1..10).each { |i| puts i }\n" Hope this helps, Jesus.