代码之家  ›  专栏  ›  技术社区  ›  A.R.

简单正则表达式问题

  •  3
  • A.R.  · 技术社区  · 15 年前

    “ThisIsMyName”应该匹配,但是

    5 回复  |  直到 15 年前
        1
  •  4
  •   eldarerathis sreeji    15 年前

    ^[a-zA-Z0-9]+$ 将匹配字符串中没有空格(或标点符号)的任何字母和数字。它还需要至少一个字母数字字符。它使用 character class 为了匹配。故障:

    ^       #Match the beginning of the string
    [       #Start of a character class
      a-z   #The range of lowercase letters
      A-Z   #The range of uppercase letters
      0-9   #The digits 0-9
    ]       #End of the character class
    +       #Repeat the previous one or more times
    $       #End of string
    

    此外,如果您想“捕获”匹配以便以后可以引用它,可以将regex括在parens中(a capture group

    ^([a-zA-Z0-9]+)$
    

    更进一步:由于您用C#标记了它,MSDN有一些如何在.NET中使用正则表达式的方法。可以找到它 here . 您还可以注意到,如果使用 RegexOptions.IgnoreCase

    ^([a-z0-9])+$

        2
  •  0
  •   Philippe Leybaert    15 年前

    \S+
    
        3
  •  0
  •   Abe Miessler    15 年前

    查看此链接可获得良好的基本Regex信息源: http://regexlib.com/CheatSheet.aspx

    他们还有一个我经常使用的方便的测试工具: http://regexlib.com/RETester.aspx

        4
  •  0
  •   eriksv88    15 年前

    我刚刚写了一篇关于regex的博客,也许你会发现它很有用:) http://blogs.appframe.com/erikv/2010-09-23-Regular-Expression

        5
  •  0
  •   Conner devopensource    13 年前

    尝试使用此regex查看它是否有效: (\w+)