Search code examples
c#.netregexstrip

Using regular expression (Regex) to strip characters based on length of input string


I wish to use .NET Regex to create a regular expression that takes an input string and outputs a string with characters removed from the front, but the number of characters removed depends on the length of the input string.

The input string can be either:

  1. ROC1230NNNNNNNN
  2. ROC1230NNNNNNNNN
  3. ROCNNNNNNNN
  4. ROCNNNNNNNNN

'N' is always numeric and '123' can be any number combination. The '0' in the first two cases is always '0' and 'ROC' is always 'ROC'.

In each case, I just want to return the 'N' part. i.e.:

  1. NNNNNNNN
  2. NNNNNNNNN
  3. NNNNNNNN
  4. NNNNNNNNN

Thanks!


Solution

  • I presume that "123" part is always 3 digits, try this:

    string value = Regex.Match(input, @"^ROC(\d{3}0)?(\d+)$").Groups[2].Value;