我正在尝试实现一个正则表达式,它能够将相同的关键字或关键字组合分配给一个或多个命名组。
例如,我想匹配('aa' AND 'bb') OR 'cc'并将 'aa' AND 'bb' 分配给 group<1> 并将 'cc' 分配给 group<2>。
我也可以有一个像('aa' AND 'bb') OR 'aa'这样的查询,我希望 'aa' AND 'bb' 在 group<1> 中,同时 'aa' 在 group< 2>。
// Works to get 'aa' everywhere but cannot find a way to add 'bb' to the group<1>
(?=(?:\s+|^)(?<1>aa)(?:\s+|$))
编辑 :
Input Example : bb is nice but not without the missingaa
Output : Does not Validate, Group<1> is null | Group<2> is null
-
Input Example : bb is nice as well as aa
Output : Validate, Group<1> : bb is nice as well as aa | Group<2> is null
-
Input Example : bb is nice but not without the missingaa or cc
Output : Validate, Group<1> is null | Group<2> is cc
-
Input Example : bb is nice as well as aa or cc
Output : Validate, Group<1> is bb is nice as well as aa | Group<2> is cc
我知道分组可能很复杂,但我希望 Group<1> 如果存在 aa 和 bb 则不为空。
我怎样才能实现这种行为?