Regex to Check Alphanumeric String in Ruby

Regex to check alphanumeric string in ruby

You can just check if a special character is present in the string.

def validate str
chars = ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a
str.chars.detect {|ch| !chars.include?(ch)}.nil?
end

Result:

irb(main):005:0> validate "hello"
=> true
irb(main):006:0> validate "_90 "
=> false

String must be alphanumeric validation

You might use 2 positive lookaheads (?= to assert that there is at least a character and at least a digit:

^(?=[a-zA-Z0-9]*[a-zA-Z])(?=[a-zA-Z0-9]*\d)[a-zA-Z0-9]*$

Or with the case insensitive flag and \d to match digits:

^(?=[a-z\d]*[a-z])(?=[a-z\d]*\d)[a-z\d]*$

Ruby alphanumeric check

Use Unicode or POSIX Character Classes

To validate that a string matches only alphanumeric text, you could use an anchored character class. For example:

# Use the Unicode class.
'foo' =~ /\A\p{Alnum}+\z/

# Use the POSIX class.
'foo' =~ /\A[[:alnum:]]+\z/

Anchoring is Essential

The importance of anchoring your expression can't be overstated. Without anchoring, the following would also be true:

"\nfoo" =~ /\p{Alnum}+/
"!foo!" =~ /\p{Alnum}+/

which is unlikely to be what you expect.

Regex to allow alphanumeric characters and should allow . (dot) ' (apostrophe) and - (dash)

A few things were missing:

  • Escape the last dash in the set. The - symbol denotes a range in a set, such as with a-z.
  • After the set add +, so that the characters are matched one or more times.

Expression

^[a-zA-Z0-9\.'\-]+$

REY

You could also revise it to something like ^[a-zA-Z0-9\.'\-]{5,}$, where the {5,} requires a minimum of 5 members of the set matched concurrently. Usually user names have to be longer than 1 character.

String must be alphanumeric and contain a certain substring

Use

^(?=.{1,60}$)[a-zA-Z0-9_.-]*foo\.bar[a-zA-Z0-9_.-]*$

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.{1,60} any character except \n (between 1 and
60 times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[a-zA-Z0-9_.-]* any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9', '_', '.', '-' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
foo 'foo'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
bar 'bar'
--------------------------------------------------------------------------------
[a-zA-Z0-9_.-]* any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9', '_', '.', '-' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string

Regex absolute begginer: filter alphanumeric

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

How to check if given username has at least one letter in ruby

As I understand you want to determine if the string has only letters and numbers and at least one letter. You could use the following regular expression:

r = /\A\p{Alnum}*\p{L}\p{Alnum}*\z/

This reads, "match a start-of-string anchor, followed by zero or more alphanumeric (Unicode) characters (letters or numbers), followed by a letter, followed by zero or more alphanumeric characters, followed by an end-of-string anchor".

"12abc34".match?(r) #=> true
"1234567".match?(r) #=> false
"=12abc3".match?(r) #=> false
"".match?(r) #=> false

Another way:

r = /\A(?=.*\p{L})\p{Alnum}*\z/

This reads, "match a start-of-string anchor, followed by a letter preceded by zero or more of characters, in a positive lookahead (which consumes no characters), followed by zero or more alphanumeric characters, followed by an end-of-string anchor".

Doesn't Ruby have isalpha?

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.

What is the regex to match an exact alphanumeric 18 character string?

The following regex uses positive look ahead to match:

(?=[a-zA-Z0-9]*[a-zA-Z])(?=[a-zA-Z0-9]*[0-9])[a-zA-Z0-9]{18}

It matches any group contains at least one letter, at least one number, consists of only letters and numbers and is exactly 18 characters long.

Rails - Alphanumeric field validation

I think this might help you:

/^[A-Za-z0-9-\/\.\s]+$/

This worked for all the examples you have provided

AB123-GH345 or AB45.NH744 or KHJ3/SD34 or HJS23 JKA34

and rejected when I inserted a character like ? in the middle(HJS23?JKA34).


Update

If you don't want multiline anchors then you can use it like this:

/\A[A-Za-z0-9-\/\.\s]+\z/

You can use this Rubular site to validate your Regex codes.



Related Topics



Leave a reply



Submit