-2

I used the following regular expression:

[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; }

How can I force user to enter space after Mr., MR., MRs., Mrs., then at least one character after the space.

For 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.

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
kuka muk
  • 349
  • 2
  • 18

4 Answers4

1

Try to replace every * with +

^(Mr. .+|MR. .+|MRS. .+|Mrs. .+|MS. .+|Ms. .+|DR. .+|Dr. .+)

I believe this could help.

1

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/

1

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))
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
0

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

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • thanks Calius one thing with `RegularExpression(@"^(Mr\. .|MR\. .|MRS\. .|Mrs\. .|MS\. .|Ms\. .|DR\. .|Dr\. .)"` it only accept one character after space – kuka muk Sep 29 '21 at 19:14
  • You'll need to use `.+` - I forgot that regular expression attributes match the whole expression – Caius Jard Sep 29 '21 at 19:47