3

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.

More:

  • special characters are not allowed (eg: !"£$%&/()=?^) except - and _;
  • accented characters are allowed (eg: à, è, é, ò, ...);

The regex that I know from this question is ^[a-zA-Z\d\s]*$ but this do not validate special characters and accented characters.

So, how I should improve the regex?

Community
  • 1
  • 1
Backo
  • 18,291
  • 27
  • 103
  • 170

3 Answers3

3

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:

^[\w\s-]*$

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.

Community
  • 1
  • 1
Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
  • Your regex '^[\w\s-]*$' match also !, ", ', ... that I would like to not match. – Backo Jul 21 '11 at 16:05
  • @Backo - [Nope, according to this test, those characters don't match](http://rubular.com/r/avVuPzggl2). `\w` only matches letters, numbers, and `_`. `\s` matches whitespace, and `-` is a literal hyphen when used at the very end of a character class. – Justin Morgan - On strike Jul 21 '11 at 16:21
  • @Backo - That's odd...Rubular uses the Ruby regex engine, so I would expect whatever works on Rubular to work for you. What version of Ruby are you using? I'm not sure why it's not working for you, but the only thing I can think of is that you might not be applying the `u` flag properly. Are you using `/^[\w\s-]*$/u` as your regex syntax? – Justin Morgan - On strike Jul 24 '11 at 17:53
  • @JustinMorgan I think that your solution doesn't work with ruby 1.9.2 I opted for adding the special characters directly into my regex. For example /^[a-zA-Z\sáÁéÉíÍóÓúÚüÜñÑ]*$/i – omrsin Nov 06 '12 at 03:17
0

Validation string only for not allowed characters. In this case |,<,>," and &.

^[^|<>\"&]*$

0

Something like ^[\w\s\-]*$ should validate characters, blank spaces, minus, and underscore.

eugen
  • 8,916
  • 11
  • 57
  • 65
  • Accented characters are in Ruby not part of `\w` according to [regular-expression.info](http://www.regular-expressions.info/refflavors.html) – stema Jul 21 '11 at 13:52
  • I checked the regex on http://rubular.com/ - it seems to work, \w matches accented chars as well. – eugen Jul 21 '11 at 13:54