正则表达式匹配必需/可选元素,仅此而已

时间:2018-09-09 02:39:35

标签: regex

我需要一个正则表达式来以任何顺序匹配两个项目,但还允许其他两个可选元素。正则表达式只能匹配这两个到四个项目,而不能匹配其他任何项目。

例如,我想将“ high”和“ unrounded”与可选的“ back”和“ tense”匹配。

输入:

high back tense unrounded     # ==> match (two required elements + two optional)
high unrounded                # ==> match (just two required, no optional elements)
unrounded high                # ==> match (two required elements, any order)
high back unrounded           # ==> match (two required elements and one optional one)
tense unrounded back high     # ==> match (any order + optional elements)
lax unrounded                 # ==> no match (doesn't include one required element)
high back tense unrounded lax # ==> no match (includes more than the two required and two optional elements)

她是我当前的正则表达式:

(?i)(?=.*high)((?=.*back))?((?=.*tense))?(?=.*unrounded)

它匹配我想要的所有内容,但也匹配上一个示例(我不想要的东西)。我可以使它与包含两个以上必需元素和两个可选元素以外的东西不匹配吗?

0 个答案:

没有答案