# Help on Regular EXpression

**URL:** <https://www.ruby-forum.com/t/help-on-regular-expression/170171>\
**Category:** Rails\
**Created:** [June 13, 2009, 6:35am UTC](https://www.ruby-forum.com/t/help-on-regular-expression/170171 "2009-06-13T06:35:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [June 13, 2009, 6:35am UTC](https://www.ruby-forum.com/t/help-on-regular-expression/170171/1 "2009-06-13T06:35:39Z")

</div>

hi  
i need to validate user input for my designation field .  
that field should accept(alphapets,numbers,-,&)  
example =\> System support - Trainee  
could u people help me with the reqular expression.  
Thanks for any helps

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [June 13, 2009, 6:45am UTC](https://www.ruby-forum.com/t/help-on-regular-expression/170171/2 "2009-06-13T06:45:30Z")

</div>

/^([-a-z0-9-&]+)$/i

Should do it

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [June 13, 2009, 11:02am UTC](https://www.ruby-forum.com/t/help-on-regular-expression/170171/3 "2009-06-13T11:02:31Z")

</div>

Am 13.06.2009 um 06:44 schrieb heimdull:

> /^([-a-z0-9-&]+)$/i
> 
> Should do it

I’d be careful with ^ and $ in regular expressions. I haven’t tested  
it, but that would also match ‘Some stuff\nSome other funny stuffäää’,  
because ^ and $ match the beginning and end of a line, if your string  
has several lines, you won’t test the whole string. If you really want  
to test the whole string, use \A and \Z to match the beginning and the  
end of the string. The Regex would be:

/\A[-a-z0-9-&]+\Z/i

Anyway, you can also visit [http://www.rubular.com/](http://www.rubular.com/) to test your regexes.

Regards,

Felix
