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!
LVL 9
brunoguimaraesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

silemoneCommented:
^[a-zA-Z0-9]*[A-Za-z][a-zA-Z0-9{2}[a-z0-9A-Z]*
0
silemoneCommented:
^[a-zA-Z0-9]*[A-Za-z][a-zA-Z0-9]{2}[a-z0-9A-Z]*
0
ddrudikCommented:
var re = /^(?=.*[a-z])[a-z\d]{3,}$/i;

^    = start of string
(?=.*[a-z])    = requires at least one a-z character somewhere within the string
[a-z\d]{3,}    = requires three or more 0-9 or a-z characters total within the string
$    = end of string
i    = match case-insensitive, match a-z and A-Z with the above pattern.

BTW, I am jealous you are in Brazil, looking forward to being there next year.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brunoguimaraesAuthor Commented:
@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?

0
brunoguimaraesAuthor Commented:
Thanks for the solution and the explanation!
0
brunoguimaraesAuthor Commented:
@ddrudik: Sorry! I missed the /i in the end. It works well as it is!
0
ddrudikCommented:
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.
0
ddrudikCommented:
brunoguimaraes, thanks for the question and the points.
0
brunoguimaraesAuthor Commented:
Fortaleza is great! I've been there twice. I'm from Salvador (two hours away from Fortaleza by plane). Hope you enjoy your stay!
0
hieloCommented:
>>@hielo: your expression didn't work, as it can accept only numbers.
I guess you missed my second post
0
ddrudikCommented:
brunoguimaraes, thanks, I will enjoy it, guaranteed.  Take care.
0
brunoguimaraesAuthor Commented:
@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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.