1

How to validate by a single regular expression the urls:

http://83.222.4.42:8880/listen.pls
http://www.my_site.com/listen.pls
http://www.my.site.com/listen.pls

to be true?

I see that I formulated the question not exactly :(, sorry my mistake. The idea is that I want to validate with the help of regexp valid urls, let it be an external ip address or the domain name. This is the idea, other valid urls can be considered:

http://93.122.34.342/
http://193.122.34.342/abc/1.html
http://www.my_site.com/listen2.pls
http://www.my.site.com/listen.php

and so on.

5
  • 2
    There is no such thing as a "C# regexp"; a regular expression is a regular expression, right? And to get the answer you want/need, it might be a good idea to specify your question more completely. Commented Dec 15, 2009 at 8:41
  • We need to know more about what patterns you want to match. 3 examples isn't enough. Describe it. Commented Dec 15, 2009 at 8:48
  • 5
    there are many slightly different flavours of regexp so specifying c# is a good idea Commented Dec 15, 2009 at 8:55
  • 2
    @jk: the pedant in me says to specifiy ".NET Regex". Commented Dec 15, 2009 at 17:46
  • @diadiora you edited to provide more examples, but I still don't think you've povided enough information. What are the patterns you need to match / reject? Commented Dec 17, 2009 at 18:39

6 Answers 6

7

The road to hell is paved with string parsing.

URL parsing in particular is the source of many, many exploited security issues. Don't do it.

For example, do you want this to match?

    

Note the uppercase scheme section. Remember that some parts of a URL are case sensitive, and some are not. Then there's encoding rules. Etc.

Start by using System.Uri to parse the URLs you provide:

var uri = new Uri("http://83.222.4.42:8880/listen.pls");

Then you can write things like:

if (uri.Scheme == "http" &&
    uri.Host == "83.222.4.42" &&
    uri.AbsolutePath == "/listen.pls"
    )
{
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

^http://.+/listen\.pls$

1 Comment

Could you add more detail to your answer? While short answers may help by pointing in the right direction, it would be helpful to have further explanation.
1

If there are strictly only 3 of them don't bother with a regular expression because there is not necessarily a good pattern match when everything is already strictly known - in fact you might accidentally match more than these three urls - which becomes a problem if the urls are intended for security purposes or something equally important. Instead, test the three cases directly - maybe put them in a configuration file.

In the future if you want to add more URLs to the list you'll likely end up with an overly complicated regular expression that's increasingly hard to maintain and takes the place of a simpler check against a small list.

You won't necessarily get speed gains by running Regex to find these three strings - in fact it might be quite expensive.

Note: If you wantUri regular expressions also try websites hosting libraries like Regex Library - there are many to pick and choose from if your needs change.

Comments

1
/^http:\/\/[-_a-zA-Z0-9.]+(:\d+)?\/listen\.pls$/

1 Comment

Not exactly what I wanted, sorry my mistake, I reformulated the question, see what I had edited.
1

Do you mean any URL ending with /listen.pls? In that case try this:

^http://[^/]+/listen\.pls$

or if the protocol identifier must be optional:

^[http://]?[^/]+/listen\.pls$

Anyway take a look here, maybe it is useful for you: Url and Email validation using Regex

2 Comments

Does not work if the URL contains directories. Contains typo ".ps" -> ".pls".
Not exactly, I reformulated the question, see what I had edited.
0

A modified version base upon Jay Bazuzi's solution above since I can't post code in comment, it checks a blacklisted extensions (I do this only for demonstration purpose, you should strongly consider to build a whitelist rather than a blacklist) :

string myurl = "http://www.my_site.com/listen.pls";
Uri myUri = new Uri(myurl);
string[] invalidExtensions = {
    ".pls",
    ".abc"
};

foreach(string invalidExtension in invalidExtensions) {
    if (invalidExtension.ToLower().Equals(System.IO.Path.GetExtension(myUri.AbsolutePath))) {
        //Logic here
    }

}

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.