Regex for all sort of name

Viewed 127

I am using this regex for handling all sort of names:

String Regex_Name="^([A-Za-z]*|\\p{L})+([ ]*|[A-Za-z]*|[']*|\\p{L}*)+([\\s]?[A-Za-z]*)+[A-Za-z]$";

While running the code I am getting this error:

Unknown character property name {​​​​​​​L} near index 44
^[A-Za-z][[A-Za-z]*\p{​​​​​​​L}​​​​​​​*[,]?[ ]?[-]?[A-Za-z]+]+([ ]?[.]?[,]?[(]?[A-Za-z]+[)]?[-]?\p{​​​​​​​L}​​​​​​​*)+([,]?|[.]?)$

How can I solve the issue?

1 Answers

Use

String Regex_Name="^\\p{L}+(?:[’'-]\\p{L}+)*(?:\\s+\\p{L}+(?:[’'-]\\p{L}+)*)*$";

See proof.

The expression does not support shortened, abbreviated names, like John G. Smith.

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \p{L}+                   any character of: letters (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    [’'-]                    any character of: '’', ''', '-'
--------------------------------------------------------------------------------
    \p{L}+                   any character of: letters (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )*                       end of grouping
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
   \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
    \p{L}+                   any character of: letters (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      [’'-]                    any character of: '’', ''', '-'
--------------------------------------------------------------------------------
      \p{L}+                   any character of: letters (1 or more
                               times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
  )*                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Related