views:

909

answers:

4
SSViewer::set_theme('bullsorbit');

this my string. I want search in string "SSViewer::set_theme('bullsorbit'); " and replace 'bullsorbit' with another string. 'bullsorbit' string is dynamically changing.

+3  A: 

Not in a situation to be able to test this so you may need to fiddle with the Regular Expression (they may be errors in it.)

import re
re.sub("SSViewer::set_theme\('[a-z]+'\)", "SSViewer::set_theme('whatever')", my_string)

Is this what you want?

Just tested it, this is some sample output:

my_string = """Some file with some other junk
SSViewer::set_theme('bullsorbit');
SSViewer::set_theme('another');
Something else"""

import re
replaced = re.sub("SSViewer::set_theme\('[a-z]+'\)", "SSViewer::set_theme('whatever')", my_string)
print replaced

produces:

Some file with some other junk
SSViewer::set_theme('whatever');
SSViewer::set_theme('whatever');
Something else

if you want to do it to a file:

my_string = open('myfile', 'r').read()
kjfletch
thank u for reply .. it is file .. i want read file and search for that string and replace what ever
rajaneesh
That will replace everything with `my_string`. Check this by running it in the interactive console
AutomatedTester
import refile_read = open("/var/www/rajaneesh/mysite/_config.php", "r")contents = file_read.read()file_read.close()this my codei want search that string "SSViewer::set_theme\('whatever')" file and replace that string with new string
rajaneesh
You want to find 'bullsorbit' from the pattern "SSViewer::set_theme('bullsorbit');" and replace all occurrences of 'bullsorbit' thereafter with another string? Is that what you are asking?
kjfletch
so nice .. man thanks a lot .......
rajaneesh
Be clear that this only works for lower case [a-z] If you want it to catch other characters you will need to tweak the regular expression.
kjfletch
+1  A: 
>> my_string = "SSViewer::set_theme('bullsorbit');"
>>> import re
>>> change = re.findall(r"SSViewer::set_theme\('(\w*)'\);",my_string)
>>> my_string.replace(change[0],"blah")
"SSViewer::set_theme('blah');"

its not elegant but it works. the findall will return a dictionary of items that are inside the ('') and then replaces them. If you can get sub to work then that may look nicer but this will definitely work

AutomatedTester
thanks..... it works beautifully ..
rajaneesh
Consider this: "SSViewer::set_theme('theme')" would be replaced with "SSViewer::set_blah('blah')"
kjfletch
A: 
st = "SSViewer::set_theme('"
for line in open("file.txt"):
    line=line.strip()
    if st in line:
        a = line[ :line.index(st)+len(st)]
        b = line [line.index(st)+len(st): ]
        i = b.index("')")
        b = b[i:]
        print a + "newword" + b
ghostdog74
A: 

while your explanation is not entirely clear, I think you might make some use of the following:

open(fname).read().replace('bullsorbit', 'new_string')
SilentGhost
i thought he meant bullsorbit is changing dynamically, so the above might not be what he needs.
ghostdog74
what exactly *changing dynamically* means? run it with another string, `'bullsorbit2'`.
SilentGhost