Reputation: 531
I am using a regular expression in an MVC app that allows users to create a username that includes many special characters, including *
. One small problem, when searching for a user, we allow wildcard searches that use the *
for the wildcard portion of the search. This is what the regular expression looks like on the creation side:
@"^([a-zA-Z0-9!\@#\$%\^&\*\(\)-_\+\.'`~/=\?\{\}\|]){6,255}$"
And here is what the regular expression would look like on the search side:
@"^([a-zA-Z0-9!\@#\$%\^&\*\(\)-_\+\.'`~/=\?\{\}\|]){6,255}$|([\w]){0,0})"
Is there a way to make this work properly by allowing a user to search a whole username that may include a *
in the username, while still allowing other users to use the *
as a wildcard search?
Upvotes: 0
Views: 320
Reputation: 39329
Short of reading the user's mind there's really no way to know if they meant *
literally or as a wildcard. I'd recommend running the search both ways and sorting the results with the literal interpretation given higher precedence.
Here's approximately what an implementation might look like, with the search details abstracted away in some DoSearch
method:
IList<Result> GetResults(string input)
{
var expr = Regex.Escape(input);
// search once with * as a literal:
var results1 = DoSearch(expr);
// replace the * literal with regex snippet:
expr = expr.Replace("\\*", "(.*)");
// search again with * as a wildcard
var results2 = DoSearch(expr);
// literal gets precidence
return results1.Concat(results2).ToList();
}
Upvotes: 3