[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regular expression mismatch ?

Han Holl

4/6/2005 2:45:00 PM

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


2 Answers

James Gray

4/6/2005 2:55:00 PM

0

On Apr 6, 2005, at 9:44 AM, Han Holl wrote:

> 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



Glenn Parker

4/6/2005 3:20:00 PM

0

Han Holl wrote:
>
> 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

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...