[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

RegExp issues....

Ben V.

9/2/2006 1:58:00 AM

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.

--
Posted via http://www.ruby-....

12 Answers

Tim Hunter

9/2/2006 2:15:00 AM

0

Ben V. wrote:
> 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_-]


Eric Hodel

9/2/2006 2:23:00 AM

0

On Sep 1, 2006, at 7:14 PM, Timothy Hunter wrote:
> Ben V. wrote:
>> 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

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



Eric Hodel

9/2/2006 2:23:00 AM

0


On Sep 1, 2006, at 6:57 PM, Ben V. wrote:

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

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



Ben V.

9/2/2006 2:29:00 AM

0


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

--
Posted via http://www.ruby-....

David Vallner

9/2/2006 2:32:00 AM

0

Timothy Hunter wrote:
> 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

Paul Lutus

9/2/2006 2:35:00 AM

0

Ben V. wrote:

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

You are not accepting lowercase characters.

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.

--
Paul Lutus
http://www.ara...

Ben V.

9/2/2006 2:50:00 AM

0


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

--
Posted via http://www.ruby-....

Tim Bray

9/2/2006 4:58:00 AM

0

On Sep 1, 2006, at 6:57 PM, Ben V. wrote:

> 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


Ben V.

9/2/2006 11:37:00 AM

0

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.

--
Posted via http://www.ruby-....

Logan Capaldo

9/3/2006 1:20:00 AM

0


On Sep 2, 2006, at 7:37 AM, Ben V. wrote:

> 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.
>
> --
> Posted via http://www.ruby-....
>

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