【问题标题】:C# Regex Match 15 Characters, Single Spaces, Alpha-NumericC# 正则表达式匹配 15 个字符、单个空格、字母数字
【发布时间】:2012-11-30 20:57:07
【问题描述】:

我需要在 C# 中使用 Regex 在以下条件下匹配一个字符串:

  1. 整个字符串只能是字母数字(包括空格)。
  2. 不得超过 15 个字符(包括空格)。
  3. 首尾字符只能是字母。
  4. 一个空格可以在除字符串的第一个和最后一个字符之外的任何位置出现多次。 (不允许有多个空格)。
  5. 应该忽略大写。
  6. 应该匹配整个单词。

如果这些先决条件中的任何一个被破坏,则不应遵循匹配。

这是我目前拥有的:

^\b([A-z]{1})(([A-z0-9 ])*([A-z]{1}))?\b$

下面是一些应该匹配的测试字符串:

  • 堆栈溢出
  • 我是最伟大的
  • 一个
  • superman23s
  • 一二三

还有一些不应该匹配的(注意空格):

  • 堆栈 [double_space] 溢出岩石
  • 23你好
  • ThisIsOver15CharactersLong
  • 你好23
  • [space_here]嘿

等等

任何建议将不胜感激。

【问题讨论】:

  • 你的正则表达式有什么问题?
  • 问题是当我只希望它匹配一个时它匹配双空格。我分享所有不相关细节的唯一原因是因为我不希望我可能不正确的正则表达式给试图匹配我设定的条件的其他人带来负担。
  • 您的第一个字符串如何匹配?超过 15 个字符

标签: c# regex


【解决方案1】:

你应该使用lookaheads

                                                               |->matches if all the lookaheads are true
                                                               --
^(?=[a-zA-Z]([a-zA-Z\d\s]+[a-zA-Z])?$)(?=.{1,15}$)(?!.*\s{2,}).*$
-------------------------------------- ----------  ----------
                 |                       |           |->checks if there are no two or more space occuring
                 |                       |->checks if the string is between 1 to 15 chars
                 |->checks if the string starts with alphabet followed by 1 to many requireds chars and that ends with a char that is not space

你可以试试here

【讨论】:

  • 啊,旧的多重前瞻匹配多重条件技巧。 (“老”是指“我在过去 48 小时内了解到这一点”。)非常好,虽然第一次前瞻至少需要两个字符
  • 这允许最后一个字符是一个数字 - OP 声明最后一个字符只能是一个字母
  • @Some1.Kill.The.DJ 没有问题 :)
【解决方案2】:

试试这个正则表达式:-

"^([a-zA-Z]([ ](?=[a-zA-Z0-9])|[a-zA-Z0-9]){0,13}[a-zA-Z])$"

解释:-

[a-zA-Z]    // Match first character letter

(                         // Capture group
    [ ](?=[a-zA-Z0-9])    // Match either a `space followed by non-whitespace` (Avoid double space, but accept single whitespace)
            |             // or
    [a-zA-Z0-9]           // just `non-whitespace` characters

){0,13}                  // from `0 to 13` character in length

[a-zA-Z]     // Match last character letter

更新:-

要处理单个字符,您可以将第一个字符之后的模式设为可选,正如 cmets 中 @Rawling 所指出的那样:-

"^([a-zA-Z](([ ](?=[a-zA-Z0-9])|[a-zA-Z0-9]){0,13}[a-zA-Z])?)$"
         ^^^                                            ^^^
     use a capture group                           make it optional

【讨论】:

  • 非常好,尽管这再次要求输入中至少有两个字符,并且进一步将最后一个字符限制为字母而不是字母或数字。
  • @Rawling.. 我认为 OP 不希望这个字符串中的最后一个字符是 digit - Hello23。他不想匹配这个。但是,它可以修改。
  • @Rawling。对于您的第一个问题,我需要看一下修改。请稍候。
  • 嗯,关于尾随数字的好点 - 规则并没有说它是不允许的,但示例确实排除了它。
  • @Rawling.. 好吧,我认为这需要进行重大修改才能使用more look-aheads。 :( 或者,一种简单但当然很奇怪的方法是使用pipe 将其作为替代方法。
【解决方案3】:

我的版本,再次使用前瞻:

^(?=.{1,15}$)(?=^[A-Z].*)(?=.*[A-Z]$)(?![ ]{2})[A-Z0-9 ]+$

解释:

^               start of string
(?=.{1,15}$)    positive look-ahead: must be between 1 and 15 chars
(?=^[A-Z].*)    positive look-ahead: initial char must be alpha
(?=.*[A-Z]$)    positive look-ahead: last char must be alpha
(?![ ]{2})      negative look-ahead: string mustn't contain 2 or more consecutive spaces
[A-Z0-9 ]+      if all the look-aheads agree, select only alpha-numeric chars + space
$               end of string

这也需要 IgnoreCase 选项设置

【讨论】:

  • (?![ ]{2} 应该是(?!.*[ ]{2}
猜你喜欢
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
相关资源
最近更新 更多