magicsword
magicsword

Reputation: 1289

regex for substring on a string then replace text

I have a text file that I'm reading as:

with open(file,'r+') as f:
   file_data = f.read()

the file_data is a long string that has the following text:

'''This file starts here dig_1 = hello\n doge ras = friend\n sox = pie\n'''

I want to search for dig_1 then get all the text after the '=' up to the new line character \n and replace it with a different text so that it is dig_1 = hello\n is now dig_1 = unknown and do the same with the others (ras = friend\n to ras = unknown and sox = pie\n to sox = unknown). Is there an easy way to do this using regex?

Upvotes: 0

Views: 86

Answers (2)

imdevskp
imdevskp

Reputation: 2223

You can make use sub function of python's re module
The pattern that you want to replace looks something like a word followed by an equal sign and space and also has a preceding newline

# import re module
import re

# original text
txt = '''This file starts here dig_1 = hello\n doge ras = friend\n sox = pie\n''' 

# pattern to look for
pattern = '= (.*?\n)'
# string to replace with
repl = 'unknown'

# replace 'pattern' with the string inside 'repl' in the string 'txt'
re.sub(pattern, repl, txt)
'This file starts here dig_1 unknown doge ras unknown sox unknown'

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521289

You may use re.sub here:

inp = "This file starts here dig_1 = hello\n doge ras = friend\n sox = pie\n"
output = re.sub(r'\b(\S+)\s*=\s*.*(?=\n|$)', r'\1 = unknown', inp)
print(output)

This prints:

This file starts here dig_1 = unknown
 doge ras = unknown
 sox = unknown

Upvotes: 0

Related Questions