# Try to copy text between two know string

**URL:** <https://www.ruby-forum.com/t/try-to-copy-text-between-two-know-string/177884>\
**Category:** Ruby\
**Created:** [October 26, 2009, 8:33am UTC](https://www.ruby-forum.com/t/try-to-copy-text-between-two-know-string/177884 "2009-10-26T08:33:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [October 26, 2009, 8:33am UTC](https://www.ruby-forum.com/t/try-to-copy-text-between-two-know-string/177884/1 "2009-10-26T08:33:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [October 26, 2009, 2:08pm UTC](https://www.ruby-forum.com/t/try-to-copy-text-between-two-know-string/177884/2 "2009-10-26T14:08:58Z")

</div>

Hi Ahmad,

def methode\_name  
test = 100;  
puts test  
(1…test).each { |i| puts i }

# (1…test).each { |i| puts i } # 1 to 100

end

puts methode\_name

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [October 26, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/try-to-copy-text-between-two-know-string/177884/3 "2009-10-26T14:52:53Z")

</div>

On Mon, Oct 26, 2009 at 8:33 AM, Ahmad A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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 =\<\<EOF  
irb(main):003:0" def  
irb(main):004:0" test = 100;  
irb(main):005:0" puts test  
irb(main):006:0" (1…10).each { |i| puts i }  
irb(main):007:0" end  
irb(main):008:0" EOF  
=\> “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  
=\> #\<MatchData “def\n test = 100;\n puts test\n (1…10).each { |i|  
puts i }\nend” 1:“\n test = 100;\n puts test\n (1…10).each { |i|  
puts i }\n”\>  
irb(main):012:0\> m[1]  
=\> “\n test = 100;\n puts test\n (1…10).each { |i| puts i }\n”

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [October 28, 2009, 6:17am UTC](https://www.ruby-forum.com/t/try-to-copy-text-between-two-know-string/177884/4 "2009-10-28T06:17:46Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> 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 =\<\<EOF  
> irb(main):003:0" def  
> irb(main):004:0" test = 100;  
> irb(main):005:0" puts test  
> irb(main):006:0" (1…10).each { |i| puts i }  
> irb(main):007:0" end  
> irb(main):008:0" EOF  
> =\> “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  
> =\> #\<MatchData “def\n test = 100;\n puts test\n (1…10).each { |i|  
> puts i }\nend” 1:"\n test = 100;\n puts test\n (1…10).each { |i|  
> puts i }\n"\>  
> irb(main):012:0\> m[1]  
> =\> “\n test = 100;\n puts test\n (1…10).each { |i| puts i }\n”
> 
> Hope this helps,
> 
> Jesus.

Thank you, it did helped me.  
Actually I’ve tried using the same regex as you use, which is  
/def(.\*)end/, however i overlooked the m in the end of /  
Thank you

Ahmad A.,

---

<div class="post-metadata">

**Author:** ![ahmad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@ahmad](https://www.ruby-forum.com/u/ahmad)\
**Post date:** [October 28, 2009, 9:17am UTC](https://www.ruby-forum.com/t/try-to-copy-text-between-two-know-string/177884/5 "2009-10-28T09:17:13Z")

</div>

On Wed, Oct 28, 2009 at 6:17 AM, Ahmad A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > irb(main):005:0" puts test  
> > =\> “\n test = 100;\n puts test\n (1…10).each { |i| puts i }\n”
> > 
> > Hope this helps,
> > 
> > Jesus.
> 
> Thank you, it did helped me.  
> Actually I’ve tried using the same regex as you use, which is  
> /def(.\*)end/, however i overlooked the m in the end of /

Just for the record: the m regex modifier means “multiline” and makes  
the dot match the newline character.

Jesus.
