# Why does this code not return what I expect it to?

**URL:** <https://www.ruby-forum.com/t/why-does-this-code-not-return-what-i-expect-it-to/124943>\
**Category:** Ruby\
**Created:** [January 3, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/why-does-this-code-not-return-what-i-expect-it-to/124943 "2008-01-03T22:23:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 3, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/why-does-this-code-not-return-what-i-expect-it-to/124943/1 "2008-01-03T22:23:18Z")

</div>

## Heres the code. Why does it miss out the “a” character?

## “This is a test”.scan(/\w\w/) {|x| puts x}

Thanks

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 3, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/why-does-this-code-not-return-what-i-expect-it-to/124943/2 "2008-01-03T22:28:30Z")

</div>

Alle giovedÃ¬ 3 gennaio 2008, Sam P. ha scritto:

> ## Heres the code. Why does it miss out the “a” character?
> 
> ## “This is a test”.scan(/\w\w/) {|x| puts x}
> 
> Thanks

I guess because the space before the ‘a’ is not a word character, so ’  
a’  
can’t match /\w\w/.

Stefano

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 3, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/why-does-this-code-not-return-what-i-expect-it-to/124943/3 "2008-01-03T22:29:41Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Jan 3, 2008, at 3:23 PM, Sam P. wrote:

> “This is a test”.scan(/\w\w/) {|x| puts x}

your regex mataches 2 letter word characters. “a” is only one.

David M.  
Maia Mailguard [http://www.maiamailguard.com](http://www.maiamailguard.com)  
removed\_email\_address@domain.invalid

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFHfVNzUy30ODPkzl0RAucoAJ45N5rkzbbuMGr3jHAiF1NAwfPLrgCfeYo0  
pDib5SU+hWuIJfKirdd9G6s=  
=VpNa  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 3, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/why-does-this-code-not-return-what-i-expect-it-to/124943/4 "2008-01-03T22:30:51Z")

</div>

On 3-Jan-08, at 4:23 PM, Sam P. wrote:

> ## Heres the code. Why does it miss out the “a” character?
> 
> ## “This is a test”.scan(/\w\w/) {|x| puts x}
> 
> ## Thanks
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Have you researched what the \w matches in a regular expression, and  
considered how many of them there are in the argument to scan?

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 3, 2008, 10:35pm UTC](https://www.ruby-forum.com/t/why-does-this-code-not-return-what-i-expect-it-to/124943/5 "2008-01-03T22:35:31Z")

</div>

David M. wrote:

> -----BEGIN PGP SIGNED MESSAGE-----  
> Hash: SHA1  
> your regex mataches 2 letter word characters. “a” is only one.  
> -----BEGIN PGP SIGNATURE-----  
> Version: GnuPG v1.4.7 (Darwin)
> 
> iD8DBQFHfVNzUy30ODPkzl0RAucoAJ45N5rkzbbuMGr3jHAiF1NAwfPLrgCfeYo0  
> pDib5SU+hWuIJfKirdd9G6s=  
> =VpNa  
> -----END PGP SIGNATURE-----

Thanks

---

<div class="post-metadata">

**Author:** ![sdpurtill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@sdpurtill](https://www.ruby-forum.com/u/sdpurtill)\
**Post date:** [January 3, 2008, 10:35pm UTC](https://www.ruby-forum.com/t/why-does-this-code-not-return-what-i-expect-it-to/124943/6 "2008-01-03T22:35:33Z")

</div>

Alle giovedÃ¬ 3 gennaio 2008, Stefano C. ha scritto:

> Stefano  
> Actually, the situation is a little more complex than I first thought,  
> because  
> there are other characters near spaces which are included in the result.  
> The  
> difference comes from the fact that ‘a’ has a space before and a space  
> after  
> it. The character ‘i’ of ‘it’, instead, is printed because ’ i’ doesn’t  
> match, but ‘it’ does. With the ‘a’, there is no matching: neither ’ a’  
> nor 'a ’ match. The same happens for each word containing an odd number  
> of  
> characters. For instance, replacing ‘is’ with ‘isx’, you don’t get the  
> ‘x’ in  
> the output.

Stefano
