我最近需要这个,所以我创建了一个完全遵循 LDAPv3 可分辨名称语法的一个:RFC-2253.
属性类型
attributeType 可以用两种方式表示。以 alpha 开头的字母数字字符串,使用以下方法进行验证:
[A-Za-z][\w-]*
或者它可以是一个 OID,使用以下方法进行验证:
\d+(?:\.\d+)*
因此 attributeType 验证使用:
[A-Za-z][\w-]*|\d+(?:\.\d+)*
属性值
attributeValue 可以用 3 种方式表示。十六进制字符串,它是带有前导的十六进制对序列#
。十六进制字符串使用以下方式进行验证:
#(?:[\dA-Fa-f]{2})+
或者转义字符串;每个非特殊字符均按“原样”表示(使用[^,=\+<>#;\\"]
)。特殊字符可以用前导来表示\
(验证使用\\[,=\+<>#;\\"]
)。最后,任何字符都可以表示为带有前导的十六进制对\
(验证使用\\[\dA-Fa-f]{2}
)。转义字符串使用以下方法进行验证:
(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*
或带引号的字符串;该值的开头和结尾为"
,并且可以包含任何转义字符,除了\
and "
。此外,可以使用上面转义字符串中的任何方法。带引号的字符串使用以下方式进行验证:
"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*"
所有组合起来, attributeValue 使用以下方式进行验证:
#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*"
名称组成部分
BNF 中的名称组件是:
name-component = attributeTypeAndValue *("+" attributeTypeAndValue)
attributeTypeAndValue = attributeType "=" attributeValue
在正则表达式中是:
(?#attributeType)=(?#attributeValue)(?:\+(?#attributeType)=(?#attributeValue))*
更换(?#attributeType)
and (?#attributeValue)
具有上述值的占位符为我们提供:
(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*")(?:\+(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*"))*
它验证单个名称组件。
专有名称
最后,专有名称的 BNF 为:
name-component *("," name-component)
在正则表达式中是:
(?#name-component)(?:,(?#name-component))*
将 (?#name-component) 占位符替换为上面的值可以得到:
^(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*")(?:\+(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*"))*(?:,(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*")(?:\+(?:[A-Za-z][\w-]*|\d+(?:\.\d+)*)=(?:#(?:[\dA-Fa-f]{2})+|(?:[^,=\+<>#;\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*|"(?:[^\\"]|\\[,=\+<>#;\\"]|\\[\dA-Fa-f]{2})*"))*)*$
在这里测试一下