Regex contains at least one uppercase letter AND NOT contains at least one underscore

Viewed 198

I need a regex that contains at least one uppercase and not contains any underscore, my current try out is:

(?=.*[A-Z_])

I tried string like : example-chocolate-easter-egg-170g_349-85428671-ACCESSIBLEEASTEREGG , as this string contains _ and at least one uppercase, so should be a false. But my above regex still give true... it should be false...

Ideally , example-chocolate-easter-egg-170g349-85428671-ACCESSIBLEEASTEREGG , this is true as it does not contain _ , contains at least one uppercase letter.

and this one should be false too, as it has no uppercase : -chocolate-easter-egg-170g349-85428671-accessibleeasteregg

Any suggestions? Regex code example would be very helpful Thanks

1 Answers

Use

^(?=.*[A-Z])(?!.*_).+

See proof.

Match a string that contains an uppercase character - (?=.*[A-Z]), does not contain underscore - (?!.*_), and is not empty - .+. Remove .+ or use .* to support empty input.

Related