Regular expression mismatch ?

H

Han Holl

Hello,

Why does the following:

s = "aaa aaa\n\n\nbbb bbb"
puts(s =~ /^\s+$/)

produce: 8 (instead of nil) ?

(If I put in only 2 newlines, it's fine).

This is in several 1.8.2 rpms (caliban, fc3).

Cheers,

Han Holl
 
J

James Edward Gray II

Hello,

Why does the following:

s = "aaa aaa\n\n\nbbb bbb"
puts(s =~ /^\s+$/)

produce: 8 (instead of nil) ?

^ means the beginning a line (right after \n)
$ means the end a line (right before \n)
\s+ manes one or more whitespace characters (the \n in the middle)

Does that clarify the match?
(If I put in only 2 newlines, it's fine).

You can't satisfy all three conditions with only two \n characters.

Hope that helps.

James Edward Gray II
 
G

Glenn Parker

Han said:
Why does the following:

s = "aaa aaa\n\n\nbbb bbb"
puts(s =~ /^\s+$/)

produce: 8 (instead of nil) ?

Maybe you would rather use \A and \Z instead of ^ and $:

puts(s =~ /\A\s+\Z/) => nil
 

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

Forum statistics

Threads
474,170
Messages
2,570,921
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top