一个正则表达式,允许只有一个大写字母的字符串

时间:2010-04-08 19:55:23

标签: c# regex string

字符串的长度应为6到20个字符。 它应该包含1个大写字母。

我可以在使用C#的代码中执行此操作:

string  st = "SomeString"

Regex rg = new Regex("[A-Z]");
MatchCollection mc = rg.Matches(st);

Console.WriteLine("Total Capital Letters: " + mc.Count);

if (mc.Count > 1)
{
  return false;
}

但我真正想要的是一个正则表达式,它匹配我的字符串,如果它只包含一个大写字母。 字符串可以以普通字母开头,并且只能包含字母。

2 个答案:

答案 0 :(得分:7)

这个字符串将匹配一个包含小写字母,然后是单个大写字母,然后是更多小写字母的字符串。

^[a-z]*[A-Z][a-z]*$

您可以根据需要调整第一个和最后一个部分以包含其他字符,具体取决于您所需的字符域。

对于长度为6到20且仅包含字母且最多只有一个大写字母的所有字符串,您可以使用前瞻:

(?=^[a-zA-Z]{6,20}$)^[a-z]*[A-Z][a-z]*$

答案 1 :(得分:4)

正则表达式

/(?=^\w{6,20}$)(?=\w*[A-Z])^\w+$/

测试

a                       # fail
aX                      # fail
ab                      # fail
aXb                     # fail
abc                     # fail
abXc                    # fail
abcd                    # fail
abcXd                   # fail
abcde                   # fail
abcdXe                  # pass
abcdef                  # fail
abcdeXf                 # pass
abcdefg                 # fail
abXcdefg                # pass
abcdefgh                # fail
abcXdefgh               # pass
abcdefghi               # fail
abcdXefghi              # pass
abcdefghij              # fail
abcdeXfghij             # pass
abcdefghijk             # fail
abcdefXghijk            # pass
abcdefghijkl            # fail
abcdefgXhijkl           # pass
abcdefghijklm           # fail
abcdefghXijklm          # pass
abcdefghijklmn          # fail
abcXdefghijklmn         # pass
abcdefghijklmno         # fail
abcdXefghijklmno        # pass
abcdefghijklmnop        # fail
abcdeXfghijklmnop       # pass
abcdefghijklmnopq       # fail
abcdefXghijklmnopq      # pass
abcdefghijklmnopqr      # fail
abcdefgXhijklmnopqr     # pass
abcdefghijklmnopqrs     # fail
abcdefghXijklmnopqrs    # pass
abcdefghijklmnopqrst    # fail
abcdefghiXjklmnopqrst   # fail
abcdefghijklmnopqrstu   # fail
abcdefghijXklmnopqrstu  # fail

正则表达式解释

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    ^                        the beginning of the string
--------------------------------------------------------------------------------
    \w{6,20}                 word characters (a-z, A-Z, 0-9, _)
                             (between 6 and 20 times (matching the
                             most amount possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    \w*                      word characters (a-z, A-Z, 0-9, _) (0 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string