【问题标题】:Regex to forbid any space before or after apostrophes and hyphens正则表达式禁止撇号和连字符之前或之后的任何空格
【发布时间】:2020-12-12 17:15:48
【问题描述】:

我需要对此要求进行详细验证:

  1. 接受字母(A-Z 或 a-z)
  2. 可能包含此特殊字符 (åçêëèïîìæôöòûùÿáíóúñ]*$)
  3. 接受空格、撇号和连字符
  4. 可以在非空格字符之间使用撇号和连字符,例如:
  • Le'brahm
  • Ben-John

不能接受Le' brahmBen -JohnBen- John

我目前使用这个正则表达式,但无法满足数字 4 的要求 和第 3 部分

^[a-zA-Z åçêëèïîìæôöòûùÿáíóúñ]*$

如果我像这样添加连字符

^[a-zA-Z -'åçêëèïîìæôöòûùÿáíóúñ]*$

正则表达式出错,它接受数字字符(不应该)

【问题讨论】:

    标签: regex validation input


    【解决方案1】:

    你可以试试这个正则表达式:

    ^[a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+(?:[-' ][a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+)*$
    

    RegEx Demo

    正则表达式详细信息:

    • [a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+: 匹配 [...] 中的 1+ 个给定字母
    • (?:[-' ][a-zA-Zåçêëèïîìæôöòûùÿáíóúñ]+)*:匹配 0 个或多个由 -' 或空格分隔的相同字符集。

    【讨论】:

    • 嗨,我在我的 iphone 应用程序中使用 Swift,它几乎完成了,只是当我像 O'Niel 一样使用 ' 时,它不能正常工作
    • 但它匹配单词O'Niel
    • 是的,从那些网站,它通过了,但不是在我的快速代码上,很奇怪,因为 - 工作正常,但不是在'
    • 你知道为什么在 iOS 中,它不读取 ' 的东西吗?就剩下了,我试图让它像 ^[a-zA-Zåçêëèïîîìæôöòûùÿáíóúñ]+(?:['- ][a-zA-Zåçêëèïîììæôöòûùÿáíóúñ]+)*$ 但它导致了崩溃
    • 如果您通过编辑问题显示您的代码,那么我可以检查
    【解决方案2】:

    使用环视:

    ^(?!.*[-'‘’ ]{2})(?![-'‘’ ])(?!.*[-'‘’ ]$)[a-zA-Zåçêëèïîìæôöòûùÿáíóúñ '‘’-]+$
    

    proof

    说明

    --------------------------------------------------------------------------------
      ^                        the beginning of the string
    --------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    --------------------------------------------------------------------------------
        .*                       any character except \n (0 or more times
                                 (matching the most amount possible))
    --------------------------------------------------------------------------------
        [-‘’' ]{2}               any character of: '-', ''', ' ', '‘', '’' (2
                                 times)
    --------------------------------------------------------------------------------
      )                        end of look-ahead
    --------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    --------------------------------------------------------------------------------
        [-'‘’ ]                    any character of: '-', ''', ' ', '‘', '’'
    --------------------------------------------------------------------------------
      )                        end of look-ahead
    --------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    --------------------------------------------------------------------------------
        .*                       any character except \n (0 or more times
                                 (matching the most amount possible))
    --------------------------------------------------------------------------------
        [-'‘’ ]                    any character of: '-', ''', ' ', '‘', '’'
    --------------------------------------------------------------------------------
        $                        before an optional \n, and the end of
                                 the string
    --------------------------------------------------------------------------------
      )                        end of look-ahead
    --------------------------------------------------------------------------------
      [a-zA-                    any character of: 'a' to 'z', 'A' to 'Z',
      Zåçêëèïîìæôöòûùÿáíóú     'å', 'ç', 'ê', 'ë', 'è', 'ï', 'î', 'ì',
      ñ '‘’-]+                   'æ', 'ô', 'ö', 'ò', 'û', 'ù', 'ÿ', 'á',
                               'í', 'ó', 'ú', 'ñ', ' ', ''', '-', '‘', '’' (1 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      $                        before an optional \n, and the end of the
                               string
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      相关资源
      最近更新 更多