1

I am trying to find out a URL which belongs to :

http://domain.blob.core.windows.net/ 
https://domain.blob.core.windows.net/

http://domain.blob.core.windows.net
https://domain.blob.core.windows.net

The regex which I wrote

Regex reg2= new Regex(@"^http(s?)://[0-9a-zA-Z](.blob.core.windows.net)(/?)$");
string url = "http://abc.blob.core.windows.net";
        if(reg2.IsMatch(url))
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("no");
        }

I am not able to find the match. It's not working :( I am pretty weak in regex. So can anyone help me to find out my mistake? I am using c#. It always prints no.

UPDATE : FINAL Answer which worked for me:

Regex reg2 = new Regex(@"^http(s?)\:\/\/[0-9a-zA-Z]*(\.blob\.core\.windows\.net)
(/?)$");

Just in case anybody needs something like this :)

3
  • 1
    Your .'s are not escaped... Commented Dec 10, 2012 at 9:13
  • 1
    Try pasting your regex string into an expression analyser. Expresso is a decent one and was free last time I used it. Commented Dec 10, 2012 at 9:16
  • thanks leppie , thanks phil Commented Dec 10, 2012 at 9:21

1 Answer 1

4

You were close with "^http(s?)://[0-9a-zA-Z](.blob.core.windows.net)(/?)$" however [0-9a-zA-Z] should be [0-9a-zA-Z]+ as you want to be multiple characters before .blob.core.windows.net not just a single character.

Note: you don't need brackets here if you not capturing parts of the match, the optional operator is applied to the previous character only so ^https?$ matches 'http' or 'https' and not '' as only the s is optional, Also escape all . characters as in regex the . characters matches any single character so to match a literal . you want \..

"^https?://[0-9a-zA-Z]+\.blob\.core\.windows\.net/?$"
Sign up to request clarification or add additional context in comments.

5 Comments

thanks a lot @sudo_O it helped me :) worked!! :) I can't accept answer for 5 mins.. i will asap :) Just one thing I want to mention Regex reg2 = new Regex(@"^http(s?)\:\/\/[0-9a-zA-Z]+(.blob.core.windows.net)(/?)$"); this is what I used. Thanks Phil and sudo_O both :)
Hell yea @leppie what do I do then :(
@leppie I added info on escaping .
@Mandy: Escape the . with a \ EG blob\.core\.
@leppie Regex reg2 = new Regex(@"^http(s?)\:\/\/[0-9a-zA-Z]*(\.blob\.core\.windows\.net)(/?)$"); Worked

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.