<p>I'm looking to convert a SQL like statement on the fly to the equivalent regex i.e. </p>

<pre><code>LIKE '%this%'
LIKE 'Sm_th'
LIKE '[C-P]arsen'
</code></pre>

<p>What's the best approach to doing this?</p>

<p>P.S. I'm looking to do this on the .Net Framework (C#).</p>

[{'answer_id': 47075, 'author': 'Eugene Yokota', 'author_id': 3827, 'author_profile': 'https://Stackoverflow.com/users/3827', 'pm_score': -1, 'selected': False, 'text': '<p>I found a Perl module called <a href="http://search.cpan.org/dist/Regexp-Wildcards/lib/Regexp/Wildcards.pm" rel="nofollow noreferrer">Regexp::Wildcards</a>. You can try to port it or try Perl.NET. I have a feeling you can write something up yourself too.</p>\n'}, {'answer_id': 47088, 'author': 'Martin', 'author_id': 2581, 'author_profile': 'https://Stackoverflow.com/users/2581', 'pm_score': 2, 'selected': False, 'text': "<p>From your example above, I would attack it like this (I speak in general terms because I do not know C#):</p>\n\n<p>Break it apart by <strong>LIKE '...'</strong>, put the <strong>...</strong> pieces into an array.\nReplace unescaped <strong>%</strong> signs by <strong>.*</strong>, underscores by ., and in this case the <strong>[C-P]arsen</strong> translates directly into regex.</p>\n\n<p>Join the array pieces back together with a pipe, and wrap the result in parentheses, and standard regex bits.</p>\n\n<p>The result would be:</p>\n\n<pre><code>/^(.*this.*|Sm.th|[C-P]arsen)$/\n</code></pre>\n\n<p>The most important thing here is to be wary of all the ways you can escape data, and which wildcards translate to which regular expressions.</p>\n\n<pre><code>% becomes .*\n_ becomes .\n</code></pre>\n"}, {'answer_id': 13180223, 'author': 'Nathan Baulch', 'author_id': 8799, 'author_profile': 'https://Stackoverflow.com/users/8799', 'pm_score': 4, 'selected': True, 'text': '<p>The following Regex converts an SQL like pattern into a Regex pattern with the help of a <a href="http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx" rel="nofollow noreferrer"><code>MatchEvaluator</code></a> delegate. It correctly handles square bracket blocks and escapes special Regex characters.</p>\n<pre><code>string regexPattern = &quot;^&quot; + Regex.Replace(\n    likePattern,\n    @&quot;[%_]|\\[[^]]*\\]|[^%_[]+&quot;,\n    match =&gt;\n    {\n        if (match.Value == &quot;%&quot;)\n        {\n            return &quot;.*&quot;;\n        }\n        if (match.Value == &quot;_&quot;)\n        {\n            return &quot;.&quot;;\n        }\n        if (match.Value.StartsWith(&quot;[&quot;) &amp;&amp; match.Value.EndsWith(&quot;]&quot;))\n        {\n            return match.Value;\n        }\n        return Regex.Escape(match.Value);\n    }) + &quot;$&quot;;\n</code></pre>\n'}, {'answer_id': 29847510, 'author': 'TheEadie', 'author_id': 1930030, 'author_profile': 'https://Stackoverflow.com/users/1930030', 'pm_score': 2, 'selected': False, 'text': '<p>In addition to @Nathan-Baulch\'s solution you can use the code below to also handle the case where a custom escape character has been defined using the <code>LIKE \'!%\' ESCAPE \'!\'</code> syntax.</p>\n\n<pre><code>   public Regex ConvertSqlLikeToDotNetRegex(string regex, char? likeEscape = null)\n   {\n        var pattern = string.Format(@"\n            {0}[%_]|\n            [%_]|\n            \\[[^]]*\\]|\n            [^%_[{0}]+\n            ", likeEscape);\n\n        var regexPattern = Regex.Replace(\n            regex,\n            pattern,\n            ConvertWildcardsAndEscapedCharacters,\n            RegexOptions.IgnorePatternWhitespace);\n\n        regexPattern = "^" + regexPattern + "$";\n\n        return new Regex(regexPattern,\n            !m_CaseSensitive ? RegexOptions.IgnoreCase : RegexOptions.None);\n    }\n\n    private string ConvertWildcardsAndEscapedCharacters(Match match)\n    {\n        // Wildcards\n        switch (match.Value)\n        {\n            case "%":\n                return ".*";\n            case "_":\n                return ".";\n        }\n\n        // Remove SQL defined escape characters from C# regex\n        if (StartsWithEscapeCharacter(match.Value, likeEscape))\n        {\n            return match.Value.Remove(0, 1);\n        }\n\n        // Pass anything contained in []s straight through \n        // (These have the same behaviour in SQL LIKE Regex and C# Regex)\n        if (StartsAndEndsWithSquareBrackets(match.Value))\n        {\n            return match.Value;\n        }\n\n        return Regex.Escape(match.Value);\n    }\n\n    private static bool StartsAndEndsWithSquareBrackets(string text)\n    {\n        return text.StartsWith("[", StringComparison.Ordinal) &amp;&amp;\n               text.EndsWith("]", StringComparison.Ordinal);\n    }\n\n    private bool StartsWithEscapeCharacter(string text, char? likeEscape)\n    {\n        return (likeEscape != null) &amp;&amp;\n               text.StartsWith(likeEscape.ToString(), StringComparison.Ordinal);\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/47052', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4843/']