不建群怎么表达"match a word or another"?

How to express "match a word or another" without creating a group?

我正在尝试用正则表达式匹配这种字符串:

{{location|
{{location dec|
{{location other|

所以我想到了这个正则表达式:

{{location( dec| other|)

效果很好,但是它在 ( dec|) 创建了一个我不需要的组。有什么方法可以做同样的事情而不创建组吗?

您可以使用 ?: 来阻止创建群组,所以请尝试:

{{location(?: dec| other|)

您需要一个组,但您可以通过在左括号后添加 ?: 使其成为非捕获组:

(?: dec| other|)

非捕获表示组只为表达式而存在;没有反向引用是可能的,组编号不受影响。