如果你简单地改变每一个*
量词 a+
,正则表达式在以下情况下将无法工作*
should匹配次数为零。换句话说,问题将从always匹配零到never匹配零。如果你问我,这两种方法都没用。
但是,您也许可以使用负前瞻来单独处理零出现的情况。例如,x*
可以重写为(?:(?!x)|x+)
。我知道这很可怕,但这是我目前能想到的最独立的解决方案。对于占有欲强的明星,你也必须这样做(*+
),但不是不情愿的星星(*?
).
这是表格形式:
BEFORE AFTER
x* (?:(?!x)|x+)
x*+ (?:(?!x)|x++)
x*? x*?
More complex atoms would need to have their own parentheses preserved:
(?:xyz)* (?:(?!(?:xyz))|(?:xyz)+)
You could probably drop them inside the lookahead, but they don't hurt anything except readability, and that's a lost cause anyway. :D If the
{min,}
and
{min,max}
forms are affected too, they would get the same treatment (with the same modifications for possessive variants):
x{0,} same as x*
x{0,n} (?:(?!x)|x{1,n})
我突然想到条件语句——(?(condition)yes-pattern|no-pattern)
——非常适合这里;不幸的是,ICU似乎并不支持他们。