views:

233

answers:

2

C# Need to locate web addresses using REGEX is that possible?

Basically I need to parse a string prior to loading it into a WebBrowser

myString = "this is an example string http://www.google.com , and I need to make the link clickable";

webBrow.DocumentText = myString;

Basically what I want to happen is a replace of the web address so that it looks like a hyperlink, and do this with any address pulled in to the string. I would need to replace the web address so that web address would read like

<a href='web address'>web address</a>

This would allow me to have the links clickable.. Any Ideas?

A: 

It's possible depending on how strict or permissive you want your parsing to be.

As a first cut, you can try @"\bhttp://\S+" which will match any string starting with "http://" at a word boundary (non-word character, such as whitespace or punctuation).

To search using a regex and replace all occurrences with your custom text, you could use the Regex.Replace method.

You may want to read up on Regular Expression Language Elements to learn more.

bobbymcr
+1  A: 
new Regex(@"https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?").Match(myString)
Jan Jongboom