Backo Backo - 1 year ago 100
Ruby Question

Regex to validate strings having only characters (without special characters but with accented characters), blank spaces and numbers

I am using Ruby on Rails 3.0.9 and I would like to validate a string that can contain only characters (case insensitive characters), blank spaces and numbers.


  • special characters are not allowed (eg: !"£$%&/()=?^) except

  • accented characters are allowed (eg: à, è, é, ò, ...);

The regex that I know from this question is
but this do not validate special characters and accented characters.

So, how I should improve the regex?

Answer Source

I wrote the ^(?:[^\W_]|\s)*$ answer in the question you referred to (which actually would have been different if I'd known you wanted to allow _ and -). Not being a Ruby guy myself, I didn't realize that Ruby defaults to not using Unicode for regex matching.

Sorry for my lack of Ruby experience. What you want to do is use the u flag. That switches to Unicode (UTF-8), so accented characters are caught. Here's the pattern you want:


And here it is in action at Rubular. This should do the trick, I think.

The u flag works on my original answer as well, though that one isn't meant to allow _ or - characters.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download