Consider an algorithm that needs to determine if a string
contains any characters outside the whitelisted characters.
The whitelist looks like this:
'-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÖÜáíóúñÑÀÁÂÃÈÊËÌÍÎÏÐÒÓÔÕØÙÚÛÝßãðõøýþÿ
Note: spaces and apostrophes are needed to be included in this whitelist.
Typically this will be a static method, but it will be converted to an extension method.
private bool ContainsAllWhitelistedCharacters(string input)
{
string regExPattern="";// the whitelist
return Regex.IsMatch(input, regExPattern);
}
Considerations:
Thanks for the performance comments to all the answerers. Performance is not an issue. Quality, readability and maintainability is! Less code = less chance for defects, IMO.
Question:
What should this whitelist regex pattern be?
Why does it have to be a regex?
private bool ContainsAllWhitelistedCharacters(string input)
{
string whitelist = "abcdefg...";
foreach (char c in input) {
if (whitelist.IndexOf(c) == -1)
return false;
}
return true;
}
No need to jump straight into regexes if you aren't sure how to implement the one you need and you haven't profiled that section of code and found out you need the extra performance.
You could pattern match using the following:
^([\-\.a-zA-Z ÇüéâäàåçêëèïîíìÄÅÉæÆôöòûùÖÜáíóúñÑÀÁÂÃÈÊËÌÍÎÏÐÒÓÔÕØÙÚÛÝßãðõøýþÿ]+)$
Make it an extension method with:
public static bool IsValidCustom(this string value)
{
string regExPattern="^([\-\.a-zA-Z ÇüéâäàåçêëèïîíìÄÅÉæÆôöòûùÖÜáíóúñÑÀÁÂÃÈÊËÌÍÎÏÐÒÓÔÕØÙÚÛÝßãðõøýþÿ]+)$";
return Regex.IsMatch(input, regExPattern);
}
I can't think of an easy way to do a maintainable range with extended characters since the order of the characters is not obvious.
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