making sure string contains no digits using regex in ruby?
HI
how 开发者_StackOverflowcan i ensure that a string contains no digits using regex in ruby?
thanks
\D is the character class meaning "not digit", so you could do
^\D*$
^ forces it to start at the beginning of the line, $ forces it to continue to the end of the line.
You can scan for any digit, then use !~
to match when if it cannot find one.
'1234' !~ /\d/ # => false
'12.34' !~ /\d/ # => false
'abc1def' !~ /\d/ # => false
'a1b2c3d' !~ /\d/ # => false
'12abc' !~ /\d/ # => false
'abc12' !~ /\d/ # => false
'oi9' !~ /\d/ # => false
'abc' !~ /\d/ # => true
'ABC' !~ /\d/ # => true
'aBcD' !~ /\d/ # => true
'' !~ /\d/ # => true
'日本語' !~ /\d/ # => true
'~!@#%^&*()}' !~ /\d/ # => true
精彩评论