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

S

Stefano Crocco

Alle gioved=C3=AC 3 gennaio 2008, Sam Phoneix ha scritto:
Heres the code. Why does it miss out the "a" character?

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

Stefano
 
D

David Morton

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


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

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

David Morton
Maia Mailguard http://www.maiamailguard.com
(e-mail address removed)



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

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

Stefano Crocco

Alle gioved=C3=AC 3 gennaio 2008, Stefano Crocco ha scritto:
Alle gioved=C3=AC 3 gennaio 2008, Sam Phoneix ha scritto:

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

Stefano

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

Stefano
 
S

Sam Phoneix

David said:
-----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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top