gpt4 book ai didi

regex - 将 ABNF 规则转换为正则表达式

转载 作者:行者123 更新时间:2023-12-04 04:33:08 24 4
gpt4 key购买 nike

请帮我理解这个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 规则的理解,或者任何人都可以确认或更正我吗?

最佳答案

Internet 规范通常需要定义格式语法。增广巴科斯-诺尔形式 ( 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com