0

I am trying to pull a URL out of a string and use it later to create a Hyperlink. I would like to be able to do the following: - determine if the input string contains a URL - remove the URL from the input string - store the extracted URL in a variable for later use

Can anyone help me with this?

3
  • 1
    It should be noted that without a few examples, this task is near impossible. Commented Mar 20, 2013 at 18:34
  • I use this tool a LOT in my own code. I am decent with regular expressions, but being able to test them quickly and efficiently, as well as help with building them, is truly priceless. If I was somewhat less broke I would gladly donate to this developer. ultrapico.com/Expresso.htm Commented Mar 20, 2013 at 18:39
  • 1
    madskristensen.net/post/Resolve-and-shorten-URLs-in-Csharp.aspx nice starting point for you Commented Mar 20, 2013 at 18:40

2 Answers 2

3

Here is a great solution for recognizing URLs in popular formats such as:

The regular expression used is:

/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/

However, I would recommend you go to http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without-the to see the working example.

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

Comments

0

Replace input with your input

        string input = string.Empty;
        var matches = Regex.Matches(input,
                      @"/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/");
        List<string> urlList = (matches.Cast<object>().Select(match => match.ToString())).ToList();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.