# Problem with getting RegEx to work

**URL:** <https://www.ruby-forum.com/t/problem-with-getting-regex-to-work/148815>\
**Category:** Ruby\
**Created:** [September 17, 2008, 10:20am UTC](https://www.ruby-forum.com/t/problem-with-getting-regex-to-work/148815 "2008-09-17T10:20:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 17, 2008, 10:20am UTC](https://www.ruby-forum.com/t/problem-with-getting-regex-to-work/148815/1 "2008-09-17T10:20:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 17, 2008, 10:38am UTC](https://www.ruby-forum.com/t/problem-with-getting-regex-to-work/148815/2 "2008-09-17T10:38:02Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 17, 2008, 10:56am UTC](https://www.ruby-forum.com/t/problem-with-getting-regex-to-work/148815/3 "2008-09-17T10:56:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 17, 2008, 11:12am UTC](https://www.ruby-forum.com/t/problem-with-getting-regex-to-work/148815/4 "2008-09-17T11:12:36Z")

</div>

On Sep 17, 3:19 am, removed\_email\_address@domain.invalid wrote:

> " -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”]]
