In this particular case, I want to extract the argument of a latex command, as:
... latex code ...
\bibitem{item1}
... latex code ...
\bibitem{item2}
... latex code ...
Is there any function in python which will retrieve a list containing item1, item2, etc.?
I would imagine something like:
latex_text.extract_argument("\bibitem{","}")
a function which would scan the text and return a list of every argument contained within the two delimiters set.
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
2 Answers 2
import re
re.findall(r'\\bibitem\{(.*?)\}', latex_text) # ['item1', 'item2']
answered Oct 31, 2012 at 12:01
Pavel Anossov
63.4k16 gold badges156 silver badges125 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Use re.search:
import re
l = '\\bibitem{item1}'
m = re.search(r'\{([^}]*)\}', l)
print m.group(1) # => 'item1'
Explanation of the regular expression \{([^}]*)\}:
- We are looking for a starting
{which we must escape as\{. - We use a group
(...)that contains - any number
*of characters that are not}:([^}]
The group from step 2 is m.group(1).
answered Oct 31, 2012 at 11:59
user647772
1 Comment
leandro
Very nice, thanks for the explanation on the regular expressions, it will be very helpful.
lang-py
item1contains commands which uses}then the regexp answers will not suffice, since you need to count opening and closing braces to find the one that closes thebibitemcommand. Have you looked at the.bblfile thatbibtexproduces?