Regex to match on capital letter, digit or capital, lowercase, and digit

I think what you want is "[A-Z][a-z]?\d*"

That is, a capital letter, followed by an optional small letter, followed by an optional string of digits.

If you want to match 0, 1, or 2 lower-case letters, then you can write:

"[A-Z][a-z]{0,2}\d*"

Note, however, that both of these regular expressions assume that the input data is valid. Given bad data, it will skip over bad data. For example, if the input string is "H2ClxxzSO4", you're going to get:

  1. H2
  2. Clx
  3. S
  4. O4

If you want to detect bad data, you'll need to check the Index property of the returned Match object to ensure that it is equal to the beginning index.

Tags:

C#

String

Regex