Dam Fa
Dam Fa

Reputation: 448

Regular Expression to catch a sequence of letter and digit but containing minimum 3 letters ? how to do?

I have to do a regular expression in PHP which catch in a text the strings that contain only uppercase letters, digits, or underscore. Exemple : THIS_STUFF, THIS_2_STUFF, ONE_MORE_STUFF_2_CATCH etc...
It works well with : /[A-Z0-9_]/

BUT, I want to catch the sequences that are only :
- minimum 5 chars ==> /[A-Z0-9_]{5,}/
- AND containing min 3 uppercase letters ==> ????

Have you any ideas ?

Thanks !

Upvotes: 0

Views: 101

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

AND containing min 3 uppercase letters ==> ????

Use a positive lookahead assertion at the start for this.

^(?=(?:[^A-Z\n]*[A-Z]){3})[A-Z0-9_]{5,}$

OR

\b(?=(?:[^A-Z]*[A-Z]){3})[A-Z0-9_]{5,}+\b

DEMO

Upvotes: 2

Related Questions