On Dec 2, 7:40 pm, Daniel DeLorme <dan... / dan42.com> wrote:
> Greg Willits wrote:
> > Greg Willits wrote:
>
> >> I'm expecting a validate_format_of with a regex like this
> >> /^[a-zA-Z\xC0-\xD6\xD9-\xF6\xF9-\xFF\.\'\-\ ]*?$/
> >> to allow many of the normal characters like =F6 =E9 =E5 to be submitted=
 via
> >> web form. However, the extended characters are being rejected.
>
> > So, I've been pounding the web for info on UTF8 in Ruby and Rails the
> > past couple days to concoct some validations that allow UTF8
> > characters. I have discovered that I can get a little further by doing
> > the
> > following:
> > - declaring $KCODE =3D 'UTF8'
> > - adding /u to regex expressions.
>
> > The only thing not working now is the ability to define a range of \x
> > characters in a regex.
>
> > So, this   /^[a-zA-Z\xE4]*?&/u will validate that a string is allowed
> > to have an =E4 in it. Perfect.
>
> > But... this fails  /^[a-zA-Z\xE4-\xE6]*?&/u
>
> > But... this works  /^[a-zA-Z\xE4\xE5\xE6]*?&/u
>
> > I've boiled the experiments down to realizing I can't define a range
> > with \x
>
> > Is this just one of those things that just doesn't work yet WRT Ruby/
> > Rails/UTF8, or is there another syntax? I've scoured all the regex
> > docs I can find, and they seem to indicate a range should work.
>
> Let me try to explain that in order to redeem myself from my previous
> angry post.
>
> Basically, \xE4 is counted as the byte value 0xE4, not the unicode
> character U+00E4. And in a range expression, each escaped value is taken
> as one character within the range. Which results in not-immediately
> obvious situations:
>
>  >> 'a=E9bvH=F6gt=E5wH=C5FuG'.scan(/[\303\251]/u)
> =3D> []
>  >> 'a=E9bvH=F6gt=E5wH=C5FuG'.scan(/[#{"\303\251"}]/u)
> =3D> ["=E9"]
>
> What is happening in the first case is that the string does not contain
> characters \303 or \251 because those are invalid utf8 sequences. But
> when the value "\303\251" is *inlined* into the regex, that is
> recognized as the utf8 character "=E9" and a match is found.
>
> So ranges *do* work in utf8 but you have to be careful:
>
>  >> "=E0=E2=E4=E7=E8=E9=EA=EE=EF=F4=FC".scan(/[=E4-=EE]/u)
> =3D> ["=E4", "=E7", "=E8", "=E9", "=EA", "=EE"]
>  >> "=E0=E2=E4=E7=E8=E9=EA=EE=EF=F4=FC".scan(/[\303\244-\303\256]/u)
> =3D> ["\303", "\303", "\303", "\244", "\303", "\247", "\303", "\250",
> "\303", "\251", "\303", "\252", "\303", "\256", "\303", "\257", "\303",
> "\264", "\303", "\274"]
>  >> "=E0=E2=E4=E7=E8=E9=EA=EE=EF=F4=FC".scan(/[#{"\303\244-\303\256"}]/u)
> =3D> ["=E4", "=E7", "=E8", "=E9", "=EA", "=EE"]
>
> Hope this helps.
>
> Dan

I missed your ranting.

Firstly, ruby doesn't have unicode support in 1.8, since unicode *IS*
a standard mapping of bytes to *characters*. That's what unicode is.
I'm sorry you don't like that, but don't lie and say ruby 1.8 supports
unicode when it knows nothing about that standard mapping and treats
everything as individual bytes (and any byte with a value greater than
126 just prints an octal escape); and please don't accuse others of
spreading FUD when they state the facts.

Secondly, as I said in my first post to this thread, the characters
trying to be matched are composite characters, which requires you to
match both bytes. You can try to using a unicode regexp, but then you
run into the problem you mention--the regexp engine expects the pre-
composed, one-byte form...

"=F2".scan(/[\303\262]/u) # =3D> []
"=F2".scan(/[\xf2]/u) # =3D> ["\303\262"]

=2E..which is why I said it's more robust to use something like the the
regexp that Jimmy linked to and I reposted, instead of a unicode
regexp.

Regards,
Jordan