[英]Regex to remove a non-alphanumeric character at the end of a string which also appears else where in the string
我不擅长Regex,需要帮助来创建表达式以删除pageUrl末尾的'/'字符: https ://qa.test.celtic.com/我尝试了一些表达式,但是每次删除所有表达式时'/'字符或无。 字符串可能会更改,但最后一个“ /”将始终存在。 使用了[a-zA-Z:/。] $,但它仅返回'/'字符。 我可以在字符串中找到一个作为子字符串的字符,然后使用代码替换该字符串,而宁愿找到一个正则表达式,该表达式只使用了字符串而没有尾随字符。
为什么不使用TrimEnd()
方法删除字符串末尾的特定字符?
string url = @"https://qa.test.celtic.com/";
string result = url.TrimEnd('/');
我同意@fubo ,并且会使用比正则表达式更合适的东西。
就是说,如果您真的想使用正则表达式,只需在字符串的末尾查找/
,在正则表达式中将其指定为$
:
Regex.Replace("https://qa.test.celtic.com/", "/$", "")
或者,如果您的问题确实是“如何在没有/的情况下匹配值”,那么您需要一个捕获组来查找除/
之外的所有内容:
Regex.Match("https://qa.test.celtic.com/","(?<host>.*)/$").Groups["host"].Value
字符串可能会更改,但最后一个“ /”将始终存在。
您可以这样尝试:
String res = url.Substring(0,url.Length -1);
你也可以把支票
if ( url!=null && url.EndsWith('/') )
{
String res = url.Substring(0,url.Length -1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.