【问题标题】:Full name regex in RubyRuby 中的全名正则表达式
【发布时间】:2014-04-18 16:22:30
【问题描述】:

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

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

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

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}/
        }

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

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

我不想将此字段拆分为 2 个不同的字段名和姓,因为这会使用户界面复杂化。

【问题讨论】:

  • 姓氏是否包含多个单词,例如“Van Damme”?
  • 我相信它的拼写是“Van Damage”。
  • 是的,忘了说。它可能包含超过 2 个单词,但不能少于 2 个

标签: ruby-on-rails regex validation whitespace


【解决方案1】:

您可以匹配以下regex:

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

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

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

它去掉了多余的空格,只保留一个,如你所愿。

演示:http://regex101.com/r/oQ6aO7

【讨论】:

  • 谢谢,正是我要找的...但是我在哪里包含'\1 \3'?它应该是正则表达式的一部分吗?
  • 它将在您替换的部分中。
【解决方案2】:
result = subject.gsub(/\A(?=[\w' -]{5,64})([\w'-]{2,})([\s]{1})\s*?([\w'-]{2,})\Z/, '\1\2\3')

http://regex101.com/r/dT1fJ4

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) «$»

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 2011-04-05
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多