1,105,427 Community Members

RE Search and Replace problem

Member Avatar
mesuge6666
Newbie Poster
1 post since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have some random text like:

>>>text= \
"""import sys  

n = int(sys.argv[1]) ;;print "blbalbalbalabllalbaa"

replace me i in range(1,n) {print "numbers:";;replace me j in range(1,3) {print j} ;;print i ;;replace me k in range(1,3) {print k} ;;print "end!"}

replace me n < 5 {print n ;;print "youragenious"}   
"""

i'd like to change all ";;" to ";;____" or ";;fourrandomthings" or whatever but only inside of {...}
i tried:

>>> p=re.compile(' {([^;]*);;([^;]*)}')
>>> print p.sub(r' {\1;;    \2}',text)
import sys  

n = int(sys.argv[1]) ;;print "blbalbalbalabllalbaa"

replace me i in range(1,n) {print "numbers:";;    replace me j in range(1,3) {print j} ;;print i ;;replace me k in range(1,3) {print k} ;;    print "end!"}

replace me n < 5 {print n ;;    print "youragenious"}

it's still not what i need...please help

Member Avatar
griswolf
Veteran Poster
1,144 posts since Apr 2010
Reputation Points: 303 [?]
Q&As Helped to Solve: 264 [?]
Skill Endorsements: 3 [?]
 
0
 

What you show isn't python, I think. I think you should start with a hello world python program, in a text file, then extend it one step at a time until you get where you need to go. Working in the python line editing environment is fine for a quick little test, but you have to re-enter everything every time you need a change, so the text script file is much better for programming.

Hint: Python string with \1 or \2 in it should be spelled r'\2' for instance (note the leading r) ... or you need to escape the backslash: '\\2'

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: