# Validates\_format\_of \> Invalid regular expression with simple

**URL:** <https://www.ruby-forum.com/t/validates-format-of-invalid-regular-expression-with-simple/52938>\
**Category:** Rails\
**Created:** [January 20, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/validates-format-of-invalid-regular-expression-with-simple/52938 "2006-01-20T20:30:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oo00oo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oo00oo](https://www.ruby-forum.com/u/oo00oo)\
**Post date:** [January 20, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/validates-format-of-invalid-regular-expression-with-simple/52938/1 "2006-01-20T20:30:45Z")

</div>

Hello,

I try this :

validates\_format\_of :name , :with =\> /^[A-z0-9\_.-]\*$/ , :message =\>  
“bad characters”

for accept any name with chars “A” to “z” , “0” to “9” , with “\_” “.”  
“-” and " "

The pattern is really simple but I have this error :

SyntaxError in Login#register

./script/…/config/…/app/models/user.rb:6: invalid regular expression:  
/^[A-z0-9\_.-]\*$/

This is strange. I forgot something ?

---

<div class="post-metadata">

**Author:** ![oo00oo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oo00oo](https://www.ruby-forum.com/u/oo00oo)\
**Post date:** [January 20, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/validates-format-of-invalid-regular-expression-with-simple/52938/2 "2006-01-20T22:49:53Z")

</div>

On Jan 20, 2006, at 10:52 AM, oo00oo wrote:

> The pattern is really simple but I have this error :
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Try this regex instead:

/^[a-zA-Z0-9\_.-]\*$/

Cheers-

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![oo00oo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oo00oo](https://www.ruby-forum.com/u/oo00oo)\
**Post date:** [January 20, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/validates-format-of-invalid-regular-expression-with-simple/52938/3 "2006-01-20T23:29:17Z")

</div>

At 1/20/2006 01:52 PM, you wrote:

> The pattern is really simple but I have this error :
> 
> SyntaxError in Login#register
> 
> ./script/…/config/…/app/models/user.rb:6: invalid regular  
> expression: /^[A-z0-9\_.-]\*$/
> 
> This is strange. I forgot something ?

You can’t have [.-] because that looks like the range from ‘.’ to ’  
’ and space is lower than full-stop.

Try: /^[A-z0-9 \_.-]_$/ or (as someone suggested: /^[A-Za-z0-9  
\_.-]_$/ if you didn’t intend [(0x5B), \ (0x5C),] (0x5D), ^ (0x5E),  
\_ (0x5F), and ` (0x60) to be included between Z and a)

-Rob

---

<div class="post-metadata">

**Author:** ![oo00oo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oo00oo](https://www.ruby-forum.com/u/oo00oo)\
**Post date:** [January 21, 2006, 3:29am UTC](https://www.ruby-forum.com/t/validates-format-of-invalid-regular-expression-with-simple/52938/4 "2006-01-21T03:29:56Z")

</div>

In other words, if you want your regular expression to match a hypen, it  
must be the first or the last character within the []:

/^[A-z0-9\_. -]_$/  
/^[-A-z0-9\_.]_$/

Also, are you sure you want the \* quantifier, as this will also match  
the empty string. Using + may be what you are looking for in this case.

/^[-A-z0-9\_.]+$/

Cheers,

Bob

Rob B. wrote:

> At 1/20/2006 01:52 PM, you wrote:
> 
> > The pattern is really simple but I have this error :
> > 
> > SyntaxError in Login#register
> > 
> > ./script/…/config/…/app/models/user.rb:6: invalid regular  
> > expression: /^[A-z0-9\_.-]\*$/
> > 
> > This is strange. I forgot something ?
> 
> You can’t have [.-] because that looks like the range from ‘.’ to ’  
> ’ and space is lower than full-stop.
> 
> Try: /^[A-z0-9 \_.-]_$/ or (as someone suggested: /^[A-Za-z0-9  
> \_.-]_$/ if you didn’t intend [(0x5B), \ (0x5C),] (0x5D), ^ (0x5E),  
> \_ (0x5F), and ` (0x60) to be included between Z and a)
> 
> -Rob

---

<div class="post-metadata">

**Author:** ![oo00oo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oo00oo](https://www.ruby-forum.com/u/oo00oo)\
**Post date:** [January 21, 2006, 11:33am UTC](https://www.ruby-forum.com/t/validates-format-of-invalid-regular-expression-with-simple/52938/5 "2006-01-21T11:33:08Z")

</div>

Oh yes from “.” to " ", I understand my mistake.  
I also use + instead \*  
Thanks to all !  
🙂
