Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby中的全名正则表达式_Ruby On Rails_Regex_Validation_Whitespace - Fatal编程技术网

Ruby on rails Ruby中的全名正则表达式

Ruby on rails Ruby中的全名正则表达式,ruby-on-rails,regex,validation,whitespace,Ruby On Rails,Regex,Validation,Whitespace,我知道有很多类似的问题,但我在任何地方都找不到我的案例 我试图在RubyonRails用户模型中编写一个全名RegEx。 它应该验证名字和姓氏是否用一个空格填充。两个名称应至少包含2个字符(例如:Li Ma) 作为奖励,但不是必要的,我想将空白区域修剪为一个字符,以防用户键入错误并输入多个空白区域(例如:Li Ma将被修剪为Li Ma) 目前我正在这样验证它(警告:它可能不正确): 这对我来说是可行的,但是没有检查每个名字至少2个字符(例如:Peter p现在是正确的)。这也接受多个不好的空格(

我知道有很多类似的问题,但我在任何地方都找不到我的案例

我试图在RubyonRails用户模型中编写一个全名RegEx。 它应该验证名字和姓氏是否用一个空格填充。两个名称应至少包含2个字符(例如:Li Ma)

作为奖励,但不是必要的,我想将空白区域修剪为一个字符,以防用户键入错误并输入多个空白区域(例如:Li Ma将被修剪为Li Ma) 目前我正在这样验证它(警告:它可能不正确):

这对我来说是可行的,但是没有检查每个名字至少2个字符(例如:Peter p现在是正确的)。这也接受多个不好的空格(例如:Peter P)

我知道识别姓名的问题非常以文化为中心,可能不是验证全名的正确方法(可能有人只有一个字符的名字),但这是目前的一项要求


我不想将此字段拆分为两个不同的字段“名字”和“姓氏”,因为这会使用户界面变得复杂。

您可以匹配以下
regex

/([\w\-\']{2,})([\s]+)([\w\-\']{2,})/

并替换为:(假设它支持捕获组)

'\1\3'
$1$3
无论语法是什么:

它去掉了多余的空格,只保留了一个,这是你想要的

演示:


如果姓氏由一个以上的单词组成,比如“Van Damme”?我相信它的拼写是“Van Damme”。是的,忘了提到。它可能包含两个以上的单词,但不能少于两个,这正是我想要的。。。但是在哪里包括“\1\3”?它应该是正则表达式的一部分吗?它应该在您替换的部分中。
validates :name,
        presence: true,
        length: {
            maximum: 64,
            minimum: 5,
            message: 'must be a minimum: 5 letters and a maximum: 64 letters'},
        format: {
            # Full Name RegEx
            with: /[\w\-\']+([\s]+[\w\-\']){1}/
        }
result = subject.gsub(/\A(?=[\w' -]{5,64})([\w'-]{2,})([\s]{1})\s*?([\w'-]{2,})\Z/, '\1\2\3')
Assert position at the beginning of the string «^»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=[\w' -]{5,64})»
   Match a single character present in the list below «[\w' -]{5,64}»
      Between 5 and 64 times, as many times as possible, giving back as needed (greedy) «{5,64}»
      A word character (letters, digits, and underscores) «\w»
      The character “'” «'»
      The character “ ” « »
      The character “-” «-»
Match the regular expression below and capture its match into backreference number 1 «([\w'-]{2,})»
   Match a single character present in the list below «[\w'-]{2,}»
      Between 2 and unlimited times, as many times as possible, giving back as needed (greedy) «{2,}»
      A word character (letters, digits, and underscores) «\w»
      The character “'” «'»
      The character “-” «-»
Match the regular expression below and capture its match into backreference number 2 «([\s]{1})»
   Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «[\s]{1}»
      Exactly 1 times «{1}»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the regular expression below and capture its match into backreference number 3 «([\w'-]{2,})»
   Match a single character present in the list below «[\w'-]{2,}»
      Between 2 and unlimited times, as many times as possible, giving back as needed (greedy) «{2,}»
      A word character (letters, digits, and underscores) «\w»
      The character “'” «'»
      The character “-” «-»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»