Jonathan
Jonathan

Reputation: 11321

vim regex: negative backreferences?

I'm trying to match XML entries where the link doesn't correspond to the link text, like this:

<link linkend="G431">X595</link> 

but NOT links where the link text is correct, like this:

<link linkend="G431">G431</link> 

How can I write a vim regex to match the first of those? I've tried things like:

linkend="\([A-Z]\d\+\)">[^\1]

and

linkend="\([A-Z]\d\+\)">^\(\1\)\@!

but I can't quite seem to get anything to work. Basically I'm trying to say "find a link where the link text does NOT equal the link location."

Upvotes: 2

Views: 241

Answers (3)

Patrick Bacon
Patrick Bacon

Reputation: 4640

Your approach is pretty close to being correct.

I just used negative lookahead as seen here:

linkend="\([A-Z]\d\+\)">\(\1\)\@!

Upvotes: 1

ZyX
ZyX

Reputation: 53614

In your second variant you have …\)">^\(\1\)\@!…. You don’t need caret here: it means itself (caret) in this position (usually it means “start of line”, but not there: see :h /^). Neither you need parenthesis (they do not harm, but they are not needed):

linkend="\([A-Z]\d\+\)">\1\@!

. Negation is expressed via ! (positive look-ahead is \@=). In [^\1] variant you match any character, but backslash, ASCII one and line break: collections do not support back references and even if they did support it it would be illogical for a collection to match more then one character (so if they did it would pull a set of characters present in a matched text).

Upvotes: 4

peterfoldi
peterfoldi

Reputation: 7471

/<link linkend="\(.*\)">\(\1\)<\/link>

Upvotes: 0

Related Questions