How to determine the order of letters by regex? I want to write a regular expression to determine whether a given string is uppercase and sorted in non-descending order.

^A*B*C*D*E*F*G*H*I*J*K*L*M*N*O*P*Q*R*S*T*U*V*Q*X*Y*Z*$


I was just wondering if it is possible to improve the above regex.

Examples of correct strings:


  AEHIIEFKLZ


Examples of wrong strings:


  AbCDE YABKL
There is no shorter solution. Because you are enforcing order, you must list every single entity in desired order. And because you are allowing zero to many instances of each entity, you must provide the * operator. And because you want to match the entire string, you must provide the anchors on both ends.

Could you write one that runs faster? Possibly. But shorter? I don't see how.