RegExp issues....

B

Ben V.

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.
 
T

Timothy Hunter

Ben 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.
Shouldn't your regexp accept lowercase as well? /[A-Za-z0-9_-]/?

Or you could simplify things a bit and use \w, which matches word
characters, and \d, which matches digits?

[\w\d_-]
 
E

Eric Hodel

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.

That regexp doesn't match lowercase characters.

input =~ /\A[\w-]+\Z/ is probably better.
 
E

Eric Hodel

Ben 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.
Shouldn't your regexp accept lowercase as well? /[A-Za-z0-9_-]/?

Or you could simplify things a bit and use \w, which matches word
characters, and \d, which matches digits?

[\w\d_-]

\w matches digits too

'0' =~ /\w/ # => 0
 
B

Ben V.

Shouldn't your regexp accept lowercase as well? /[A-Za-z0-9_-]/?
Yep, that was the issue, as someone coming from a Coldfusion backround,
I'm not that familiar with RegExps, but that's good to know that you can
just use /w, and /d... Thanks for your help and time.
 
D

David Vallner

Timothy said:
Or you could simplify things a bit and use \w, which matches word
characters, and \d, which matches digits?

I could've sworn digits are word characters too.

irb(main):001:0> /\w/ =~ "1"
=> 0

Ruby seems to agree.

David Vallner
 
B

Ben V.

For convenience, try this:

/\w-/

The special token \w translates into "A-Za-z0-9_", IOW uppercase and
lowercase letters, numbers, and underscore, leaving only the dash to be
included explicitly.

That definetly is much easier, like I said before, I am no expert at
RegEXP. I am trying to modify an image within a custom directory
created with the person's username, and because Rmagick doesn't seem to
want to work, I will have to run /usr/bin/convert, and I feel uneasy
putting user inputted text in a shell, but this regExp should do the job
of sanitizing it. Again, thank you very much for helping a
misunderstanding novice like me.
 
T

Tim Bray

I am trying to validate user input so it contains only letters, = =20
numbers,
underscores and dashes. I am using this regexp:/[A-Z0-9_-]/

There are many more letters than [A-Za-z]. You should either be =
=20
explicit that you're testing only the ASCII subset, or be prepared to=
=20
fail the first time someone includes the word 'caf=E9'. And are you =
OK =20
with '=97' as well as '-'? -Tim
 
B

Ben V.

And are you OK
with '�' as well as '-'? -Tim

Good point, well actually this will go in a url, for example if I enter
mypage, I can access my member page at mydomain.com/mypage. Allowing
question marks would be confusing, because as you know, questionmarks
have special status in URLs for passing data.
 
L

Logan Capaldo

And are you OK

Good point, well actually this will go in a url, for example if I =20
enter
mypage, I can access my member page at mydomain.com/mypage. Allowing
question marks would be confusing, because as you know, questionmarks
have special status in URLs for passing data.

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

That actually only shows up as a "question mark" because either your =20
browser or ruby-forum.com wasn't all that bright. It was actually an =20
emdash. Anyway since this is for urls, you are ok as far as domain =20
names go, because they only allow alphanumeric characters and =20
hyphens. As far as stuff after the slash goes though, that could be =20
open game, I'm not sure what the rules are.=
 
D

dblack

Hi --

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.

That regexp doesn't match lowercase characters.

input =~ /\A[\w-]+\Z/ is probably better.

\Z will allow a final newline character, though, so you'd probably
want to use \z.


David

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

dblack

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top