I need a regex to meet the following requirements:
Matches:
"Hello world."
"Hello World. This is ok."
Not Matches:
" Hello World. "
"Hello world 123."
"Hello world."
This worked in my case
<asp:RegularExpressionValidator ID="revDescription" runat="server"
ControlToValidate="taDescription" Display="Dynamic" ErrorMessage="Invalid Description."
Text=" "
ValidationExpression="^(?i)(?![ ])(?!.*[ ]{2})(?!.*[ ]$)[A-Z. ]{8,20}$"></asp:RegularExpressionValidator>
Here's a solution in Python, using anchors and negative lookahead assertions to make sure the whitespace rules are followed:
regex = re.compile(
"""^ # Start of string
(?![ ]) # Assert no space at the start
(?!.*[ ]{2}) # Assert no two spaces in the middle
(?!.*[ ]$) # Assert no space at the end
[A-Z. ]{8,20} # Match 8-20 ASCII letters, dots or spaces
$ # End of string""",
re.IGNORECASE | re.VERBOSE)