Regular expression with at least 1 letter and 1 number

I think it's simple, but I'm too dumb to write it. Can someone provide me with a regular expression that checks the given string for at least 1 letter and at least 1 number?

Also give some explanation.

+4
source share
5 answers

REs are not as good for very complex things as multiple orders, but this simple one should be represented with:

[A-Za-z].*[0-9]|[0-9].*[A-Za-z] 
+6
source

It would be best to check this separately using 2 regular expressions: /[az]/i and /\d/ .

Otherwise, if you want to apply a security rule for a password, divide it (for example, in PHP with str_split ) and count the number of occurrences of each type of character, which will give you an estimate of the strength of the password and allow you to adapt your rules.

Performance is not a problem, if it is to change the password, you do not do it for 20 thousand bites ...

+2
source

That would be much simpler with two checks, one for the string, one for the number.

Something like the following may work, although, seeing that you have both a number and a letter, you should by definition have next to the letter (or vice versa):

 ([A-Za-z][0-9]|[0-9][A-Za-z]) 

Update: removed false "|". Please note that the above assumes that no other characters are valid, which, I believe, may be unacceptable. See Another answer for a better solution if punctuation is allowed.

+1
source

Someone already answered, but you may also find this helpful ...

This is a visual regular expression editor written in .NET. Its pretty cool, you can see in real time what you get. Check here .

:)

+1
source

I don’t think it matters a lot, but I did it that way.

 ([A-Za-z])+([0-9])+|([0-9])+([A-Za-z])+ 
+1
source

Source: https://habr.com/ru/post/1286125/


All Articles