I'm trying to search and replace citations from pandoc-markdown. They have the following syntax:
[prenote @autorkey, postnote]
Or for more than one Author
[prenote1 @authorekey1, postnote1; prenote2 @authorkey2, postnote2]
The pre-notes, the author-keys and the post-notes should each be in their own capture group.
For only one author in a citation I used regex this:
\[((.*) )?@(.*?)(, (.*))?\]
But I can't figure out how to match a citation with multiple authors. Ideally it would be possible to match citations with one or more author keys. The pre-note and the post-note should be optional.
Is this possible?
We need more context with code (full sample code) to be able to answer fully, so I can only answer in the same general way in which you asked the question.
I do not believe you can do it in one operation with one regular expression.
So the overall technique I would use is:
[ and ].; to get a list of "prenote @authorkey, postnote" strings.[ and ] in around it.You can put steps 2 to 4 in a function f(match_object), and then use re.sub(pattern, f, string) to do the replacement. It will call function f for each match it finds, and replace that match with the return value of f.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With