473,320 Members | 1,990 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,320 software developers and data experts.

Backreferences in python ?


I have something like below in perl and i am searching for equivalent
in python:

::: Perl :::
***********
while( <FILEHANDLE> )
{

line = $_;

pattern = "printf\( \"$lineNo \" \),";

line =~ s/"for(.*)\((*.)\;(.*)/for$1\($pattern$2\;$3/g;
}

This is used to

search for : for ( i = 0; i < 10; i++)
Replace with: for( printf( "10" ), i =0; i < 10; i++)
Where 10 is the line no.

****************************************
What i tried in python was::
****************************************

f = open( "./1.c", "r")
fNew = open( "./1_new.c", "w")
for l in f:
print l
lineno = lineno + 1
strToFind = "for\((.*)\;(.*)"

## For Converting int to string, i.e. line no. to string
lineNoClone = lineno

pattern = "printf(\"" + str( lineNoClone) + "\"),"

print pattern

strToReplace = "for\(" + pattern + "\1\;"

fNew.write( l.replace( strToFind, strToReplace) )

print l

fNew.close()

Jan 23 '06 #1
7 1567
Pankaj wrote:
[...]
****************************************
What i tried in python was::
****************************************

f = open( "./1.c", "r")
fNew = open( "./1_new.c", "w")
for l in f:
print l
lineno = lineno + 1
strToFind = "for\((.*)\;(.*)"
[...]


Regular expressions are not handled automatically in Python the way you
apparently think they are.

In Python, you will need to use the "re" module:

http://docs.python.org/lib/module-re.html

-- Gerhard
Jan 23 '06 #2
My tries have with re have not yielded results::
{
strToFind = 'for*;*'

## Converting int to string, i.e. line no. to string
lineNoClone = lineno

pattern = "printf(\"" + str( lineNoClone) + "\"),"

regObj = re.compile( strToFind)

m = regObj.search( l)
if ( m != None ) :
subStrPattern1_hasInitialization = "\1"
#m.group(1)

subStrPattern2_hasRestTillEnd = "\2"
#m.group(2)

strToReplace = "for(" + pattern +
subStrPattern1_hasInitialization + ";" + subStrPattern2_hasRestTillEnd
fNew.write( regObj.sub( strToFind, strToReplace ) )
else:
fNew.write( l)
}
Here problem is , i am not getting backreferences using \1 and \2

The string : for( i =0; i < 10; i++)

