tags:

views:

409

answers:

6

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.

+1  A: 

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

Konamiman
Does not work if the URL contains directories. Contains typo ".ps" -> ".pls".
Taneli Waltari
Not exactly, I reformulated the question, see what I had edited.
diadiora
+1  A: 
/^http:\/\/[-_a-zA-Z0-9.]+(:\d+)?\/listen\.pls$/
Asaph
does not match the first one
Taneli Waltari
@twaltari: Ah, thanks. I fixed it.
Asaph
Not exactly what I wanted, sorry my mistake, I reformulated the question, see what I had edited.
diadiora
+1  A: 

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

Taneli Waltari
+1  A: 

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.

John K
+6  A: 

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?

    HTTP://83.222.4.42:8880/listen.pls

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"
    )
{
    // ...
}
Jay Bazuzi
A: 

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
    }

}
Jay Zeng