请帮我理解这一点ABNF规则 ([a-z]* [A-Z]* [0-9]*)*
。
我认为它可以转换为像这样的正则表达式 [a-zA-Z0-9]*
。因此,ABNF 规则应该匹配任意顺序的小写和/或大写字母和/或数字及其组合。例如,下面的字符串应该与规则匹配。
"ABC", "abc", "abc12", "aAbC876", "123go", etc.
如果ABNF规则是([a-z]* [A-Z]* | [0-9]*)*
,它也可以转换为相同的正则表达式。
验证正则表达式很容易,但是有没有工具或其他东西可以验证我对这些 ABNF 规则的理解,或者有人可以确认或更正我吗?
最佳答案
互联网规范通常需要定义格式语法。 增强巴科斯-诺尔范式 (ABNF) 是 Backus-Naur Form
的修改版本(通常用于描述计算中使用的语言的语法)并且在许多此类规范中很流行,以平衡紧凑性和简单性。
ABNF 在标准 BNF 中具有一组特定的核心规则。
你的规则:
([a-z]* [A-Z]* [0-9]*)*
作为ABNF规则的解释:
( ) Elements enclosed in parentheses are treated as a
single element whose contents are strictly ordered.
[ ] Square brackets enclose an optional element sequence
a-z A-Z Core rule for a ALPHA character
0-9 Core rule for a DIGIT character
* Repeat (Repetition rule)
翻译成扩展正则表达式的规则几乎是相同的。
([a-z]*[A-Z]*[0-9]*)*
说明:
( group and capture to \1 (0 or more times)
[a-z]* any character of: 'a' to 'z' (0 or more times)
[A-Z]* any character of: 'A' to 'Z' (0 or more times)
[0-9]* any character of: '0' to '9' (0 or more times)
)* end of \1
ABNF 规则与基本正则表达式类似,都涉及命名规则、重复、替代、顺序无关和范围。
关于regex - 将 ABNF 规则转换为正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20233894/