# RegExp issues

**URL:** <https://rubytalk.org/t/regexp-issues/30586>\
**Category:** ruby-talk\
**Created:** [2 September 2006 01:57 UTC](https://rubytalk.org/t/regexp-issues/30586 "2006-09-02T01:57:57Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_V](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@Ben\_V](https://rubytalk.org/u/Ben_V)\
**Post date:** [2 September 2006 01:57 UTC](https://rubytalk.org/t/regexp-issues/30586/1 "2006-09-02T01:57:57Z")

</div>

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-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [2 September 2006 02:14 UTC](https://rubytalk.org/t/regexp-issues/30586/2 "2006-09-02T02:14:34Z")

</div>

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\_-]

---

<div class="post-metadata">

**Author:** ![Steve\_D](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@Steve\_D](https://rubytalk.org/u/Steve_D)\
**Post date:** [2 September 2006 02:19 UTC](https://rubytalk.org/t/regexp-issues/30586/3 "2006-09-02T02:19:09Z")

</div>

You need to validate lowercase also: /[A-Za-z0-9\_-]/

> **···**
>
> On Sat, 2 Sep 2006 10:57:57 +0900 "Ben V." \<comprug@gmail.com\> 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.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [2 September 2006 02:22 UTC](https://rubytalk.org/t/regexp-issues/30586/4 "2006-09-02T02:22:34Z")

</div>

That regexp doesn't match lowercase characters.

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

> **···**
>
> 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.
> 
> --  
> Eric Hodel - drbrain@segment7.net - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [2 September 2006 02:40 UTC](https://rubytalk.org/t/regexp-issues/30586/5 "2006-09-02T02:40:26Z")

</div>

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.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![Tim\_Bray](https://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@Tim\_Bray](https://rubytalk.org/u/Tim_Bray)\
**Post date:** [2 September 2006 04:58 UTC](https://rubytalk.org/t/regexp-issues/30586/6 "2006-09-02T04:58:25Z")

</div>

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

> **···**
>
> 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\_-]/

---

<div class="post-metadata">

**Author:** ![Gareth\_Adams](https://avatars.discourse-cdn.com/v4/letter/g/c68b51/32.png) [@Gareth\_Adams](https://rubytalk.org/u/Gareth_Adams)\
**Post date:** [3 September 2006 12:12 UTC](https://rubytalk.org/t/regexp-issues/30586/7 "2006-09-03T12:12:32Z")

</div>

Ben V. \<comprug \<at\> gmail.com\> writes:

> 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 strings  
/conatining/ a letter, digit hyphen or underscore:

irb(main):001:0\> /[A-Z0-9\_-]/ =~ "£$%^"  
=\> nil  
irb(main):002:0\> /[A-Z0-9\_-]/ =~ "£$0%^"  
=\> 2

You probably want something like

/^[\w-]+$/

Gareth

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [2 September 2006 02:22 UTC](https://rubytalk.org/t/regexp-issues/30586/8 "2006-09-02T02:22:34Z")

</div>

\w matches digits too

'0' =~ /\w/ # =\> 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\_-]
> 
> --  
> Eric Hodel - drbrain@segment7.net - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Ben\_V](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@Ben\_V](https://rubytalk.org/u/Ben_V)\
**Post date:** [2 September 2006 02:29 UTC](https://rubytalk.org/t/regexp-issues/30586/9 "2006-09-02T02:29:00Z")

</div>

> 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-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [2 September 2006 02:32 UTC](https://rubytalk.org/t/regexp-issues/30586/10 "2006-09-02T02:32:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ben\_V](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@Ben\_V](https://rubytalk.org/u/Ben_V)\
**Post date:** [2 September 2006 02:50 UTC](https://rubytalk.org/t/regexp-issues/30586/11 "2006-09-02T02:50:21Z")

</div>

> 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-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ben\_V](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@Ben\_V](https://rubytalk.org/u/Ben_V)\
**Post date:** [2 September 2006 11:37 UTC](https://rubytalk.org/t/regexp-issues/30586/12 "2006-09-02T11:37:07Z")

</div>

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-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [3 September 2006 12:22 UTC](https://rubytalk.org/t/regexp-issues/30586/13 "2006-09-03T12:22:25Z")

</div>

Hi --

> **···**
>
> On Sat, 2 Sep 2006, Eric Hodel wrote:
> 
> > 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.
> 
> \Z will allow a final newline character, though, so you'd probably  
> want to use \z.
> 
> David
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;David A. Black | dblack@wobblini.net  
> Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]  
> DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [3 September 2006 12:23 UTC](https://rubytalk.org/t/regexp-issues/30586/14 "2006-09-03T12:23:23Z")

</div>

Hi -

> **···**
>
> On Sun, 3 Sep 2006, Gareth Adams wrote:
> 
> > Ben V. \<comprug \<at\> gmail.com\> writes:
> > 
> > > 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 strings  
> > /conatining/ a letter, digit hyphen or underscore:
> > 
> > irb(main):001:0\> /[A-Z0-9\_-]/ =~ "£$%^"  
> > =\> nil  
> > irb(main):002:0\> /[A-Z0-9\_-]/ =~ "£$0%^"  
> > =\> 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
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;David A. Black | dblack@wobblini.net  
> Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]  
> DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [3 September 2006 01:20 UTC](https://rubytalk.org/t/regexp-issues/30586/15 "2006-09-03T01:20:22Z")

</div>

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.

> **···**
>
> 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-forum.com/\](http://www.ruby-forum.com/%5C).
