6

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

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/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>

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

1 Answer 1

8

This will also catch 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:

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

enter image description here

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.

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

Output: enter image description here


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 ")".

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your answer. but i have to learn to edit match value. $1 is getting full adress. i want to edit $1 for example matched_value.Substring(0,5) what is the right way ? second question : the url www.google.com found and replaced by your code but the link is without http. so when i clicked link it will show mydomain.com/www.google.com how can i add link with http
You want to replace www.google.de with http://www.google.de? I will edit my question and explain group caption.
thanks for your helpful answer. I will update my code with your suggestions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.