0

I am new in Regex can you please help in writing for regex in C# to extract url from text below?

Example 1

x+=1;
top.location.href = "http://www.keenthemes.com/preview/index.php?theme=metronic";

Example 2

alert("are you sure");     
top.location.href   = 'http://www.keenthemes.com/preview/index.php?theme=metronic';
3
  • Are you looking to get www.keenthemes.com from the above URL in C# ? or you want to get the string which starts with http ? Commented May 2, 2013 at 12:43
  • May be HTML Agility Pack is what you are looking for Commented May 2, 2013 at 12:49
  • already using htmlagility pack but it work with dom object now Javascript text Commented May 2, 2013 at 12:52

2 Answers 2

1

If the URL always starts with http://, this one should do it:

["'](http.*)["']

The URL is stored in the second group (Groups[1].Value) of the Match object

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

Comments

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

This will work for any kind of url. For more info please look at http://regexlib.com/Search.aspx?k=URL&AspxAutoDetectCookieSupport=1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.