用替代和可选解析正则表达式 [英] Parsing regex with alternatives and optionals
问题描述
我正在构建 RiveScript 的聊天机器人子集,并尝试使用正则表达式构建模式匹配解析器.哪三个正则表达式与以下三个示例匹配?
I'm building a chatbot subset of RiveScript and trying to build the pattern matching parser with regular expression. Which three regexes match the following three examples?
ex1: I am * years old
valid match:
- "I am 24 years old"
invalid match:
- "I am years old"
ex2: what color is [my|your|his|her] (bright red|blue|green|lemon chiffon) *
valid matches:
- "what color is lemon chiffon car"
- "what color is my some random text till the end of string"
ex3: [*] told me to say *
valid matches:
- "Bob and Alice told me to say hallelujah"
- "told me to say by nobody"
通配符表示可以接受任何不为空的文本.
The wildcards mean any text that is not empty is acceptable.
在示例2中,[ ]
之间的任何内容是可选的,( )
之间的任何内容都是替代的,每个选项或替代选项之间都用|
分隔.
In example 2, anything between [ ]
is optional, anything between ( )
is alternative, each option or alternative is separated by a |
.
在示例3中,[*]
是可选的通配符,表示可以接受空白文本.
In example 3, the [*]
is an optional wildcard, meaning blank text can be accepted.
推荐答案
I am (?:\d+) years old
https://regex101.com/r/CuZuMi/2
what color is.*(?:my|your|his|her).*(?:bright red|blue|green|lemon chiffon)?.*
https://regex101.com/r/CuZuMi/3
.*told me to say.*
我主要使用两件事:
I am using mostly 2 things:
-
(?:)
非捕获组,用于将事物分组在一起,例如在数学上使用括号. -
.*
匹配任意字符0次或更多次.可以替换为{1,3}
以匹配1到3次.
(?:)
non-capture groups, to group things together like the parenthesis use on math..*
match any character 0 or more times. Could be replaced by{1,3}
to match between 1 and 3 times.
您可以用+
交换*
以匹配至少1个字符,而不是0.
非捕获组之后的?
使该组成为可选.
You can exchange *
by +
to match at least 1 character, instead of 0.
And the ?
after the non-capture group, makes that group optional.
这些是您开始的黄金地点:
These are golden place for you to start:
- http://www.rexegg.com/regex-quickstart.html
- https://regexone.com/
- http://www.regular-expressions.info/quickstart.html
- 参考-此正则表达式是什么意思?
- http://www.rexegg.com/regex-quickstart.html
- https://regexone.com/
- http://www.regular-expressions.info/quickstart.html
- Reference - What does this regex mean?
这篇关于用替代和可选解析正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!