I used the following regular expression. How can I force user to enter space after Mr., MR., MRs., Mrs,. then at least one character after the space .
[RegularExpression(@"^(Mr. .*|MR. .*|MRS. .*|Mrs. .*|MS. .*|Ms. .*|DR. .*|Dr. .*)", ErrorMessage = "Greeting must begin with Mr., Mrs., Ms., or Dr. and be followed by a name.")]
public string? Greeting { get; set; }
Fore example: if they enter Mr. Joe Doe
or Mr. Joe
is valid but they enter Mr.
with space only or Mr.
without space is invalid.
CodePudding user response:
Try to replace every * with
^(Mr. . |MR. . |MRS. . |Mrs. . |MS. . |Ms. . |DR. . |Dr. . )
I believe this could help.
CodePudding user response:
You can try something like this to get started
^[Mr\.] [ ] [A-Za-z]{0,25}[ ]?[A-za-z]{1,25}
This should match the use cases you gave.
The backslash escapes the period so it matches a literal period.
The curly brackets give a range of how many patterns are allowed of the grouping before it.
The question mark makes the bracket grouping before it optional.
Also, give this site a shot to try out your regex in real time. https://regexr.com/
CodePudding user response:
Use
^(?:[DM][rR][sS]?|M[Ss])\. .
See proof.
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
[DM] any character of: 'D', 'M'
--------------------------------------------------------------------------------
[rR] any character of: 'r', 'R'
--------------------------------------------------------------------------------
[sS]? any character of: 's', 'S' (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
M 'M'
--------------------------------------------------------------------------------
[Ss] any character of: 'S', 's'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
' '
--------------------------------------------------------------------------------
. any character except \n (1 or more times
(matching the most amount possible))
CodePudding user response:
Couple of issues
.
matches "any character" so saying Mr.
allows someone to write Mra
Mrb
etc - put a \
before it or put it in a character class (surround it with [
]
)
*
means "zero or more of" so a regex of Mr. .*
allows them to write zero characters for the name. To quantify "one or more" we use
instead of *
If you want to do away with repetitive elements you could
(M[rRsS]|M(RS|rs)|D[rR])\. \w
The first one takes care of Mr and Ms, second takes care of Mrs, third Dr, then is the common element of "literally a period" followed by a succession of word characters