Python: match a long string with special characters and white spaces and then prepend two characters to the beginning

StackOverflow https://stackoverflow.com/questions/9416381

Question

I'm not sure how to approach this, i'm trying to match this long string in a text file that has lots of whitespace and special characters and append the characters to the front ie. "//"

i need to match this line:

$menu_items['gojo_project']                    => array('http://www.gojo.net/community/plugin-inventory/ops-gojo/gojo', 'gojo',3),

and turn it into this:

//$menu_items['gojo_project']                    => array('http://www.gojo.net/community/plugin-inventory/ops-gojo/gojo', 'gojo',3),

notice i just prepended two '/' character.

I tried using re.escape to format the string, but its just really long and still throw sytax error. Am i going about this the right way using 're' ? or is there a better pythonic way to match a string like this one in a text file and prepend to it?

Edit: Forgot to mention that i need to edit the file in-line. In short, its a long php script that i'm trying to find that line and comment it out (ie. //). So, I cant really use some of the proposed solutions(i think) since they have it writing the modification to a separate file.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top