Regex to check alphanumeric string in ruby

前端 未结 6 1996
生来不讨喜
生来不讨喜 2021-02-19 21:40

I am trying to validate strings in ruby. Any string which contains spaces,under scores or any special char should fail validation. The valid string should contain only chars a-z

6条回答
  •  感情败类
    2021-02-19 22:02

    def alpha_numeric?(char)  
    
       if((char =~ /[[:alpha:]]) || (char =~ [[:digits:]]))
          true
       else
          false
       end
    
    end
    

    OR

    def alpha_numeric?(char)  
    
       if(char =~ /[[:alnum:]])
          true
       else
          false
       end
    
    end
    

    We are using regular expressions that match letters & digits:

    The above [[:alpha:]] ,[[:digit:]] and [[:alnum:]] are POSIX bracket expressions, and they have the advantage of matching unicode characters in their category.Hope this helps helps.

    checkout the link below for more options: Ruby: How to find out if a character is a letter or a digit?

提交回复
热议问题