C# Remove a letter that trails a number from multiple instances in a string whether with Regex by another method

后端 未结 1 1021
小鲜肉
小鲜肉 2021-01-25 09:08

I have a log giving me messages that look like this

Error Message: \" Expected: 660m But was: 600m\" Error Message: \" Expected: 358m But was: 325m\" Error M

1条回答
  •  深忆病人
    2021-01-25 09:45

    You may use a capturing group with a replacement backreference in the replacement pattern:

    example = Regex.Replace(example, @"([0-9])[a-zA-Z]\b", "$1");
    

    Note the \b word boundary after the letter pattern, it is used to make sure the letter is at the end of the word.

    See the regex demo. Result:

    Pattern details

    • ([0-9]) - Capturing group 1 (later referred to with $1) matching any one digit
    • [a-zA-Z] - an ASCII letter
    • \b - a trailing word boundary

    0 讨论(0)
提交回复
热议问题