• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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