27

How can I replace strings in a file with using regular expressions in Python?

I want to open a file in which I should replace strings for other strings and we need to use regular expressions (search and replace). What would be some example of opening a file and using it with a search and replace method?

2
  • 2
    re.sub is function you want Commented Feb 28, 2016 at 20:54
  • 1
    @Arman I think you mean re.sub. Commented Feb 28, 2016 at 20:55

2 Answers 2

46
# The following code will search 'MM/DD/YYYY' (e.g. 11/30/2016 or NOV/30/2016, etc ),
# and replace with 'MM-DD-YYYY' in multi-line mode.
import re
with open ('input.txt', 'r' ) as f:
    content = f.read()
    content_new = re.sub('(\d{2}|[a-yA-Y]{3})\/(\d{2})\/(\d{4})', r'\1-\2-\3', content, flags = re.M)
Sign up to request clarification or add additional context in comments.

9 Comments

By 'mm' you mean 11, not NOV (which has 3 characters), right? But then you're using \w to match it as a character word instead of a \d digit like the others, so 'mm' would need to be matched as \d as well.
@lucidbrot: To the first one, with or without r, in this specific scenario, there is no difference. There is a thread, you can check it out: stackoverflow.com/questions/8157267/…
@Timo: Yes, of course.
uh.. does this do inline replacement? don't you have to write it somewhere?
@johnktejik: re.sub is not inline replacement, so additional steps are required if you'd like to save new content to a file.
|
1

Here is a general format. You can either use re.sub or re.match, based on your requirement. Below is a general pattern for opening a file and doing it:

import re

input_file = open("input.h", "r")
output_file = open("output.h.h", "w")
br = 0
ot = 0

for line in input_file:
    match_br = re.match(r'\s*#define .*_BR (0x[a-zA-Z_0-9]{8})', line) # Should be your regular expression
    match_ot = re.match(r'\s*#define (.*)_OT (0x[a-zA-Z_0-9]+)', line) # Second regular expression

if match_br:
    br = match_br.group(1)
    # Do something

elif match_ot:
    ot = match_ot.group(2)
    # Do your replacement

else:
    output_file.write(line)

3 Comments

Will not work with multiple line regular expressions.
thank you , I am just begginer in python and our project is to create script in Python which in line containing string xkcd (norvig.com/ipython/xkcd1313.ipynb) will replace bu.*ls with string [gikuj]..n|a.[alt]|[pivo].l|i..o|[jocy]e|sh|di|oo and also in line where are characters = a [ it will replace it with our name...so this is why i was asking this question because I am completely lost..
I think you should formulate your requirements clearly and start trying some example here to start with pymotw.com/2/re

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.