# Explain this ruby regex

**URL:** <https://www.ruby-forum.com/t/explain-this-ruby-regex/150093>\
**Category:** Ruby\
**Created:** [October 3, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/explain-this-ruby-regex/150093 "2008-10-03T17:40:58Z")\
**Posts on this page:** 7\
**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:** [October 3, 2008, 5:40pm UTC](https://www.ruby-forum.com/t/explain-this-ruby-regex/150093/1 "2008-10-03T17:40:58Z")

</div>

Can someone explain this regex …

“one two”.scan(/\w\*/).length

returns 4. I can see it matching the 2 words and the space, what else  
is it matching on? Is there a null terminator, I thought Ruby strings  
were not null termed.

---

<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:** [October 3, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/explain-this-ruby-regex/150093/2 "2008-10-03T17:46:14Z")

</div>

On Sat, Oct 04, 2008, removed\_email\_address@domain.invalid wrote:

> “one two”.scan(/\w\*/).length
> 
> returns 4. I can see it matching the 2 words and the space, what else  
> is it matching on? Is there a null terminator, I thought Ruby strings  
> were not null termed.

Try replacing #length with #inspect and seeing what the output of scan  
is. You’ll find that it’s returning two empty strings as well. I  
suspect what you really want is \w+…

Ben

---

<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:** [October 3, 2008, 7:56pm UTC](https://www.ruby-forum.com/t/explain-this-ruby-regex/150093/3 "2008-10-03T19:56:44Z")

</div>

On Oct 3, 11:44 am, Ben B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ben

Yeah, you’re right \w+ will pull out the words, which is what I want  
anyway. Though I’m trying to understand what \w\* is doing.  
irb(main):015:0\> “one two”.scan(/\w\*/).inspect  
=\> “["one", "", "two", ""]”

My question is, what is the last "", where does it come from.

---

<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:** [October 3, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/explain-this-ruby-regex/150093/4 "2008-10-03T20:38:01Z")

</div>

\w\* does not match the space between string “one” and “two”. it matches  
“one”, \<empty string after “one”\>, “two”, \<empty string after “two”\>.

There are some other examples:

```
irb(main):004:0> "one".scan(/^\w*/)
=> ["one"]
irb(main):005:0> "one".scan(/\w*$/)
=> ["one", ""]

```

–  
Patrick

---

<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:** [October 3, 2008, 9:29pm UTC](https://www.ruby-forum.com/t/explain-this-ruby-regex/150093/5 "2008-10-03T21:29:41Z")

</div>

The key idea here is that "_" means “match zero or more of” whereas “+”  
means “match one or more of”. So, when you match \w_ against “one two”,  
there are zero or more instances of a word character (3, in fact, ‘o’,  
‘n’,  
and ‘e’), so that produces one result. Following that result, there are  
zero matches of a word character, but since you asked for “zero or more  
of”,  
you get that empty string result. Later, rinse, repeat for the “two”  
part.

FWIW, instead of looking at the result with #inspect, I found it more  
informative to look at the result returned from #scan by itself, e.g.

irb\> “one two”.scan(/\w\*/)  
=\> [“one”, “”, “two”, “”]

–wpd

---

<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:** [October 5, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/explain-this-ruby-regex/150093/6 "2008-10-05T18:21:13Z")

</div>

On 03.10.2008 18:44, Patrick D. wrote:

> The key idea here is that "_" means “match zero or more of” whereas “+”  
> means “match one or more of”. So, when you match \w_ against “one two”,  
> there are zero or more instances of a word character (3, in fact, ‘o’, ‘n’,  
> and ‘e’), so that produces one result. Following that result, there are  
> zero matches of a word character, but since you asked for “zero or more of”,  
> you get that empty string result. Later, rinse, repeat for the “two” part.

It boils down to this statement: a subexpression with “\*” potentially  
matches an _empty string anywhere_ in a string.

Kind regards

robert

---

<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:** [October 3, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/explain-this-ruby-regex/150093/7 "2008-10-03T21:47:42Z")

</div>

> FWIW, instead of looking at the result with #inspect, I found it more  
> informative to look at the result returned from #scan by itself, e.g.
> 
> irb\> “one two”.scan(/\w\*/)  
> =\> [“one”, “”, “two”, “”]

irb displays the expression value using “inspect”, so you are using  
inspect even though you didn’t ask for it 🙂
