Given searchString = "23423asdfa-''"
This regular expression should evaluate to false but it does not! Any ideas?
Regex rgx = new Regex(@"[\w-]*");
rgx.IsMatch(searchString)
It's because you haven't constrained it to match the entire string. Hence it is allowed to consider matches on subsets of the string. A very large subset of the string matches the data hence the regex returns true.
Try the following to force it to match the entire input.
Regex rgx = new Regex(@"^[\w-]*$");
rgx.IsMatch(searchString)
You need to anchor your expression. If you don't, then if any substring of the input matches, the regex match is considered successful. Change the regex to "^[\w-]*$" where the ^ and $ will match the beginning and end of the string, respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With