Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Why does this code not return what I expect it to?

Reply
Thread Tools

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

 
 
Sam Phoneix
Guest
Posts: n/a
 
      01-03-2008
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/.

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      01-03-2008
Alle gioved=C3=AC 3 gennaio 2008, Sam Phoneix 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'=20
can't match /\w\w/.

Stefano

 
Reply With Quote
 
 
 
 
David Morton
Guest
Posts: n/a
 
      01-03-2008
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Jan 3, 2008, at 3:23 PM, Sam Phoneix wrote:

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




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

iD8DBQFHfVNzUy30ODPkzl0RAucoAJ45N5rkzbbuMGr3jHAiF1 NAwfPLrgCfeYo0
pDib5SU+hWuIJfKirdd9G6s=
=VpNa
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Mike Stok
Guest
Posts: n/a
 
      01-03-2008

On 3-Jan-08, at 4:23 PM, Sam Phoneix 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/.



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 Stok <>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.





 
Reply With Quote
 
Stefano Crocco
Guest
Posts: n/a
 
      01-03-2008
Alle gioved=C3=AC 3 gennaio 2008, Stefano Crocco ha scritto:
> Alle gioved=C3=AC 3 gennaio 2008, Sam Phoneix 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


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

 
Reply With Quote
 
Sam Phoneix
Guest
Posts: n/a
 
      01-03-2008
David Morton 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)
>
> iD8DBQFHfVNzUy30ODPkzl0RAucoAJ45N5rkzbbuMGr3jHAiF1 NAwfPLrgCfeYo0
> pDib5SU+hWuIJfKirdd9G6s=
> =VpNa
> -----END PGP SIGNATURE-----


Thanks

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
how to expect eof with expect+pty Simon Strandgaard Ruby 4 12-20-2006 04:00 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Bug in $obj->expect() ... ? (Expect 1.15) Phil Perl Misc 0 07-07-2006 07:25 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments