line breaks in multiline regexp

A

Andrey Chaschev

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!
 
D

Daniel Finnie

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
 
R

Rob Biedenharn

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

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!

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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Rob Biedenharn

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

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
 
A

Andrey Chaschev

Daniel said:
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.
 
A

Andrey Chaschev

Andrey said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top