98

I need a regular expression that will match any three uppercase letters, so AAA or ABC or DKE. It can't match four or more though, like AAAA or ABCDEF or aBBB.

My solution: ^([A-Z][A-Z][A-Z])$

Questions:

  1. Is this correct?
  2. Is there another way, just for the sake of learning?
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Scocco
  • 7,036
  • 13
  • 51
  • 78
  • @owlstead, I did, and in fact I managed to get the right answer by myself apparently. I needed to cross check to make sure I was not wrong though, since I am new to regex. – Daniel Scocco Jan 29 '13 at 18:56
  • 1
    Voted question up, but I would read the part about repetition again, because you should also know about [limiting repetition](http://www.regular-expressions.info/repeat.html) – Maarten Bodewes Jan 29 '13 at 19:22

2 Answers2

153

What you have is correct, but this is more consice:

^[A-Z]{3}$
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • Gotcha. And these two should be the only two ways, right? Yep I am sure about the uppercase. – Daniel Scocco Jan 29 '13 at 18:48
  • @DanielS - With or without the capture group. Yes. – Joseph Silber Jan 29 '13 at 18:49
  • 7
    @owlstead - This is a very valid question. The OP is clearly learning, demonstrated what they're trying, and asked for guidance. The question is *very* well formed. Why would anybody close this? – Joseph Silber Jan 29 '13 at 18:55
  • 7
    @owlstead - [**No question is too trivial or too "newbie"**](http://meta.stackexchange.com/questions/9663/is-so-appropriate-for-newbish-question-about-a-specific-technology/9664#9664). – Joseph Silber Jan 29 '13 at 18:57
  • @owlstead, I also researched on SO and didn't find anything identical to my question, hence why I decided to post. – Daniel Scocco Jan 29 '13 at 18:57
  • OK, the link I found was not trivial enough, and I could not find one either. No worries, removed part about uppercase chars from the answer (because of the first sentence in the question) and voted both up... My apologies. – Maarten Bodewes Jan 29 '13 at 19:18
  • What's the use of ^ and $ in this case? – sunitprasad1 Jan 31 '17 at 20:52
  • @sunitprasad1 - `^` makes sure the pattern matches at the beginning of the string. `$` makes sure it matches at the the end of the string. Both of them together make sure that the pattern matches the entire string, ensuring that the string is no longer the pattern specified. – Joseph Silber Feb 01 '17 at 01:39
  • You can also use `^[A-Z]{3,5}$` to match between three to five uppercase letters :-) – coccoinomane May 11 '20 at 15:23
26

Your solution is correct, but there is some redundancy in your regex.
The similar result can also be obtained from the following regex:

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

The {3} indicates that the [A-Z] must appear exactly 3 times.

Ali Shah Ahmed
  • 3,263
  • 3
  • 24
  • 22
  • 1
    Thanks for that. It's the same answer as the previous person, however, so I'll pick his answer as the right one to be fair. – Daniel Scocco Jan 29 '13 at 18:59
  • 1
    Yeah, I too noticed that after posting my answer. Well I just wanted to help, so it doesn't really matter if it is not marked as correct answer. Still thanks for commenting :) – Ali Shah Ahmed Jan 29 '13 at 19:21