简体   繁体   English

使用正则表达式(Regex)根据输入字符串的长度去除字符

[英]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. 我希望使用.NET Regex创建一个正则表达式,该正则表达式接受输入字符串并输出从前面删除了字符的字符串,但是删除的字符数取决于输入字符串的长度。

The input string can be either: 输入字符串可以是:

  1. ROC1230NNNNNNNN ROC1230NNNNNNNN
  2. ROC1230NNNNNNNNN ROC1230NNNNNNNNN
  3. ROCNNNNNNNN 罗克·NNNNNN
  4. ROCNNNNNNNNN 罗恩·NNNNNNNN

'N' is always numeric and '123' can be any number combination. “ N”始终是数字,“ 123”可以是任何数字组合。 The '0' in the first two cases is always '0' and 'ROC' is always 'ROC'. 前两种情况下的“ 0”始终为“ 0”,“ ROC”始终为“ ROC”。

In each case, I just want to return the 'N' part. 在每种情况下,我只想返回“ N”部分。 ie: 即:

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

Thanks! 谢谢!

我假设“ 123”部分始终为3位数字,请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM