0

I'm playing codewars in Ruby and I'm stuck on a Kata. The goal is to validate if a user input string is alphanumeric. (yes, this is quite advanced Regex)

The instructions:

At least one character ("" is not valid)
Allowed characters are uppercase / lowercase latin letters and digits from 0 to 9
No whitespaces/underscore

What I've tried :

^[a-zA-Z0-9]+$
^(?! !)[a-zA-Z0-9]+$
^((?! !)[a-zA-Z0-9]+)$

It passes all the test except one, here's the error message:

Value is not what was expected

I though the Regex I'm using would satisfy all the conditions, what am I missing ?


SOLUTION: \A[a-zA-Z0-9]+\z (and better Ruby :^) )
  • $ => end of a line
  • \z => end of a string

(same for beginning: ^ (line) and \A (string), but wasn't needed for the test)

Favourite answer from another player:

/\A[A-z\d]+\z/
Sumak
  • 927
  • 7
  • 21

2 Answers2

1

My guess is that maybe, we would start with an expression similar to:

^(?=[A-Za-z0-9])[A-Za-z0-9]+$

and test to see if it might cover our desired rules.

In this demo, the expression is explained, if you might be interested.

Test

re = /^(?=[A-Za-z0-9])[A-Za-z0-9]+$/m
str = ' 
ab
c
def
abc*
def^
'

# Print the match result
str.scan(re) do |match|
    puts match.to_s
end
Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    That's the same as `r = /^[A-Za-z0-9]+$/`, but try this `"a1\n@".match?(r) #=> true`. Can you spot the problem? – Cary Swoveland Jun 26 '19 at 17:34
  • 1
    It doesn't works, but thank you a lot because I didn't knew we could pass a block to `scan`. It's gonna help me a lot to do some test ! Also I don't really get the goal of `(?=.)`, could you briefly explain ? – Sumak Jun 26 '19 at 17:53
  • 1
    @CarySwoveland The problem comes from `$` which means _"end of the line"_ ? When it meets `\n` it stops scanning and return `true` because `a1` is valid and `@` is ignored ? So I guess this is why we should use `\z` right ? – Sumak Jun 26 '19 at 17:58
  • 1
    @Emma thanks for the advises ! I'll keep experimenting, it looks like there's sooo much to know about regular expressions ! Hopefully regex101 helps a lot Have a great day/evening :) – Sumak Jun 26 '19 at 18:10
  • 1
    @Sumak, yes, here we need to use the string anchors `\A` and `\z`. When it is known that the string contains no newline characters you can use either those anchors of the line anchors `^` and `$`, but it's best to use the set of anchors that reflects your intent, in part so that you don't confuse readers. If I see line anchors where string anchors are intended I need to waste time examining the code to ensure the the coder did not have a reason for avoiding the use of string anchors. Also, in this case an error may be introduced if in future strings may contain newline characters. – Cary Swoveland Jun 26 '19 at 18:57
0
str !~ /[^A-Za-z\d]/

The string contains alphanumeric characters only if and only if it does not contain a character other than an alphnumeric character.

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100