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?