regex question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rajarshi Guha

    regex question

    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@presi dency.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.

  • Günter Jantzen

    #2
    Re: regex question


    "Rajarshi Guha" <rajarshi@presi dency.com> schrieb im Newsbeitrag
    news:pan.2004.0 4.06.00.54.58.8 64209@presidenc y.com...[color=blue]
    > 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@presi dency.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.
    >[/color]

    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{d kapp3,
    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


    Comment

    Working...