tags:

views:

34

answers:

2

Given lines that look like the following:
Blah \cite[9.1173]{Joyce:1986aa}\autocite[42]{Kenner:1970ab}\autocite[108]{Hall:1960aa} bbb.\n
I’d like to remove the second (and any subsequent) occurrence of \autocite, resulting in the following:
Blah \autocite[9.1173]{Joyce:1986aa}[42]{Kenner:1970ab}[108]{Hall:1960aa} bbb.\n

I’m struggling to express this in regex form (I’m using the python 2.7 RE module), however, as I’m not sure how to formulate “remove only the second and subsequent occurrences of \autocite when followed by […]{…}, until a space or period is encountered”.

+3  A: 

Regular expressions are not a panacea.

l = s.split('\\autocite')
print '%s\\autocite%s' % (l[0], ''.join(l[1:]))
Ignacio Vazquez-Abrams
+1 Nice. Hammers and screws come to mind.
djna
You’re absolutely right. Couldn’t see the wood for the trees.
urschrei
A: 

If you absolutly want regexes you can use (?<=\\autocite)(.*?)\\autocite(.*) and replace with \1\2.

But @Ignacio Vazquez-Abrams answer is way better an efficient.

Colin Hebert