473,698 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1894

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******** *************** **************@ python.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******** *************** **************@ python.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.searc h(l)
if(exist.sear ch(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<t ext></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
2551
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 pattern (sens|respons)e and \1ibility matches "sense and sensibility" and "response and responsibility", but not "sense and responsibility".
4
62105
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 supposed to write this function? String.replace(/</g,'&lt;');
2
1643
by: CSN | last post by:
Can you use backreferences in regular expressions like so? update table set title='foobar ' || \1 where title ~* '+(+)';
1
1207
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 href="mailto:foo@example.com">foo@example.com</a> WOULD match and be replaced by foo@example.com
7
1573
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
4502
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. Initially this would seem easy enough, however not all of a potential regex match is going to be within a back reference. So it's necessary to replace the back reference, and only the back reference, while preserving the context of the match. ...
1
2459
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? Thank You! #! /usr/bin/perl use warnings; use strict;
4
1426
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 | ? ?)\b By something like that : \b (25 |2 | ? ?)\.)\1\1 (25 |2 | ? ?)\b? I ask that in aim of understand backreferences thank you for answer me. (@^_^@)
8
3112
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 far from reality ??? Greetings, Uwe
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7729
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.