How to remove repeating non-adjacent string
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”.
Regular expressions are not a panacea.
l = s.split('\\autocite')
print '%s\\autocite%s' % (l[0], ''.join(l[1:]))
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.
精彩评论