why does . match non-ascii chars?

Discussion in 'Ruby' started by 7stud --, Feb 23, 2009.

  1. 7stud --

    7stud -- Guest

    str = "abcdéf "

    result = str.gsub(/./n) do |match|
    puts "%%%02X" % match[0]
    end
    puts


    --output:--
    %61
    %62
    %63
    %64
    %C3
    %A9
    %66


    Doesn't the 'n' option say to match ascii? For what it's worth, I get
    the same result without the 'n' option.
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Feb 23, 2009
    #1
    1. Advertising

  2. On Tue, Feb 24, 2009 at 1:34 AM, 7stud -- <> wrote:
    > str =3D "abcd=C3=A9f "
    >
    > result =3D str.gsub(/./n) do |match|
    > puts "%%%02X" % match[0]
    > end
    > puts
    >
    >
    > --output:--
    > %61
    > %62
    > %63
    > %64
    > %C3
    > %A9
    > %66
    >
    >
    > Doesn't the 'n' option say to match ascii? For what it's worth, I get
    > the same result without the 'n' option.


    The default switch of a regex is actually 'n' already, that only
    changes if you set $KCODE before.
    It has little influence on what is matched when it comes to '.', but
    it influences how the matched bytes will be grouped to resemble
    characters.

    sigma ~ % ruby -e 'p "abcd=C3=A9f ".scan(/./)'
    ["a", "b", "c", "d", "\303", "\251", "f", " "]

    sigma ~ % ruby -e 'p "abcd=C3=A9f ".scan(/./u)'
    ["a", "b", "c", "d", "\303\251", "f", " "]

    sigma ~ % ruby -Kue 'p "abcd=C3=A9f ".scan(/./u)'
    ["a", "b", "c", "d", "=C3=A9", "f", " "]

    sigma ~ % ruby19 -e 'p "abcd=C3=A9f ".scan(/./)'
    ["a", "b", "c", "d", "=C3=A9", "f", " "]

    Please see some excellent articles about this topic from James Edward Gray =
    II:

    http://blog.grayproductions.net/articles/bytes_and_characters_in_ruby_18
    http://blog.grayproductions.net/categories/character_encodings

    ^ manveru
    Michael Fellinger, Feb 24, 2009
    #2
    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:
    1,739
    Smokey Grindel
    Dec 2, 2006
  2. Replies:
    3
    Views:
    770
  3. TOXiC
    Replies:
    5
    Views:
    1,216
    TOXiC
    Jan 31, 2007
  4. bruce
    Replies:
    38
    Views:
    255
    Mark Lawrence
    Nov 1, 2013
  5. MRAB
    Replies:
    0
    Views:
    85
Loading...

Share This Page