• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

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!
0
brunoguimaraes
Asked:
brunoguimaraes
  • 5
  • 5
  • 3
  • +1
1 Solution
 
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now