Daniel Kaplan
Daniel Kaplan

Reputation: 67360

What do pipes between brackets mean in a regex?

Reading this vim plugin I see this line:

syntax match tweeDelimiter "[<<|>>|\]\]|\[\[]"

To me, that regex doesn't make much sense when it's surrounded by []. According to this, "POSIX bracket expressions match one character out of a set of characters".

So isn't this matching < or > or [ or ]? I know from context that it's trying to match << or >> or [[ or ]].

Upvotes: 1

Views: 318

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172590

That indeed looks like a bug in the plugin. If it wants to match pairs of those characters, it has to use plain regexp branches (\|), not a collection:

<<\|>>\|\]\]\|\[\[

If there were additional stuff to match, above would have to be enclosed in \%(...\) to group it. However, using [...] will match any of the contained characters; Vim just ignores the duplicate ones. As others have commented already, such could be written in shorter form, for example [][<>|].


So, if the plugin indeed mistakenly matches stuff like <> and <[ instead of just << and [[, please inform its author about the bug.

Upvotes: 1

Related Questions