Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 一个正则表达式,允许字符串只有一个大写字母_C#_Regex_String - Fatal编程技术网

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

C# 一个正则表达式,允许字符串只有一个大写字母,c#,regex,string,C#,Regex,String,字符串长度应为6-20个字符。 它应该包含1个大写字母 我可以使用C#在代码中实现这一点: 但我真正想要的是一个正则表达式,如果它只包含一个大写字母,它将匹配我的字符串。 字符串可以以普通字母开头,并且只能包含字母。此字符串将匹配包含小写字母的字符串,然后是单个大写字母,然后是更多小写字母 ^[a-z]*[A-Z][a-z]*$ 您可以根据需要调整第一部分和最后一部分以包含其他字符,具体取决于所需的字符域 对于长度为6到20的所有字符串(仅包含字母,最多包含一个大写字母),可以使用lookah

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

我可以使用C#在代码中实现这一点:

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

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

^[a-z]*[A-Z][a-z]*$
您可以根据需要调整第一部分和最后一部分以包含其他字符,具体取决于所需的字符域

对于长度为6到20的所有字符串(仅包含字母,最多包含一个大写字母),可以使用lookaheads:

(?=^[a-zA-Z]{6,20}$)^[a-z]*[A-Z][a-z]*$
正则表达式 测验 正则表达式解释
添加一个积极的前瞻性以强制执行6-20字符限制,这实际上将接受任何只有一个大写字母的字符串。这包括没有小写字符的字符串。起初您没有前瞻性。。。现在它就在那里,但我没有看到“x分钟前编辑的”。我疯了吗?@torak-不,如果Regex选项没有设置为不区分大小写,它会工作的。你可以缩短它:^(?=.{6,20}$)[a-z]*[a-z][a-z]*$你用了什么工具来解释?
(?=^[a-zA-Z]{6,20}$)^[a-z]*[A-Z][a-z]*$
/(?=^\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