我想在一个正则表达式中组合AND和OR操作。
我有2套标签。设置A和设置B.在一个集合中,我需要一个OR运算,并在设置AND。
之间示例:
Set A has 3 values:
A1, A2, A3
Set B has 2 values:
B1 and B2
搜索操作示例:
A1 OR A2 AND B2
A2 OR A3 AND B1 OR B2
...
知道如何实现这一目标吗?
答案 0 :(得分:1)
在一个集合中,我需要一个OR运算,并在之间设置AND。
您可以使用这样的前瞻:
^(?=.*?(?:A1|A2|A3))(?=.*?(?:B1|B2)).*$
说明强>
(?=.*?(?:A1|A2|A3)) Positive Lookahead - Assert that the regex below can be matched
.*? matches any character (except newline)
Quantifier: Between zero and unlimited times, as few times as possible, expanding as needed
(?:A1|A2|A3) Non-capturing group
1st Alternative: (null, matches any position)
2nd Alternative: A1
A1 matches the characters A1 literally (case sensitive)
3rd Alternative: A2
A2 matches the characters A2 literally (case sensitive)
4th Alternative: A3
A3 matches the characters A3 literally (case sensitive)
(?=.*?(?:B1|B2)) Positive Lookahead - Assert that the regex below can be matched
.*? matches any character (except newline)
Quantifier: Between zero and unlimited times, as few times as possible, expanding as needed
(?:B1|B2) Non-capturing group
1st Alternative: (null, matches any position)
2nd Alternative: B1
B1 matches the characters B1 literally (case sensitive)
3rd Alternative: B2
B2 matches the characters B2 literally (case sensitive)
答案 1 :(得分:0)
我会在这里提交Regexps是一个错误的工具。你想要一个解析器。或者,更简单地说,将字符串拆分为"AND"
,然后为每个元素拆分"OR"
,并检查所有标记是否在同一个集合中。