is getting replace by: for *;* {

I don't believe this, they have given that \1 and \2 store
backreferences, then where r they??

Jan 23 '06 #3
I got my answer

if ( m != None ) :
subStrPattern1_hasInitialization = m.group(1)
subStrPattern2_hasRestTillEnd = m.group(2)

str = subStrPattern1_hasInitialization +
subStrPattern2_hasRestTillEnd
strToReplace = "for(" + pattern + str
This gave me my solution

But to tell u, i have not got that, why i should concatenate and then
only place it . while i was trying the same thing by concatenation it
straight to replace string, it was not working

Any body has reasons ???

Jan 23 '06 #4
Pankaj wrote:
Here problem is , i am not getting backreferences using \1 and \2

You wrote: subStrPattern1_hasInitialization = "\1"


"\1" is the way to create a string containing a control-A character. What
you actually wanted was a string containing a backslash and a "1", so you
need either:

"\\1"

or

r"\1"

Try using the print statement to see what all those strings you are
creating actually contain.
Jan 23 '06 #5
"Pankaj" <pa********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

I have something like below in perl and i am searching for equivalent
in python:

::: Perl :::
***********
while( <FILEHANDLE> )
{

line = $_;

pattern = "printf\( \"$lineNo \" \),";

line =~ s/"for(.*)\((*.)\;(.*)/for$1\($pattern$2\;$3/g;
}

This is used to

search for : for ( i = 0; i < 10; i++)
Replace with: for( printf( "10" ), i =0; i < 10; i++)
Where 10 is the line no.


Here is a solution using pyparsing instead of re's. You're already used to
re's from using Perl, so you may be more comfortable using that tool in
Python as well. But pyparsing has some builtin features for pattern
matching, calling out to callback routines during parsing, and a lineno
function to report the current line number, all wrapped up in a simple
transformString method call.

Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul
from pyparsing import Keyword,SkipTo,lineno,cStyleComment

# define grammar for a for statement
for_ = Keyword("for")
forInitializer = SkipTo(';').setResultsName("initializer")
forStmt = for_ + "(" + forInitializer + ';'

# ignore silly comments
forStmt.ignore(cStyleComment)

# setup a parse action that will insert line numbers
# parse actions are all called with 3 args:
# - the original string being parsed
# - the current parse location where the match occurred
# - the matching tokens
# if a value is returned from this function, transformString will
# insert it in place of the original content
def insertPrintStatement(st,loc,toks):
lineNumber = lineno(loc,st)
if toks[0]:
return r'print("%d\n"), %s' % (lineNumber,toks[0])
else:
return r'print("%d\n")' % lineNumber
forInitializer.setParseAction(insertPrintStatement )

# transform some code
# this is how you would read in a whole file as a single string
#testdata = file(inputfilename).read()
# to read the entire file into a list of strings, do:
#testdata = file(inputfilename).readlines()
# for now, just fake some source code
testData = """
for(i = 0; i <= 100; ++i)
{
/* some stuff */
}

for (;;;)
{
/* do this forever */
}

/* this for has been commented out
for(a = -1; a < 0; a++)
*/

"""

# use the grammar and the associated parse action to
# transform the source code
print forStmt.transformString(testData)

--------------------------
Gives:
for(print("2\n"), i = 0; i <= 100; ++i)
{
/* some stuff */
}

for(print("7\n");;;)
{
/* do this forever */
}

/* this for has been commented out
for(a = -1; a < 0; a++)
*/

Jan 23 '06 #6
Pankaj wrote:
Perl :::

***********
while( <FILEHANDLE> )
{

line = $_;

pattern = "printf\( \"$lineNo \" \),";

line =~ s/"for(.*)\((*.)\;(.*)/for$1\($pattern$2\;$3/g;
}

This is used to

search for : for ( i = 0; i < 10; i++)
Replace with: for( printf( "10" ), i =0; i < 10; i++)
Where 10 is the line no.

import re
import fileinput

for L in fileinput.input(inplace=True):
pattern = 'printf("%d"),' % input.filelineno()
L = re.sub(r"for(.*)\((*.)\;(.*)", r"for\1\(%s\2;\3" % pattern, L)
print L,

or something
--
Giovanni Bajo
Jan 23 '06 #7
Pankaj <pa********@gmail.com> wrote:
search for : for ( i = 0; i < 10; i++)
Replace with: for( printf( "10" ), i =0; i < 10; i++)
Where 10 is the line no. f = open( "./1.c", "r")
fNew = open( "./1_new.c", "w")
for l in f:
print l
lineno = lineno + 1
strToFind = "for\((.*)\;(.*)" [etc.]search for : for ( i = 0; i < 10; i++)
Replace with: for( printf( "10" ), i =0; i < 10; i++)
Ah, the dangers of thinking of all string manipulation as requiring
regexps, thanks to their ubiquity in Perl. Observe:search for : for ( i = 0; i < 10; i++)
Replace with: for( printf( "10" ), i = 0; i < 10; i++)


All you need is:
strToFind = "for ("
strToReplace = 'for (printf( "+str(lineno)+'" ),'
# Note the use of '' to avoid the need to escape the "s
fNew.write(l.replace(strToFind, strToReplace)

(OK, maybe you do need the regexp if you've got any "for (;" loops,
or inconsitencies as to whether it's "for(" or "for (". But if a
simple string replace will do the job, use it.)

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Jan 24 '06 #8

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: Amy Dillavou | last post by:
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...
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...
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. ...
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 | ?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.