c#用正则表达式从字符串中查找并替换url

时间:2022-07-05 16:53:09

i want to replace url for example www.google.com or http://www.google.com with www.google.com i have a code for this

我想用www.google.com替换url,例如www.google.com或http://www.google.com,我有一个代码

str = Regex.Replace(str,
                @"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)",
                "<a target='_blank' href='$1'>$1</a>");

it is working with http://www.google.com but it is not working with www.google.com or subdomain.google.com which regex code matches with every url links. and when i wrote a long link it will write same of the url for example

它正在与http://www.google.com合作,但它无法与www.google.com或subdomain.google.com合作,正则表达式代码与每个网址链接匹配。当我写一个长链接时,它会写相同的网址

http://www.google.com/search/asdadad/sdsdsd/sadasdx-sadasd-weqeqwe-zxcxzc.com

. i want to write it as

。我想把它写成

<a href="http://www.google.com/search/asdadad/sdsdsd/sadasdx-sadasd-weqeqwe-zxcxzc.com">google.com/asdas... </a>

google.com/asdas ...

what is the best way to make this? i am new for regex

做这个的最好方法是什么?我是正则表达式的新手

1 个解决方案

#1


2  

This will also catch www.test.com:

这也将赶上www.test.com:

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

Just surround the part thats optional and append a questionmark. You can check it out here.

只需将可选部分包围,然后添加问号。你可以在这里查看。


The first match in this regex (matches are defined with "(" and ")") is the whole url. So you could use replacing like this:

此正则表达式中的第一个匹配(匹配用“(”和“)”定义)是整个URL。所以你可以像这样使用替换:

Regex rgxUrls = new Regex(pattern);
string result = rgxUrls.Replace(yourText, "<a href=\"$1\"> space for custom text </a>");
                                                      ↑ Inserts first match

c#用正则表达式从字符串中查找并替换url

Where I've used $1 you can also use $2 - $5. Check the image above thats showing which groups are capturing which part of the url.

在我使用1美元的地方你也可以使用2美元 - 5美元。检查上面的图像,显​​示哪些组正在捕获网址的哪个部分。

Full test can be found here.
Just click execute on the top.

完整的测试可以在这里找到。只需点击顶部的执行即可。

Output: c#用正则表达式从字符串中查找并替换url


According the comments, how group caption works:

根据评论,组标题如何工作:

Text: "this is your text to search"  
Pattern: "text to"

Match[0] will always match your whole match text to. Every groups above like Match[1] or Match[2] has to be defined with "(" and ")".

匹配[0]将始终匹配您的整个匹配文本。像Match [1]或Match [2]这样的每个组都必须用“(”和“)”来定义。

Text: "this is your text to search"  
Pattern: "text (to)"  
Match[0]: "text to"  
Match[1]: "to"  


Pattern: "text (t(o))"  
Match[0]: "text to"  
Match[1]: "to"  
Match[2]: "o"  

The caption with "()" works from the outside to the inside.

带有“()”的标题从外到内。

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

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

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

$4 (.com)
(((http|ftp|https):\/\/)?[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)
                                 ↑----------↑   

$5 (/appendedSubdirectory/anotherOne)
(((http|ftp|https):\/\/)?[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)
                                              ↑--------------------------------------------------↑   

I cant explain everything about regex in here. This question looks solved for me. If you've got deeper questions according regex start a new one and show some effort you've done before.

我不能在这里解释有关正则表达式的所有内容。这个问题看起来很适合我。如果你有更深层次的问题,根据正则表达式开始一个新的,并展示你以前做过的一些努力。

#1


2  

This will also catch www.test.com:

这也将赶上www.test.com:

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

Just surround the part thats optional and append a questionmark. You can check it out here.

只需将可选部分包围,然后添加问号。你可以在这里查看。


The first match in this regex (matches are defined with "(" and ")") is the whole url. So you could use replacing like this:

此正则表达式中的第一个匹配(匹配用“(”和“)”定义)是整个URL。所以你可以像这样使用替换:

Regex rgxUrls = new Regex(pattern);
string result = rgxUrls.Replace(yourText, "<a href=\"$1\"> space for custom text </a>");
                                                      ↑ Inserts first match

c#用正则表达式从字符串中查找并替换url

Where I've used $1 you can also use $2 - $5. Check the image above thats showing which groups are capturing which part of the url.

在我使用1美元的地方你也可以使用2美元 - 5美元。检查上面的图像,显​​示哪些组正在捕获网址的哪个部分。

Full test can be found here.
Just click execute on the top.

完整的测试可以在这里找到。只需点击顶部的执行即可。

Output: c#用正则表达式从字符串中查找并替换url


According the comments, how group caption works:

根据评论,组标题如何工作:

Text: "this is your text to search"  
Pattern: "text to"

Match[0] will always match your whole match text to. Every groups above like Match[1] or Match[2] has to be defined with "(" and ")".

匹配[0]将始终匹配您的整个匹配文本。像Match [1]或Match [2]这样的每个组都必须用“(”和“)”来定义。

Text: "this is your text to search"  
Pattern: "text (to)"  
Match[0]: "text to"  
Match[1]: "to"  


Pattern: "text (t(o))"  
Match[0]: "text to"  
Match[1]: "to"  
Match[2]: "o"  

The caption with "()" works from the outside to the inside.

带有“()”的标题从外到内。

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

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

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

$4 (.com)
(((http|ftp|https):\/\/)?[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)
                                 ↑----------↑   

$5 (/appendedSubdirectory/anotherOne)
(((http|ftp|https):\/\/)?[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?)
                                              ↑--------------------------------------------------↑   

I cant explain everything about regex in here. This question looks solved for me. If you've got deeper questions according regex start a new one and show some effort you've done before.

我不能在这里解释有关正则表达式的所有内容。这个问题看起来很适合我。如果你有更深层次的问题,根据正则表达式开始一个新的,并展示你以前做过的一些努力。