[英]RegEx multiple optional characters in group
How would I define optional characters in a group? 如何在组中定义可选字符?
I am trying to match the following... 我想尝试匹配以下内容......
kg
kilo
kilos
kilogram
kilograms
g
gram
grams
I know I can put them individually in a group, but was wondering if I could do something fancy like this... 我知道我可以将它们单独放在一个组中,但我想知道我是否可以做这样的奇特......
(kg|kilo?g?ram?s?)
Problem is it could match only the s? 问题是它只能匹配s? or none of the second alternation so it would match zero length.
或者没有第二次交替,所以它将匹配零长度。
I would start by enumerating all of the possible match conditions and then paring down from there to see if there is a more efficient solution: 我首先列举所有可能的匹配条件,然后从那里开始削减,看看是否有更有效的解决方案:
kg|kilo|kilos|kilogram|kilograms|g|gram|grams
the plural 's' is an obvious redundancy: 复数''是一个明显的冗余:
kg|kilos?|kilograms?|g|grams?
g and kg can be collapsed: g和kg可以折叠:
k?g|kilos?|kilograms?|grams?
We can collapse the units for kilograms: 我们可以将单位折成千克:
k?g|kilo(?:s|grams?)?|grams?
Are you OK with the six character duplication of "grams?" 对于“克”的六个字符重复,你还好吗? :)
:)
You can use (?:)
to group items without capturing (this works in most RegEx flavours; look up "non-capturing groups" in your engine's documetation if you are unsure). 您可以使用
(?:)
对项目进行分组而不进行捕获(这适用于大多数RegEx风格;如果您不确定,请在引擎的文档中查找“非捕获组”)。
With that, you can try something like this: 有了它,你可以尝试这样的事情:
(k?g|(?:kilo)?grams?|kilos?)
This matches exactly 这完全匹配
g kg gram grams kilogram kilograms kilo kilos
and nothing else. 没有别的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.