-3

What does the regex [A-Z]{0,2,3}\\d+ mean?

Does it match strings like

AAA1234 0000123 AA12345

Thanks

Uska
  • 65
  • 1
  • 6
  • 14

2 Answers2

4
[A-Z]{0,2,3}\\d+

That looks like a typo.

{} braces shouldn't have more than two numbers in it.

[A-Z]{0,3} mean any alphabet between A and Z (capital) can occur 0 to 3 times.

And \d+ means any digit (0-9) can occur 1 or more times.

That is the reason [A-Z]{0,3}\d+ matches AAA1234, 0000123 and AA12345

Pranay Majmundar
  • 803
  • 7
  • 15
  • OP's syntax is valid and compiles fine. The OP's regex matches the input string `A{0,2,3}1`. It is true that it's likely a typo, however. – ggorlen Jun 11 '18 at 17:57
  • Thanks, that makes sense yes the {} was wrong it should have been [A-Z]{2,3}\\d+, I had to add another exception for only characters so my new string looks like: [A-Z]{2,3}\\d+|\\d{7} which has 2 or 3 characters in caps, with lagging digits or digits which are limited to 7 – Uska Jun 11 '18 at 18:00
  • right, ggorlen... Adjusted my answer – Pranay Majmundar Jun 11 '18 at 18:01
  • The backslash before the `d` is escaped, so it doesn't mean a digit... right? – Callum Watkins Jun 11 '18 at 18:05
  • In `[A-Z]{2,3}\d+|\d{7}`, with regards to `lagging digits or digits which are limited to 7 `; `\d+` is a subset of `\d{7}`; i.e.: anything matched by `\d{7}` will also be matched by `\d+`. This makes `\d{7}` redundant. – Pranay Majmundar Jun 11 '18 at 19:19
0

Your regex ([A-Z]{2,3}\\d+) will match

  • Two or three letters A-Z,
  • followed by a backslash "\",
  • and finally one or more instances of the letter "d".
Callum Watkins
  • 2,844
  • 4
  • 29
  • 49