user1112324
user1112324

Reputation: 643

Matching for subdomains and other urls containing wildcards

Im trying to do some generic type url matching based on a bank of predefined urls and actual urls I get from another table.

The bank of urls contain generic urls that I need to use in order to check if an incoming url matches or not.

So the bank of urls may look like this:

1. https://*.sharepoint.com
2. https://trello.com/b/*
3. https://*.google.*

Then incoming url's may look like the following (and ill give an yes/no if they should match)

https://bob.sharepoint.com yes

https://ilovesharepoint.com no

https://trello.com no

https://trello.com/b/3782932 yes

https://www.google.com yes

You get the idea. Aside for coding for each of those cases, Im struggling to find a generic way to parse these incoming urls to see if they match ANY one of them.

At the moment I code for each one, but thats a nightmare.

Upvotes: 0

Views: 1860

Answers (1)

vasek
vasek

Reputation: 2849

This might give you a good starting point:

string[] patterns = new[] 
{ 
    "https://*.sharepoint.com", 
    "https://trello.com/b/*", 
    "https://*.google.*" 
};

public bool IsMatch(string input)
{
    foreach (var p in patterns)
    {
        if (Regex.Match(input, Regex.Escape(p).Replace("\\*", "[a-zA-Z0-9]+")).Success)
            return true;
    }

    return false;
}

Note the mask [a-zA-Z0-9]+ is very simple and you will probably want to use a better one, depending on what you need to achieve.

Upvotes: 3

Related Questions