Drongo_III Posted August 21, 2014 Share Posted August 21, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/290582-must-contain-letters-and-numbers/ Share on other sites More sharing options...
Solution Jacques1 Posted August 21, 2014 Solution Share Posted August 21, 2014 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”) 1 Quote Link to comment https://forums.phpfreaks.com/topic/290582-must-contain-letters-and-numbers/#findComment-1488577 Share on other sites More sharing options...
Drongo_III Posted August 21, 2014 Author Share Posted August 21, 2014 I never cease to be impressed by regex and the people who get it Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/290582-must-contain-letters-and-numbers/#findComment-1488578 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.