[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Problem with getting RegEx to work

henrik415

9/17/2008 8:20:00 AM

All,

I'm a novice when it comes to Ruby so I'm hoping that one of you guys
can explain the following to me.

The following line works as I expected it to and prints "This is a
test" with one word per line:
" -XThis -Xis -Xa -Xtest ".scan( /-X(.*?)\s/ ) { |x| puts x }


However this line only prints 4 lines of empty text:
" -XThis -Xis -Xa -Xtest ".scan( /\s-X(.*?)/ ) { |x| puts x }

Can someone explain this to me?
Thanks.
3 Answers

Brian Candler

9/17/2008 8:31:00 AM

0

> However this line only prints 4 lines of empty text:
> " -XThis -Xis -Xa -Xtest ".scan( /\s-X(.*?)/ ) { |x| puts x }
>
> Can someone explain this to me?

The question-mark in (.*?) makes the capture "ungreedy". It matches as
few characters as possible to make the entire regexp match. In this
case, matching zero characters is a successful match, so that's what you
get.

(.*) is "greedy" and will match as many characters as possible. So if
you use it here, it will eat everything up to the end of the line.

You might prefer something like this:

" -XThis -Xis -Xa -Xtest ".scan( /-X(\S+)/ ) { |x| puts x }

\S+ matches one or more non-space characters in a greedy fashion, hence
it will eat up to the first non-space character or end of line.

HTH,

Brian.
--
Posted via http://www.ruby-....

William James

9/17/2008 8:39:00 AM

0

On Sep 17, 3:19 am, henrik...@gmail.com wrote:
> All,
>
> I'm a novice when it comes to Ruby so I'm hoping that one of you guys
> can explain the following to me.
>
> The following line works as I expected it to and prints "This is a
> test" with one word per line:
> " -XThis -Xis -Xa -Xtest ".scan( /-X(.*?)\s/ ) { |x| puts x }
>
> However this line only prints 4 lines of empty text:
> " -XThis -Xis -Xa -Xtest ".scan( /\s-X(.*?)/ ) { |x| puts x }
>
> Can someone explain this to me?
> Thanks.

* means zero or more, so Ruby gives you
zero characters.

" -XThis -Xis -Xa -Xtest ".scan( /\s-X(\S*)/ )
==>[["This"], ["is"], ["a"], ["test"]]

henrik415

9/17/2008 8:51:00 AM

0

So this wasn't a Ruby question at all.
Thanks for the explanation guys.