At work, we had to do wildcard searches using regular expressions to match allowed domain names. For instance, this expression “*.madskristensen.dk” should match “www.madskristensen.dk”.

So, my boss Jimmi wrote a method that he allowed me to post here. Here are some examples on how to use it:

IsDomainValid( "www.madskristensen.dk" , "*.madskristensen" ); is true

IsDomainValid( "www.madskristensen.dk" , "www.madskristensen*" ); is true

IsDomainValid( "www.madskristensen.dk" , "madskristensen*" ); is false

IsDomainValid( "www.madskristensen.dk" , "*mads*.dk" ); is true
IsDomainValid( "www.madskristensen.dk" , "www.*.dk" ); is true

Here is the method.

/// <summary>

/// Checks if a wildcard string matches a domain

/// </summary>

public static bool IsDomainValid( string domain, string domainToCheck)

{

  if (domainToCheck.Contains( "*" ))

  {

    string checkDomain = domainToCheck;

 

    if (checkDomain.StartsWith( "*." ))

      checkDomain = "*" + checkDomain.Substring(2, checkDomain.Length - 2);

 

    return DoesWildcardMatch(domain, checkDomain);

  }

  else

  {

    return domainToCheck.Equals(domain, StringComparison .OrdinalIgnoreCase);

  }

}

 

/// <summary>

/// Performs a wildcard (*) search on any string.

/// </summary>

public static bool DoesWildcardMatch( string originalString, string searchString)

{

  if (!searchString.StartsWith( "*" ))

  {

    int stop = searchString.IndexOf( '*' );

    if (!originalString.StartsWith(searchString.Substring(0, stop)))

      return false ;

  }

 

  if (!searchString.EndsWith( "*" ))

  {

    int start = searchString.LastIndexOf( '*' ) + 1;

    if (!originalString.EndsWith(searchString.Substring(start, searchString.Length - start)))

      return false ;

  }

 

  Regex regex = new Regex (searchString.Replace( @"." , @"\." ).Replace( @"*" , @".*" ));

  return regex.IsMatch(originalString);

}

You can also use the DoesWildcardMatch method alone.

DoesWildcardMatch( "hello world" , "*world" ); is true

In C# 2.0 Microsoft introduces some new methods on the String class, one of them being Contains. The Contains method checks if a string in contained within another string in a case-sensitive way. It is very easy to use and a good addition to the String class. However, often you don’t want a case-sensitive search or you want to know how many times a particular string is contained within another. 

Because the String class doesn’t provide us with such methods, we have to create our own. Here are three methods – two overloaded – that does just that.

using System.Text.RegularExpressions;

 

/// <summary>

/// Checks if a string is contained within another.

/// The search is case-insensitive.

/// </summary>

/// <param name="text">The text to search.</param>

/// <param name="stringToFind">The string to look for.</param>

public static bool Contains(string text, string stringToFind)

{

  return NumberOfOccurrences(text, stringToFind) > 0;

}

 

/// <summary>

/// Searches the text for occurrences of a specific string.

/// The search is case-insensitive.

/// </summary>

/// <param name="text">The text to search.</param>

/// <param name="stringToFind">The string to look for.</param>

public static int NumberOfOccurrences(string text, string stringToFind)

{

  return NumberOfOccurrences(text, stringToFind, RegexOptions.IgnoreCase);

}

 

/// <summary>

/// Searches the text for occurrences of a specific string.

/// </summary>

/// <param name="text">The text to search.</param>

/// <param name="stringToFind">The string to look for.</param>

/// <param name="options">Specify the regex option.</param>

public static int NumberOfOccurrences(string text, string stringToFind, RegexOptions options)

{

  if (text == null || stringToFind == null)

  {

    return 0;

  }

 

  Regex reg = new Regex(stringToFind, options);

  return reg.Matches(text).Count;
}

Example of use

if (Contains("Hello world", "hello"))

{

  DoSomething();

}

 

if (NumberOfOccurrences("Hello world", "o") > 1)

{

  DoSomething();

}

 

if (NumberOfOccurrences("Hello world", "o", RegexOptions.None) = 2)

{

  DoSomething();
}

Let’s hope they add similar functionality to the .NET Framework in the future. To read more on regular expression, read this guide on MSDN.