A regular expression that will contain a string with only one letter "Capital"

The string must be between 6 and 20 characters long. And it should contain 1 uppercase letter.

I can do this in code using 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;
}

But I really want this regular expression that will match my string if it contains only one capital. A string can begin with a common letter and must have only letters.

+3
source share
2 answers

This will correspond to a string containing lowercase letters, then one uppercase letter, then lowercase letters.

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

, , .

6 20, , lookaheads:

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

/(?=^\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
+4

Source: https://habr.com/ru/post/1740375/


All Articles