.NET的正则表达式白名单字符 [英] .NET Regex for whitelisted characters

查看:118
本文介绍了.NET的正则表达式白名单字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个算法,需要确定是否字符串包含外白名单字符的任何字符。

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.

这篇关于.NET的正则表达式白名单字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