backreferences

Discussion in 'Python' started by Amy Dillavou, Sep 28, 2005.

  1. Amy Dillavou

    Amy Dillavou Guest

    Can someone help me with understanding how python uses backreferences?
    I need to remember the item that was last matched by the re engine but i
    cant seem to understand anything that I find on backreferences. if I
    want to access the last match do i use \number or is there something
    else i have to do?

    heres part of my code:
    renDate = re.compile("$((\d){4}-(\d){2}-(\d){2}))")
    renDate.search(l)
    if(exist.search(l) and str(lastmodDate) < \1): #i need help here with \1

    Thanks in advance
    A.D
    Amy Dillavou, Sep 28, 2005
    #1
    1. Advertising

  2. Amy Dillavou

    Iain King Guest

    Amy Dillavou wrote:
    > Can someone help me with understanding how python uses backreferences?
    > I need to remember the item that was last matched by the re engine but i
    > cant seem to understand anything that I find on backreferences. if I
    > want to access the last match do i use \number or is there something
    > else i have to do?
    >
    > heres part of my code:
    > renDate = re.compile("$((\d){4}-(\d){2}-(\d){2}))")
    > renDate.search(l)
    > if(exist.search(l) and str(lastmodDate) < \1): #i need help here with \1
    >
    > Thanks in advance
    > A.D


    I haven't had to use backreferences yet, so I don't know offhand how to
    go about it. What I do know is that this online book is very useful:

    http://gnosis.cx/TPiP/


    Chapter 3 covers REs:

    http://gnosis.cx/TPiP/chap3.txt

    >From what I remember, in python you can use numbered backreferences (up

    to 99), or named ones.

    Iain
    Iain King, Sep 28, 2005
    #2
    1. Advertising

  3. "Amy Dillavou" <> wrote in message
    news:...
    > Can someone help me with understanding how python uses backreferences?
    > I need to remember the item that was last matched by the re engine but i
    > cant seem to understand anything that I find on backreferences. if I
    > want to access the last match do i use \number or is there something
    > else i have to do?
    >
    > heres part of my code:
    > renDate = re.compile("$((\d){4}-(\d){2}-(\d){2}))")
    > renDate.search(l)
    > if(exist.search(l) and str(lastmodDate) < \1): #i need help here with \1
    >
    > Thanks in advance
    > A.D


    renDate = re.compile(some_regex)
    match = renDate.search(input)
    if match and str(lastmodDate) < match.group(1):
    do_something()


    HTH,
    George
    George Sakkis, Sep 28, 2005
    #3
  4. Amy Dillavou

    Amy Dillavou Guest

    George and Iain -
    Thanks for your help!!! It worked for me, and that book seems to be
    really useful =)
    A.D

    On Wed, 2005-09-28 at 11:16 -0400, George Sakkis wrote:
    > "Amy Dillavou" <> wrote in message
    > news:...
    > > Can someone help me with understanding how python uses backreferences?
    > > I need to remember the item that was last matched by the re engine but i
    > > cant seem to understand anything that I find on backreferences. if I
    > > want to access the last match do i use \number or is there something
    > > else i have to do?
    > >
    > > heres part of my code:
    > > renDate = re.compile("$((\d){4}-(\d){2}-(\d){2}))")
    > > renDate.search(l)
    > > if(exist.search(l) and str(lastmodDate) < \1): #i need help here with \1
    > >
    > > Thanks in advance
    > > A.D

    >
    > renDate = re.compile(some_regex)
    > match = renDate.search(input)
    > if match and str(lastmodDate) < match.group(1):
    > do_something()
    >
    >
    > HTH,
    > George
    >
    >
    Amy Dillavou, Sep 28, 2005
    #4
  5. Amy Dillavou

    Peter Guest

    Iain King wrote:

    >Amy Dillavou wrote:
    >
    >
    >>Can someone help me with understanding how python uses backreferences?
    >>I need to remember the item that was last matched by the re engine but i
    >>cant seem to understand anything that I find on backreferences. if I
    >>want to access the last match do i use \number or is there something
    >>else i have to do?
    >>
    >>heres part of my code:
    >>renDate = re.compile("$((\d){4}-(\d){2}-(\d){2}))")
    >>renDate.search(l)
    >>if(exist.search(l) and str(lastmodDate) < \1): #i need help here with \1
    >>
    >>Thanks in advance
    >>A.D
    >>
    >>

    >
    >I haven't had to use backreferences yet, so I don't know offhand how to
    >go about it. What I do know is that this online book is very useful:
    >
    >http://gnosis.cx/TPiP/
    >
    >
    >Chapter 3 covers REs:
    >
    >http://gnosis.cx/TPiP/chap3.txt
    >
    >>From what I remember, in python you can use numbered backreferences (up

    >to 99), or named ones.
    >
    >Iain
    >
    >
    >

    This shows both named backreferences and indexed. In both the
    replacement and the searching.

    (?P<name>data) sets the value of a named backreference named name to the
    value of data (but can only be used in the pattern).

    \NUMBER gets the value of a backreference by its index (can be used in
    the pattern or the replacement).

    (?P=name) gets the value of a named backreference (but only in the pattern).

    \g<name> gets the value of a named backreference (but only in the
    replacement).

    Example:

    re.sub(r"""<(?P<tag>H[1-5]) style="(.*)">(?P<text>.*)</(?P=tag)>""",
    r"""<B style="\2">\g<text></B> | <\1 style="\2">\3<\1>""",
    """<H1 style="color: #000000">abc</H1>"""
    )

    HTH,
    Peter
    Peter, Sep 28, 2005
    #5
    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. Mark Fletcher
    Replies:
    1
    Views:
    481
    Mark Fletcher
    May 19, 2004
  2. Chris Nolte
    Replies:
    9
    Views:
    4,265
    Jeff Schwab
    May 25, 2004
  3. dhek bhun kho

    java.util.regex: Backreferences?

    dhek bhun kho, Jul 9, 2003, in forum: Java
    Replies:
    2
    Views:
    777
    dhek bhun kho
    Jul 9, 2003
  4. Pankaj

    Backreferences in python ?

    Pankaj, Jan 23, 2006, in forum: Python
    Replies:
    7
    Views:
    3,256
    Sion Arrowsmith
    Jan 24, 2006
  5. Replies:
    1
    Views:
    1,940
Loading...

Share This Page