---2049402039-1771093385-1157286178=:26073
Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1771093385-1157286178=:26073"
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
---2049402039-1771093385-1157286178=:26073
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE
Hi -
Ben V. said:
I am trying to validate user input so it contains only letters, numbers,
underscores and dashes. I am using this regexp:/[A-Z0-9_-]/. However,
when I type in 'hello' in the field, it says that it doesn't match the
regexp, yet I'm sure that reg exp works for letters, numbers, and
underscores only. What am I doing wrong? Thanks for your help and time.
No one else seems to have mentioned that that regex will match all string= s
/conatining/ a letter, digit hyphen or underscore:
irb(main):001:0> /[A-Z0-9_-]/ =3D~ "=C2=A3$%^"
=3D> nil
irb(main):002:0> /[A-Z0-9_-]/ =3D~ "=C2=A3$0%^"
=3D> 2
You probably want something like
/^[\w-]+$/
Eric Hodel already suggested using \A and \Z, and I just suggested \A
and \z. The problem with ^ and $ is that they match the beginnings
and endings of lines, but not the whole string.
David
--=20
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1]
http://www.manning.com/black | [3]
http://www.rubypowerandlight.com
[2]
http://dablog.rubypal.com | [4]
http://www.rubycentral.org
---2049402039-1771093385-1157286178=:26073--
---2049402039-1771093385-1157286178=:26073--