# Line breaks in multiline regexp

**URL:** <https://www.ruby-forum.com/t/line-breaks-in-multiline-regexp/135152>\
**Category:** Ruby\
**Created:** [April 18, 2008, 3:25pm UTC](https://www.ruby-forum.com/t/line-breaks-in-multiline-regexp/135152 "2008-04-18T15:25:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaschev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cab0a1/32.png) [@chaschev](https://www.ruby-forum.com/u/chaschev)\
**Post date:** [April 18, 2008, 3:25pm UTC](https://www.ruby-forum.com/t/line-breaks-in-multiline-regexp/135152/1 "2008-04-18T15:25:02Z")

</div>

Hi! I really want to know how to denote line breaks in multiline regexp.

The problem is that \s is not only a line break.

I.e. how to parse this in a general way?

s=\<\<HERE  
a loooong-loooong text with  
MANY spaces and _single_ line breaks  
followed by three empty lines

this line must be detached from the previous  
HERE

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![chaschev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cab0a1/32.png) [@chaschev](https://www.ruby-forum.com/u/chaschev)\
**Post date:** [April 19, 2008, 4:21am UTC](https://www.ruby-forum.com/t/line-breaks-in-multiline-regexp/135152/2 "2008-04-19T04:21:42Z")

</div>

Hi,

I see that Rob posted a response to this but his email came thru blank  
for me so I will answer again. Hopefully I’m not repeating his info.

“\n” is a line break and only a line break. So to detach those lines  
you would do:  
s = s.split(/\n{3}/)  
s[0] #=\> a looongtest…many spaces…followed by 3 empty lines, etc.  
s[1] #=\> this line must be detached…

Dan

---

<div class="post-metadata">

**Author:** ![chaschev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cab0a1/32.png) [@chaschev](https://www.ruby-forum.com/u/chaschev)\
**Post date:** [April 19, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/line-breaks-in-multiline-regexp/135152/3 "2008-04-19T21:30:30Z")

</div>

Oops! If I have the “signed” option in Mail.app, the response doesn’t  
make it sometimes.  
-Rob

On Apr 18, 2008, at 9:25 AM, Andrey C. wrote:

> followed by three empty lines
> 
> this line must be detached from the previous  
> HERE
> 
> Thanks in advance!

Just use \n to match the literal newline. The m option just changes  
when \n is matched by a wildcard.

irb\> s.match(/(._\n\n\n)/m).captures  
=\> [“a loooong-loooong text with\nMANY spaces and single line  
breaks\nfollowed by three empty lines\n\n\n”]  
irb\> s =~ /(._\n\n\n)/m  
=\> 0  
irb\> $1  
=\> “a loooong-loooong text with\nMANY spaces and _single_ line  
breaks\nfollowed by three empty lines\n\n\n”

Without the m

irb\> s =~ /(.\*\n\n\n)/  
=\> 69  
irb\> $1  
=\> “followed by three empty lines\n\n\n”

You can still have \n, but the .\* doesn’t match all the previous  
characters.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![chaschev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cab0a1/32.png) [@chaschev](https://www.ruby-forum.com/u/chaschev)\
**Post date:** [April 19, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/line-breaks-in-multiline-regexp/135152/4 "2008-04-19T21:32:50Z")

</div>

I reposted by earlier response, but Dan’s suggestion might be closer  
to what you want.  
-Rob

---

<div class="post-metadata">

**Author:** ![chaschev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cab0a1/32.png) [@chaschev](https://www.ruby-forum.com/u/chaschev)\
**Post date:** [April 20, 2008, 12:37am UTC](https://www.ruby-forum.com/t/line-breaks-in-multiline-regexp/135152/5 "2008-04-20T00:37:15Z")

</div>

Daniel F. wrote:

> s = s.split(/\n{3}/)

Yes, indeed this works! Thank you guys!

The problem was with my JRuby. It understands “\n” neither in a search  
string nor in a regexp. It is probably a bug. I will give you an  
example if it is interesting, when I am at work.

---

<div class="post-metadata">

**Author:** ![chaschev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cab0a1/32.png) [@chaschev](https://www.ruby-forum.com/u/chaschev)\
**Post date:** [April 21, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/line-breaks-in-multiline-regexp/135152/6 "2008-04-21T17:19:11Z")

</div>

Andrey C. wrote:

> Daniel F. wrote:
> 
> > s = s.split(/\n{3}/)
> 
> The problem was with my JRuby. It understands “\n” neither in a search  
> string nor in a regexp. It is probably a bug. I will give you an  
> example if it is interesting, when I am at work.

Sorry, this indeed works fine. Don’t know what was the problem. Thank  
you!
