So I have a big file of 3000 lines. I need to find the first occurence of $SETGLOBAL and I need to change the word after the first occurence of this word. To do this I use the following regex expression
with open("textfile.txt","r") as F:
FF=F.read()
FF=re.sub("\$SETGLOBAL\s(.*)", FF ,"CCCC",1)
F2 = open("textfile.txt","w").write(FF)
The problem is that in order to change the text in my huge file I also need regex to capture everything before and after this occurrence. So I can write the new text file with the changed word in it.
How would I do this?
My problem is that I need the entire file in my variable FF. so I can write it to a new file.
Imagine I have for example the following file :
123456
$SETGLOBAL AAAA
BBBBBB
$SETGLOBAL TTTT
What I need is a new file as following
123456
$SETGLOBAL CCCC
BBBBBB
$SETGLOBAL TTTT
But my solution overwrites everything and I am left with only
$SETGLOBAL CCCC
in my new file