regex question
Günter Jantzen
nc-jantzegu at netcologne.de
Tue Apr 6 19:15:17 EDT 2004
"Rajarshi Guha" <rajarshi at presidency.com> schrieb im Newsbeitrag
news:pan.2004.04.06.00.54.58.864209 at presidency.com...
> Hi,
> this is not specifically a Python question but since I'm doing it in
> Python I thought I'd ask here.
>
> I have a BibTeX file with entries of the form
>
> @Article{dkapp3,
> author = {Kier, L.B},
> title = {Distinguishing Atom Differences in a Molecular Graph Index},
> journal = {Quant. Struct.-Act. Relat. Pharmacol.,Chem. Bio},
> year = {1986},
> OPTkey = {},
> volume = {5},
> OPTnumber = {},
> pages = {7-12},
> OPTmonth = {},
> OPTnote = {},
> OPTannote = {}
> }
>
> (it also has entries for Book and InBook)
>
> I have an re that is able to get each bibitem: @Article{(.*?)}\n
> From the result I can easily get the key using: @.+?\{(.+?),
>
> I was wondering if it would be possible to do the two things in one re.
> That is, first get the string of 1 bibitem and then within that get the
> key from the matched bibitem.
>
> I think I should be using a lookbehind(?) but I'm not sure.
>
> Any pointers would be appreicated
>
> Thanks
>
>
> --
> -------------------------------------------------------------------
> Rajarshi Guha <rajarshi at presidency.com> <http://jijo.cjb.net>
> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
> -------------------------------------------------------------------
> Q: What's purple and commutes?
> A: An abelian grape.
>
Hello Rajarshi,
I think the following program is not very useful, if the entries differ only
a bit, but it answers your question
import re
rx_bibtex= re.compile(
'''\@Article\{(?P<article>[^,]+),
*author = \{(?P<author>[^}]*)\},
*title = \{(?P<title>[^}]*)\},
*journal = \{(?P<journal>[^}]*)\},
*year = \{(?P<year>[^}]*)\},
*OPTkey = \{(?P<OPTkey>[^}]*)\},
*volume = \{(?P<volume>[^}]*)\},
*OPTnumber = \{(?P<OPTnumber>[^}]*)\},
*pages = \{(?P<pages>[^}]*)\},
*OPTmonth = \{(?P<OPTmonth>[^}]*)\},
*OPTnote = \{(?P<OPTnote>[^}]*)\},
*OPTannote = \{(?P<OPTannote>[^}]*)\}
\}''')
s='''@Article{dkapp3,
author = {Kier, L.B},
title = {Distinguishing Atom Differences in a Molecular Graph Index},
journal = {Quant. Struct.-Act. Relat. Pharmacol.,Chem. Bio},
year = {1986},
OPTkey = {},
volume = {5},
OPTnumber = {},
pages = {7-12},
OPTmonth = {},
OPTnote = {},
OPTannote = {}
}
'''
mo = rx_bibtex.match(s)
if mo:
print mo.groupdict()
Günter
More information about the Python-list
mailing list