Reputation: 23
I am trying to code for the following validation in Django: "Capital Letters are allowed only as of the first-word letter or only if all letters in a word are uppercase" I have done this till now, but it is failing, Can someone please help me?
For example:
This is The GOOD day - acceptable
ThIS is THE gOOD day - not acceptable
My code:
RegexValidator(
regex='(^[A-Z][\sa-z0-9]+$)|(^([A-Z]\w*\s*)+$)',
message='Capital Letters are allowed only as first word letter or only if all letters in word are uppercase',
code='invalid_capitalisation'
)
Upvotes: 1
Views: 416
Reputation: 626748
You may use
^(?!.*[a-z][A-Z])(?!.*[A-Z][a-z]+[A-Z]).*
See the regex demo
Details
^
- start of string(?!.*[a-z][A-Z])
- no lowercase letter followed with an uppercase one anywhere in the string is allowed(?!.*[A-Z][a-z]+[A-Z])
- no uppercase letter followed with any 1 or more lowercase letters followed with an uppercase one anywhere in the string is allowed.*
- any 0 or more chars other than line break chars, as many as possibleUpvotes: 1