各种名称的正则表达式

时间:2021-01-06 13:04:37

标签: regex regex-group

我正在使用这个正则表达式来处理各种名称:

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

运行代码时出现此错误:

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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用

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

proof

该表达式不支持缩短的缩写名称,例如 John G. Smith

说明

--------------------------------------------------------------------------------
  ^                        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
相关问题