C# Source Code: Using RegEx to create a "Like" string comparison
[
Home
|
Contents
|
Search
|
Reply
| Previous |
Next
]
C# Source Code
Using RegEx to create a "Like" string comparison
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, June 15, 2004
Hits:
2560
Category:
General/Framework
Article:
Below is a useful static function which uses the .NET regular expression object (RegEx) to give the same functionality as the VB Like string comparison: using System; using System.Text.RegularExpressions; using System.Diagnostics; namespace VBusers.General.Functions { public class Test { [STAThread] static void Main(string[] args) { //Test calls //Returns True (* matches any pattern) Debug.WriteLine(VBFunctions.Like(@"workbook.xls",@"*.xls",true)); //Returns True (* matches any pattern) Debug.WriteLine(VBFunctions.Like(@"workbook.xls",@"workbook*",true)); //Returns True (* matches any pattern) Debug.WriteLine(VBFunctions.Like(@"workbook.xls",@"workbook.*",true)); //Returns True (* matches any pattern) Debug.WriteLine(VBFunctions.Like(@"workbook.xls",@"work*.xls",true)); //Returns False (* matches any pattern) Debug.WriteLine(VBFunctions.Like(@"abcxls",@"*.xls",true)); //Returns True Debug.WriteLine(VBFunctions.Like(@"abcxls",@"?*xls",true)); //Returns False (as there are more ? than characters) Debug.WriteLine(VBFunctions.Like(@"workbook.xls",@"work?????.*",true)); //Returns True Debug.WriteLine(VBFunctions.Like(@"Abcxls",@"???xls",true)); //Returns True Debug.WriteLine(VBFunctions.Like(@"workbook.xls",@"work????.???",true)); //Returns False Debug.WriteLine(VBFunctions.Like(@"hello",@"[!h]ello",true)); //Returns True Debug.WriteLine(VBFunctions.Like(@"hello",@"[a-z]ello",true)); } } public class VBFunctions { ///
/// Indicates whether the regular expression specified in "pattern" could be found in the "text". ///
///
The string to search for a match ///
The pattern to find in the "text" string /// (supports the *, ? and # wildcard characters). /// ///
Returns true if the regular expression finds a match otherwise returns false.
///
/// ? 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. /// /// 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. ///
public static bool Like(string text, string pattern, bool ignoreCase) { // 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]"); 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); } } } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet