C# Regex.Replace match by the same amount of characters

后端 未结 2 687
太阳男子
太阳男子 2021-01-22 10:01

I like to know how to replace a regex-match of unknown amount of equal-signs, thats not less than 2... to the same amount of underscores

So far I got this:



        
2条回答
  •  长发绾君心
    2021-01-22 10:11

    You can use Regex.Replace(String, MatchEvaluator) instead and analyze math:

    string result = new Regex("(={2,})")
        .Replace(text, match => new string('_', match.ToString().Length)); 
    

提交回复
热议问题