Cheetah
Cheetah

Reputation: 14449

Regex for alphanumber string

I am trying to match a alphanumber string that MUST have numbers and MUST have letters in it.

How do I do that?

Upvotes: 1

Views: 87

Answers (2)

FailedDev
FailedDev

Reputation: 26940

Use lookaheads:

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

In order for this match to succeed at least one ASCII number and at least on ASCII letter must be a part of the string.

Upvotes: 1

Gyrfalcon2138
Gyrfalcon2138

Reputation: 362

The first thing that comes to mind would be a regex like this:

(\d.*[a-zA-Z].* | [a-zA-Z].*\d.*)

So, a digit, anything, and a letter somewhere, or a letter, anything and a digit somewhere with no beginning or end of string markings.

It's super broad, but does that help at all?

Upvotes: 1

Related Questions