regex question

R

Rajarshi Guha

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
 
G

Günter Jantzen

Rajarshi Guha said:
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


--

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top