Question: I need a regex statement that will check for three capital letters in a row.

For example, it should match: ABC, aABC, abcABC

But it should not match: AaBbBc, ABCDE

At the moment this is my statement:

```
'[^A-Z]*[A-Z]{3}[^A-Z]*'

```

But this matches ABCDE. What am I doing wrong?

Answer: 
In your regular expression, the `[^A-Z]*` at the beginning and end is saying *"Look for any number of non-capital letters, including 0."* And so, `ABCDE` will satisfy your regular expression. For example, `A` can be seen as "0 non-capital letters" followed by `BCD` followed by `E`, which is also "0 non-capital letters."

I think what you want to do instead is craft a regular expression that looks for:

1. Either "a non-capital letter" **or** the start of my string.
2. Followed by "exactly 3 capital letters."
3. Followed by "a non-capital letter" **or** the end of my string.

It doesn't matter *how many* non-capital letters precede or follow your 3 capital letters, as long as there is at least 1. So, you just need to look for 1.

Try this:

```none
(^|[^A-Z])[A-Z]{3}([^A-Z]|$)

```

Note that the first `^` means start of string, which is different from the meaning of `^` inside the brackets. The `$` means end of string.

Tested in ruby, here is what we have:

```none
regexp = /(^|[^A-Z])[A-Z]{3}([^A-Z]|$)/
'ABC'.match(regexp)    # returns a match
'aABC'.match(regexp)   # returns a match
'abcABC'.match(regexp)  # returns a match
'AaBbBc'.match(regexp) # returns nil
'ABCDE'.match(regexp)  # returns nil

```
Your regex has all explanation that what you are doing wrong

```
'[^A-Z]*[A-Z]{3}[^A-Z]*'

```

If ^ is used inside character set i.e [] which means ignore, so your regex would ignore if it starts A-Z (capital letters) either one or more at the starting. But as per your example, I think you don't want that

[A-Z]{3} means it will exactly match three capital letters in a row.

[^A-Z]\* means the same what I explained for the first one.

If you write '[A-Z]{3}' only, it would match exactly first three consecutive capital letters at anywhere in the string.

It would match ABCde abCDE aBCDe ABCDE
but it would not match abcDE ABcDE AaBcCc

Just try it.

**Example in Perl**

```
#!/usr/bin/perl
use strict;
use warnings;

my @arr = qw(AaBsCc abCDE ABCDE AbcDE abCDE ABC aABC abcABC);

foreach my $string(@arr){
  if($string =~ m/[A-Z]{3}/){
    print "Matched $string\n";
  }
  else {
    print "Didn't match $string \n";
  }
}

```

**Output:**

```none
Didn't match AaBsCc
Matched abCDE
Matched ABCDE
Didn't match AbcDE
Matched abCDE
Matched ABC
Matched aABC
Matched abcABC

```