2

This is my string, which always starts with a alphabet followed by 3 digits, then - (hyphen), followed by 3 digits, then alphabet, then 4 digits

I123-123S1234N1234

I have come up with a regular expression, which seems to be very long! How can I write a better expression for this?

[a-zA-Z][0-9][0-9][0-9][-][0-9][0-9][0-9][a-zA-Z].........goes on

Note

  • Shall support case insensitive
  • Hyphen, S and N always constant
kudlatiger
  • 3,028
  • 8
  • 48
  • 98

4 Answers4

3

First use the case-insensitive flag, and then when repeating tokens, use {<number of times to repeat>}` instead of repeating yourself:

(?i)[a-z][0-9]{3}-[0-9]{3}[a-z][0-9]{4}

If you want to match the trailing N1234 as well, then put the final [a-z][0-9]{4} in a group, and repeat it twice:

(?i)[a-z][0-9]{3}-[0-9]{3}(?:[a-z][0-9]{4}){2}

https://regex101.com/r/5ra1eU/1

If the S and N are constant, then don't use character sets to match them, just match the plain characters:

(?i)[a-z][0-9]{3}-[0-9]{3}S[0-9]{4}N[0-9]{4}

https://regex101.com/r/5ra1eU/2

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
3

Try this:

var testString = "I123-123S1234N1234";
var pattern = @"[A-Za-z]\d{3}-\d{3}[sS]\d{4}[nN]\d{4}";
var isMatch = Regex.Match(testString, pattern).Success;

Pattern used: [A-Za-z]\d{3}-\d{3}[sS]\d{4}[nN]\d{4}

Explanation:

[A-Za-z] - match any letter

\d{3} - match three digits

[sS] - match S or s literally

\d{4} - match four digits

[nN] - match N or n literally

\d{4} - match four digits

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • will mark as answer! But it shall support for small letters too! [a-z] – kudlatiger Apr 09 '19 at 05:51
  • There is a regex option that makes it case insensitive, thus making it more simple. – Cleptus Apr 09 '19 at 06:15
  • @bradbury9 Yes, you are right, but I prefer using flags only when needed badly – Michał Turczyn Apr 09 '19 at 06:17
  • Is there a validation that can be performed to make sure the alphabetical values are not repeated? [sS] var testString = "I123-123Sssss1234N1234"; in my validation if I have multiples of the character s repeating the regex still evaluates to true. I would want it to fail – MadMardigans Apr 08 '22 at 23:26
2

if you are sure to have capital letter and S and N constant:

[A-Z]\d{3}-\d{3}S\d{4}N\d{4}

[A-Z] - match any uppercase letter

\d{3} - match 3 digits

S - match S literally

\d{4} - match 4 digits

N - match N literally

\d{4} - match 4 digits

if you want upper and lower case:

[a-zA-Z]\d{3}-\d{3}[Ss]\d{4}[Nn]\d{4}

or

(?i)[a-z]\d{3}-\d{3}s\d{4}n\d{4}
Frenchy
  • 16,386
  • 3
  • 16
  • 39
0

This should work:

[a-zA-Z][0-9]{3}-[0-9]{4}[a-zA-Z][0-9]{4}
Nico Schreiner
  • 397
  • 5
  • 13