Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Backreferences in python ?

Reply
Thread Tools

Backreferences in python ?

 
 
Pankaj
Guest
Posts: n/a
 
      01-23-2006

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()

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Gerhard_H=E4ring?=
Guest
Posts: n/a
 
      01-23-2006
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
 
Reply With Quote
 
 
 
 
Pankaj
Guest
Posts: n/a
 
      01-23-2006
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??

 
Reply With Quote
 
Pankaj
Guest
Posts: n/a
 
      01-23-2006
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 ???

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      01-23-2006
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.
 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      01-23-2006
"Pankaj" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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++)
*/



 
Reply With Quote
 
Giovanni Bajo
Guest
Posts: n/a
 
      01-23-2006
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


 
Reply With Quote
 
Sion Arrowsmith
Guest
Posts: n/a
 
      01-24-2006
Pankaj <(E-Mail Removed)> 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 -- http://www.velocityreviews.com/forums/(E-Mail Removed) -- 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
 
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 Amy Dillavou Python 4 09-28-2005 09:03 PM
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