user9250371
user9250371

Reputation:

Regex match at least 3 letters

I need a regex to match

I did this ^(?=.*[a-zA-Z_-].*)(?=.*\d.*)[\w]{3,}$ but it doesn't match strings like str1, 1str, str-, str_.

Upvotes: 2

Views: 4318

Answers (2)

Constantin
Constantin

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

Andrew
Andrew

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

Related Questions