简体   繁体   English

正则表达式从字符串C#中删除URL

[英]Regex to remove urls from string c#

I have the following code and the regular expression I am currently using does not appear to be catching any url I enter in standard format (www.google.com) as when it is displayed in a listbox, the URL is still there. 我有以下代码,并且当前正在使用的正则表达式似乎无法捕获以标准格式(www.google.com)输入的任何URL,因为当它显示在列表框中时,URL仍然存在。 Does anyone know where I'm going wrong? 有谁知道我要去哪里错了?

e1.MessageBody = txtMessage.Text;
                Regex.Replace(e1.MessageBody, @"/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/", ""string.Empty);
var msg = "ASD www.google.com EFIG";

msg = Regex.Replace(msg, @"((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)", string.Empty);

C# doesn't use the regex identifiers (the leading/trailing / ) and you had extra quotes " by the string.empty parameter. C#不使用正则表达式的标识符(前/后/ ),你有额外的引号"通过string.empty参数。

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

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