473,396 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

backreferences

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

Sep 28 '05 #1
4 1886

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

Sep 28 '05 #2

"Amy Dillavou" <gs****@line72.net> wrote in message
news:ma*************************************@pytho n.org...
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
Sep 28 '05 #3
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" <gs****@line72.net> wrote in message
news:ma*************************************@pytho n.org...
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


Sep 28 '05 #4
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

Sep 28 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Mlynarczyk | last post by:
Quote from PHP Manual: A back reference matches whatever actually matched the capturing subpattern in the current subject string, rather than anything matching the subpattern itself. So the...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
2
by: CSN | last post by:
Can you use backreferences in regular expressions like so? update table set title='foobar ' || \1 where title ~* '+(+)';
1
by: Martin Lucas-Smith | last post by:
Is there any way to use backreferences _within_ a _pattern_, as works on some regexp parsing applications, i.e. '<a href="mailto:(*)@(*)">\\1@\\2</a>' => '\\1@\\2' so that <a...
7
by: Pankaj | last post by:
I have something like below in perl and i am searching for equivalent in python: ::: Perl ::: *********** while( <FILEHANDLE> ) { line = $_;
4
by: Allen | last post by:
For my web-based php regex find/replace do-hickey, I need to match individual back references and wrap a tag around them so they'll be unique to the rest of the match for individual color markup. ...
1
by: everymn | last post by:
Could someone tell me what I'm doing wrong to cause the backreference variable to fail to be assigned here? It's entering the If block 4 times, so I know it's matching but there's nothing in $1? ...
4
by: TenTen | last post by:
hi, I'm a regex beginer and I want to know if with backreferences \1 I can reduce the ip test regex that I found on a web site : \b (25 |2 | ? ?)\. (25 |2 | ? ?)\. (25 |2 | ? ?)\. (25 |2 | ?...
8
by: Uwe Schmitt | last post by:
Hi, Is anobody aware of this post: http://swtch.com/~rsc/regexp/regexp1.html ? Are there any plans to speed up Pythons regular expression module ? Or is the example in this artricle too...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.