backreferences

A

Amy Dillavou

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

Iain King

Amy said:
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
 
G

George Sakkis

Amy Dillavou said:
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
 
A

Amy Dillavou

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

Peter

Iain said:
Amy Dillavou wrote:



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

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
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top