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

Discussion in 'Ruby' started by Sam Phoneix, Jan 3, 2008.

  1. Sam Phoneix

    Sam Phoneix Guest

    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/.
     
    Sam Phoneix, Jan 3, 2008
    #1
    1. Advertising

  2. 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
     
    Stefano Crocco, Jan 3, 2008
    #2
    1. Advertising

  3. Sam Phoneix

    David Morton Guest

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

    iD8DBQFHfVNzUy30ODPkzl0RAucoAJ45N5rkzbbuMGr3jHAiF1NAwfPLrgCfeYo0
    pDib5SU+hWuIJfKirdd9G6s=
    =VpNa
    -----END PGP SIGNATURE-----
     
    David Morton, Jan 3, 2008
    #3
  4. Sam Phoneix

    Mike Stok Guest

    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.
     
    Mike Stok, Jan 3, 2008
    #4
  5. 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
     
    Stefano Crocco, Jan 3, 2008
    #5
  6. Sam Phoneix

    Sam Phoneix Guest

    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)
    >
    > iD8DBQFHfVNzUy30ODPkzl0RAucoAJ45N5rkzbbuMGr3jHAiF1NAwfPLrgCfeYo0
    > pDib5SU+hWuIJfKirdd9G6s=
    > =VpNa
    > -----END PGP SIGNATURE-----


    Thanks

    --
    Posted via http://www.ruby-forum.com/.
     
    Sam Phoneix, Jan 3, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,258
    Smokey Grindel
    Dec 2, 2006
  2. Greenhorn
    Replies:
    15
    Views:
    885
    Keith Thompson
    Mar 6, 2005
  3. Simon Strandgaard

    how to expect eof with expect+pty

    Simon Strandgaard, Dec 20, 2006, in forum: Ruby
    Replies:
    4
    Views:
    393
    Simon Strandgaard
    Dec 20, 2006
  4. Phil
    Replies:
    0
    Views:
    183
  5. R
    Replies:
    1
    Views:
    124
    Dr J R Stockton
    Nov 11, 2009
Loading...

Share This Page