Quantcast
Channel: Need to perform Wildcard (*,?, etc) search on a string using Regex - Stack Overflow
Viewing all articles
Browse latest Browse all 12

Answer by user3816530 for Need to perform Wildcard (*,?, etc) search on a string using Regex

$
0
0

All upper code is not correct to the end.

This is because when searching zz*foo* or zz* you will not get correct results.

And if you search "abcd*" in "abcd" in TotalCommander will he find a abcd file so all upper code is wrong.

Here is the correct code.

public string WildcardToRegex(string pattern){                 string result= Regex.Escape(pattern).        Replace(@"\*", ".+?").        Replace(@"\?", ".");     if (result.EndsWith(".+?"))    {        result = result.Remove(result.Length - 3, 3);        result += ".*";    }    return result;}

Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>