Jump to content

Must contain letters and numbers...


Drongo_III

Recommended Posts

Hi Guys

 

I have a regex conundrum that at first I thought would be very easy but then appears to be quite hard :/

 

I want to match a string with a regex where the regex can only pass if it contains letters and numbers (nothing else) and it has to contain at least one of each.

 

I've been trying out lookaheads but just can't get it right.  

 

Any help is appreciated. 

 

Drongo

Link to comment
https://forums.phpfreaks.com/topic/290582-must-contain-letters-and-numbers/
Share on other sites

No need for any lookaround magic. The general logic is this: You want the string to start with at least one letter followed by at least one number followed by zero or more numbers or letters, or it should start with at least one number followed by at least one letter followed by zero or more numbers or letters.

'/\\A(?:[a-z]+\\d+|\\d+[a-z]+)[a-z\\d]*\\z/i'

(I've interpreted “letters” as “letters from the Latin alphabet”)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.