Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > backreferences

Reply
Thread Tools

backreferences

 
 
Amy Dillavou
Guest
Posts: n/a
 
      09-28-2005
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

 
Reply With Quote
 
 
 
 
Iain King
Guest
Posts: n/a
 
      09-28-2005

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

 
Reply With Quote
 
 
 
 
George Sakkis
Guest
Posts: n/a
 
      09-28-2005

"Amy Dillavou" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Amy Dillavou
Guest
Posts: n/a
 
      09-28-2005
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
>
>


 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      09-28-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can't get backreferences to work everymn@yahoo.com Perl 1 02-21-2007 12:25 AM
Backreferences in python ? Pankaj Python 7 01-24-2006 09:15 AM
regular expressions - math on backreferences Chris Nolte Perl 9 05-25-2004 07:43 PM
How to use backreferences in a variable for a regular expression Mark Fletcher Perl 1 05-19-2004 11:12 AM
java.util.regex: Backreferences? dhek bhun kho Java 2 07-09-2003 11:29 AM



Advertisments