4

I need a regex to meet the following requirements:

  • Only letters, periods and whitespace are allowed.
  • No white space at the beginning and at the end of the string.
  • White space in the middle of the string is OK, but not two consecutive white spaces.

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="&nbsp" 
                                ValidationExpression="^(?i)(?![ ])(?!.*[ ]{2})(?!.*[ ]$)[A-Z. ]{8,20}$"></asp:RegularExpressionValidator>
Afnan Ahmad
  • 2,492
  • 4
  • 24
  • 44
  • 2
    Hi, welcome to StackOverflow! First, I think it would be a good idea if you familiarized yourself with the formatting options of the editor - your question as stated doesn't really reflect your requirements. I'll do this for you this time, so you can see how it works. Second, you haven't really asked a question - what part of the problem are you stuck at? Third, which language are you using? All of these topics are covered in the help page and in the regex FAQ. – Tim Pietzcker Jan 05 '14 at 10:13
  • 1
    I will definately learn it asap. Thankyou for your help :) @Tim Pietzcker – Afnan Ahmad Jan 05 '14 at 10:48

2 Answers2

3

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)
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • I am using asp.net user control regular expression validator like this . Would you please guide me . Your suggested regex is not working. – Afnan Ahmad Jan 05 '14 at 10:45
  • @AfnanAhmad: You didn't make the regex case-insensitive. Add `(?i)` to the very start of the regex. – Tim Pietzcker Jan 05 '14 at 11:29
  • Thankyou so much I am done with it. I have done the following changes. . I am really thankful to you. I have searched alot but finally you gave me the answer. Super :) – Afnan Ahmad Jan 05 '14 at 11:37
0

I'd suggest to check for the length outside of the regular expression, otherwise the expression might get too complicated.

Here's an example snippet in JavaScript:

if (str.length < 8 || str.length > 20)
  return false;
if (str.match(/(^\s|\s$|\s\s|[^A-Za-z.\s])/))
  return false;

The regular expression checks for a match of any of the forbidden patterns:

  • ^\s a whitespace at the beginning
  • \s$ a whitespace at the end
  • \s\s two consecutive whitespace characters
  • [^A-Za-z.\s] a character that is not a letter, period or whitespace

If you will allow only spaces (ASCII 32), not tabs or other whitespace characters, you can replace all \s by the literal space character.

Another solution would be a combination of a “positive” expression to check for the permitted characters and length and a “negative” one to rule out the rejected patterns:

return str.match(/[A-Za-z. ]){8,20}/) && !str.match(/(^ | $|  )/);

Update: If you need to put everything into a single expression, I'm afraid you have to leave out the check for consecutive spaces, because this restriction makes the language context-sensitive, hence it cannot be checked by a regular expression. What you can do is to check for a string that begins with a letter, followed by 6 to 18 letters, dots or spaces and ends with a letter:

[A-Z][A-Z. ]{6,18}[A-Z]
ralfstx
  • 3,893
  • 2
  • 25
  • 41
  • I am using asp.net Regular Expression Validator . I have changed my code with your suggession but with no Luck. Here is the code. – Afnan Ahmad Jan 05 '14 at 11:01
  • I have left the Miimun value and maximun value validation. I will do that by applying Maxlength to textbox. Now I just want not to allow white space in start in end and not to allow two consective white spaces in the middle. Not Match = (Hello world) = ( Hello World ) – Afnan Ahmad Jan 05 '14 at 11:27
  • consective white space is the main problem I am done with start white space and end white space like this ^[a-zA-Z][\.\s\a-zA-Z]*[a-zA-Z\.]$ I just dont want the user to enter conssective spaces. – Afnan Ahmad Jan 05 '14 at 11:30