Like Python? I'm trying to check whether each character in a string is an alphanumeric or not?
There's a special character class for this:
char.match(/^[[:alpha:]]$/)
That should match a single alphabetic character. It also seems to work for UTF-8.
To test a whole string:
string.match(/^[[:alpha:]]+$/)
Keep in mind this doesn't account for spaces or punctuation.
You can roll your own :) Replace alnum
with alpha
if you want to match only letters, without numbers.
class String
def alpha?
!!match(/^[[:alnum:]]+$/)
end
end
'asdf234'.alpha? # => true
'asdf@#$'.alpha? # => false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With