![](/img/trans.png)
[英]regex to exact match 11 digit phone number from string and remove hyphen(-) from match in c#
[英]Regex to remove digits & hyphen(-) from end of a string
如何在 C# 中使用正则表达式从字符串末尾删除数字和“-”。
string name= "100% program name - 200";
Regex rgx = new Regex(@"[0-9]+$");
name= rgx.Replace(name, "").Replace(" -","");
目前它返回“ 100% 程序名称”,但我需要使用单个正则表达式而不使用第二个替换Replace(" -","")
您可以在第二个替换的字符串前面加上一个空格,然后在正则表达式中添加一个空格并使用单个替换。
- [0-9]+$
.NET 正则表达式演示
string name= "100% program name - 200";
Regex rgx = new Regex(@" - [0-9]+$");
name= rgx.Replace(name, "");
Console.WriteLine(name);
结果
100% 程序名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.