def normalized?
matches = match(/[^A-Z]*/)
return matches.size == 0
end
This is my function operating on a string, checking wether a string contains only uppercase letters. It works fine ruling out non matches, but when i call it on a string like "ABC"
it says no match, because apparently matches.size
is 1 and not zero. There seems to be an empty element in it or so.
Can anybody explain why?
Your regex is wrong - if you want it to match ONLY uppercase strings, use /^[A-Z]+$/
.
Your regular expression is incorrect. /[^A-Z]*/
means "match zero or more characters that are not between A
and Z
, anywhere in the string". The string ABC
has zero characters that are not between A
and Z
, so it matches the regular expression.
Change your regular expression to /^[^A-Z]+$/
. This means "match one or more characters that are not between A
and Z
, and make sure every character between the beginning and end of the string are not between A
and Z
". Then the string ABC
will not match, and then you can check matches[0].size
or whatever, as per sepp2k's answer.
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