laurent
laurent

Reputation: 90776

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

I'm trying to match this kind of strings with a regular expression:

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

so I came up with this regular expression:

{{location( dec| other|)

which works fine, however it's creating a group at ( dec|), which I don't need. Is there any way to do the same thing but without creating a group?

Upvotes: 1

Views: 81

Answers (2)

Bohemian
Bohemian

Reputation: 425033

You need a group, but you can make it a non-capturing group by adding ?: after the opening bracket:

(?: dec| other|)

Non-capturing means the group exists just for the expression; no back references are possible and group numbering is unaffected.

Upvotes: 4

Aguardientico
Aguardientico

Reputation: 7779

You can use ?: to prevent group creation so please give a try to:

{{location(?: dec| other|)

Upvotes: 2

Related Questions