Reputation:
I need a regex to match
[0-9_-]
if there are 3 letters in string already (true if str1
, str_
, str-
,---------str
, -s-t-r-
but false if 123
, ---
, ___
, 123---
, 123___
, ---___
)I did this ^(?=.*[a-zA-Z_-].*)(?=.*\d.*)[\w]{3,}$
but it doesn't match strings like str1
, 1str
, str-
, str_
.
Upvotes: 2
Views: 4318
Reputation: 218
Try this: [0-9_-]*[a-zA-Z]{3,}[0-9_-]*
. This assumes that your string must have at least 3 letters.
Upvotes: 0
Reputation: 7880
This matches what you need:
([0-9_-]*[a-z][0-9_-]*){3}
Matches:
str1
str_
1str
str-
Does not match:
123
---
___
123---
123___
---___
Do these terms come alone or are they separated by spaces, commas?
If they come one by one, you should use enclose that regex between ^ and $.
Upvotes: 1