Jump to content

Must contain letters and numbers...


Drongo_III
Go to solution Solved by Jacques1,

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
Share on other sites

  • Solution

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”)

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.