regex question

Discussion in 'Python' started by Rajarshi Guha, Apr 6, 2004.

  1. 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 <> <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.
     
    Rajarshi Guha, Apr 6, 2004
    #1
    1. Advertising

  2. "Rajarshi Guha" <> schrieb im Newsbeitrag
    news:p...
    > 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 <> <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
     
    Günter Jantzen, Apr 7, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    715
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,635
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    603
  4. Xah Lee
    Replies:
    1
    Views:
    947
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    773
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page