代码之家  ›  专栏  ›  技术社区  ›  SetiSeeker

排除双空格的正则表达式

  •  1
  • SetiSeeker  · 技术社区  · 15 年前

    我正在为C ASP.NET 3.5寻找一个正则表达式,如果一个句子或一组单词中有任何双空格,它将失败。

    the cat chased the dog = true
    the  cat  chased  the dog = false (doubles spaces occur at random intervals)
    

    谢谢

    4 回复  |  直到 15 年前
        1
  •  2
  •   Jens    15 年前

    尝试

    ^((?!\s{2}).)*$
    

    在这个表达式中 (?!\s{2}). 匹配除空白字符外的每个字符,后跟另一个空白。

        2
  •  4
  •   Callum Rogers    15 年前

    你甚至需要使用regex吗?为什么不试试:

    string test = "the  cat  chased  the dog";
    bool containsDoubleSpaces = test.Contains("  ");
    
        3
  •  1
  •   chris    15 年前

    您的regexp如下: " +" (这是2个空格,后面加+号)

    它将匹配一行中的两个或更多空格。

        4
  •  0
  •   Amarghosh    15 年前

    ^.* .*$ 甚至 (只有两个空格)就行了。将空格替换为 \s 如果您希望连续容纳任意两个空格字符(制表符、新行等)