考虑一个算法,需要确定是否字符串
包含外白名单字符的任何字符。
Consider an algorithm that needs to determine if a string
contains any characters outside the whitelisted characters.
白名单看起来是这样的:
The whitelist looks like this:
' - abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÖÜáíóúñÑÀÁÂÃÈÊËÌÍÎÏÐÒÓÔÕØÙÚÛÝßãðõøýþÿ
'-.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);
}
注意事项:
感谢您的性能评价所有的回答者。性能不成问题。质量,可读性和可维护性!少code =缺陷的机会较少,海事组织。
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.
问:
我应该这样白名单正则表达式是什么?
What should this whitelist regex pattern be?
您可以使用下面的模式匹配:
You could pattern match using the following:
^([\-\.a-zA-Z ÇüéâäàåçêëèïîíìÄÅÉæÆôöòûùÖÜáíóúñÑÀÁÂÃÈÊËÌÍÎÏÐÒÓÔÕØÙÚÛÝßãðõøýþÿ]+)$
请用扩展方法:
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.