I have a string which varies everytime it is input as it is a search query, an example of this may be Z00044XLE1311, however I wish to do that the same product can be returned if the customer removes all the 0’s (there is always zeros after the first few letters) however I don’t seem to be able to do this, I want to essentially search for Z*44XLE1311 instead.
I would prefer to do this using Regex as this is how my current search feature is done. I am aware that I should use substitution but I am unsure how to use this properly for this problem.
My current code is,
if (!string.IsNullOrEmpty(base.RemoveCharacters))
{
var regex = new Regex(string.Format("[{0}]+", base.RemoveCharacters));
text = regex.Replace(text, string.Empty);
}
if (Regex.IsMatch(text, "^[a-z]+00+", RegexOptions.IgnoreCase))
{
text = new Regex("0{1,}", RegexOptions.IgnoreCase).Replace(text, "00*", 1);
}
else
{
if (Regex.IsMatch(text, "^[a-z]+", RegexOptions.IgnoreCase))
{
text = new Regex("[A-Z]{1,}", RegexOptions.IgnoreCase).Replace(text, "[A-Z]*", 1);
}
URL = WebUtilities.AddToQueryString(URL, box.Key, Page.Server.UrlEncode(text));
}
}
I actually have this now working, I have used: