Consider a basic regex like a(.+?)a. How can one replace all occurences of that regex in a file with the content of the first group?
Consider a basic regex like a(.+?)a . How can one replace all occurences of
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use can use the
remodule to use regular expressions in python and thefileinputmodule to simply replace text in files in-placeExample:
Before:
After:
Note: this works because the argument
inplace=Truecauses input file to be moved to a backup file and standard output is directed to the input file, as documented under Optional in-place filtering.