【问题标题】:ASP.NET remove URLs from string (regex)ASP.NET 从字符串中删除 URL(正则表达式)
【发布时间】:2017-08-24 06:47:22
【问题描述】:

已在此处检查:C# Remove URL from Stringhere 和此处Remove URLs from text string

我尝试了上一篇文章中的正则表达式:
Regex.Replace(txt, "!\b(((ht|f)tp(s?))\://)?(www.|[a-z].)[a-z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-z0-9\.\,\;\?\\'\\\\\+&%\$#\=~_\-]+))*\b!i", "")

我还尝试了引用的正则表达式:
^(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$

但两者都不会删除网址。

我想使用 VB 从字符串中删除所有 URL。这些 URL 不在 a 标记中。

示例字符串:
This is https://www.example.com/an-example-url and http://www.example.com/and-some-friendlyurl?utm_campaign=test&id=4.<br/>.Themes.

想要的结果:
This is and .<br/>.Themes.

【问题讨论】:

  • (http|https):\/\/[\w\-_]+(\.[\w\-_]+)+[\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#] 这个正则表达式有效,检查here...您可以将它与replace 结合使用并删除匹配项...

标签: asp.net regex string vb.net url


【解决方案1】:

试试这个Regex...

 (http|https):\/\/[\w\-_]+(\.[\w\-_]+)+[\w\-\.,@?^=%&:\/~‌​\+#]*[\w\-\@?^=%&amp‌​;\/~\+#]

您可以将其与Replace 结合使用并删除匹配项。如果您不想删除匹配项,您还可以对匹配项的每个部分进行任何操作。

这个已经测试过了,可以试试here

【讨论】:

    猜你喜欢
    • 2021-01-17
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    • 2021-09-07
    • 2012-02-06
    • 2012-03-21
    • 2018-11-20
    相关资源
    最近更新 更多