Skip to content

Instantly share code, notes, and snippets.

@cori
Created November 11, 2010 16:05
Show Gist options
  • Save cori/672685 to your computer and use it in GitHub Desktop.
Save cori/672685 to your computer and use it in GitHub Desktop.
C# String extension method implementing VB's Like() method (shamelessly cribbed from http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=25)
using System;
using System.Text.RegularExpressions;
namespace ETC.Helpers
{
public static class StringExtensions
{
/// <summary>
/// Indicates whether the regular expression specified in "pattern" could be found in the "text".
/// </summary>
/// <param name="text">The string the methdod is being called on</param>
/// <param name="pattern">The pattern to find in the "text" string
/// (supports the *, ? and # wildcard characters).
/// </param>
/// <param name="ignoreCase">true [default] to ignore case.</param>
/// <param name="useCharClasses">true to unescape "[", "[!", and "]" to allow using VB's character classes for like comparison.
/// False if any of those literal characters appears in the string pattern to match</param>
/// <returns>Returns true if the regular expression finds a match otherwise returns false.</returns>
/// <remarks>
/// ? Matches any single character (between A-Z and a-z)
/// # Matches any single digit. For example, 7# matches numbers that include 7 followed by another number, such as 71, but not 17.
/// * Matches any one or more characters. For example, new* matches any text that includes "new", such as newfile.txt.
///
/// Thanks to Andrew Baker for the original code at http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=25 and by proxy:
///
/// This functionality is based on the following article:
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxgrfwildcards.asp
///
/// Thanks to Stefan Schletterer for corrections.
/// </remarks>
public static bool Like( this string text, string pattern, bool ignoreCase = true, bool useCharClasses = true )
{
// Check input parameters
if ( pattern == null || text == null || pattern.Length == 0 || text.Length == 0 || pattern == "*.*" == true ) {
// Default return is true
return true;
}
// Escape all strings
System.Text.StringBuilder regPattern = new System.Text.StringBuilder( Regex.Escape( pattern ) );
// Replace the LIKE patterns with regular expression patterns
regPattern = regPattern.Replace( Regex.Escape( "*" ), ".*" );
regPattern = regPattern.Replace( Regex.Escape( "?" ), @"." );
regPattern = regPattern.Replace( Regex.Escape( "#" ), @"[0-9]" );
//if pattern to compare includes VB character class markers and is not using character classes then don't unescape them
if ( useCharClasses ) {
regPattern = regPattern.Replace( Regex.Escape( "[!" ), @"[!" );
regPattern = regPattern.Replace( Regex.Escape( "[" ), @"[" );
regPattern = regPattern.Replace( Regex.Escape( "]" ), @"]" );
}
// Add begin and end blocks (to match on the whole string only)
regPattern.Insert( 0, "^" );
regPattern.Append( "$" );
if ( ignoreCase == false ) {
return Regex.IsMatch( text, regPattern.ToString() );
} else {
return Regex.IsMatch( text, regPattern.ToString(), RegexOptions.IgnoreCase );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment