How do I match something that is not a letter, number, or space?

I am using Ruby 2.4. How do I match something that is not a letter, number, or space? I tried

2.4.0 :004 > str = "-"
 => "-"
2.4.0 :005 > str =~ /[^[:alnum:]]*/
 => 0
2.4.0 :006 > str = " "
 => " "
2.4.0 :007 > str =~ /[^[:alnum:]]*/
 => 0

      

but as you can see it still matches a space.

+3


source to share


1 answer


Your pattern /[^[:alnum:]]*/

matches 0 or more characters other than alphanumeric characters. It will match a blank.

To match 1 or more characters other than alphanumeric and whitespace, you can use



/[^[:alnum:][:space:]]+/

      

Use a negative parenthesis expression with the appropriate POSIX character classes inside.

+1


source







All Articles