开发者

making sure string contains no digits using regex in ruby?

开发者 https://www.devze.com 2023-01-07 07:23 出处:网络
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

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号