Link to home
Start Free TrialLog in
Avatar of brunoguimaraes
brunoguimaraesFlag for Brazil

asked on

Need help with regex

Hi,

I need an regex that will do the following:

1. Accept 3 characters at minimum;
2. Characters can be only letters or numbers;
3. There can be only letters or letters and numbers, but not only numbers.

I found the following on the web, but it does not accept only letters.

^(?=.*\d)(?=.*[a-zA-Z])(?!.*[\W_\x7B-\xFF]).{3,}$

I tried changing it to

^(?=.*\d)(?=.*[a-zA-Z])(?!.*[\W_\x7B-\xFF]).{3,}$ | ^[a-zA-Z]{3,}$

but it didn't work.

Any help is appreciated!
Avatar of hielo
hielo
Flag of Wallis and Futuna image

try:
^[a-zA-Z0-9]{3,}$
If it must start with a letter, then try:
^[a-zA-Z][a-zA-Z0-9]{2,}$

var re = /^(?=.*[a-z])[a-z\d]{3,}$/i;

Open in new window

^[a-zA-Z0-9]*[A-Za-z][a-zA-Z0-9{2}[a-z0-9A-Z]*
^[a-zA-Z0-9]*[A-Za-z][a-zA-Z0-9]{2}[a-z0-9A-Z]*
ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brunoguimaraes

ASKER

@hielo: your expression didn't work, as it can accept only numbers.

@ddrudik: your expression looks ok. I just had to add A-Z for uppercase letters. Thanks!

These past few days here have been hot as hell. Minimum 30º C, and it is not even summer yet! Which cities are you planning to visit?

Thanks for the solution and the explanation!
@ddrudik: Sorry! I missed the /i in the end. It works well as it is!
brunoguimaraes, adding A-Z should not be required, note the /i in the regex code that makes that pattern case-insensitive.

We will fly into São Paulo and then from there into Fortaleza (where my wife is from originally), it should be a great time.
We keep in touch with the news in Brazil on TVGlobo/Record/RBTI etc. and see her family over webcam but it's not the same as being there.
brunoguimaraes, thanks for the question and the points.
Fortaleza is great! I've been there twice. I'm from Salvador (two hours away from Fortaleza by plane). Hope you enjoy your stay!
>>@hielo: your expression didn't work, as it can accept only numbers.
I guess you missed my second post
brunoguimaraes, thanks, I will enjoy it, guaranteed.  Take care.
@hielo: sorry, but in your second expression the string must start with a letter, but I need it to be able to start with a number also.