Regex with first cap char + digits

Viewed 35

I hit a wall with this one so looking for help.

My strings must be in the following format:

777_7.70B_A777777
777_7.7B_A777777
777_7.7_A777777

I got the first 2 groups working just fine but I just cannot get the last group to work. This is my regex:

^\\d{3}_\\d{1,2}[.]\\w{1,3}_\\[A-Z0-9]{7}$

Trying to get [A-Z0-9]{7} to work for A777777... at least. What would be awesome is to have a rule that allows only one cap char in the beginning followed by 6 digits... but that's a gravy.

Thank you in advance!

1 Answers

You can use

^\d{3}_\d{1,2}\.[^\W_]{1,3}_[A-Z0-9]{7}$
^\d{3}_\d{1,2}\.[^\W_]{1,3}_[A-Z]\d{6}$

See the regex demo #1 and this regex demo #2.

Details:

  • ^ - start of string
  • \d{3} - three digits
  • _ - a _ char
  • \d{1,2} - one or two digits
  • \. - a . char
  • [^\W_]{1,3} - one to three word chars other than _
  • _ - a _ char
  • [A-Z] - an uppercase ASCII letter
  • \d{6} - six digits
  • $ - end of string.

If you only need to allow ASCII chars, compile the regex with RegexOptions.ECMAScript.

Related