0

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?

2 Answers 2

1

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'
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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.