2

I have a test.txt file with following data:

jon doe smith 
\jon\-\1\ \jon\-\2\ \jon\-\3\ \doe\- \1\ \doe\-\2\ 
\doe\-\3\ \smith\- \1\ \smith\-\2\ \smith\-\3\

what i've been trying to get here is to remove the contents mentioned in a list "name" mentioned below code from the txt file.

with open(r'test.txt', 'r') as file:
    data = file.read()
print(data)
name=['jon','doe']

for e in name:
   new_string=data.replace('\\' + e + '\\-\\1\\',' ').replace('\\' + e + '\\-\\2\\',' ').replace('\\' + e + '\\-\\3\\',' ').replace('\\' + e + '\\',' ')

print(new_string)

the output of which this code presents:-

jon doe smith 
\jon\-\1\ \jon\-\2\ \jon\-\3\ \doe\- \1\ \doe\-\2\
\doe\-\3\ \smith\- \1\ \smith\-\2\ \smith\-\3\
jon doe smith
\jon\-\1\ \jon\-\2\ \jon\-\3\  - \1\
  \smith\- \1\ \smith\-\2\ \smith\-\3\

the output which i need:-

jon doe smith 
\jon\-\1\ \jon\-\2\ \jon\-\3\ \doe\- \1\ \doe\-\2\
\doe\-\3\ \smith\- \1\ \smith\-\2\ \smith\-\3\
         smith

           \smith\- \1\ \smith\-\2\ \smith\-\3\

Also Is there a way where instead of replace, regex can be used for this ?

matru878
  • 33
  • 4

1 Answers1

1

Try:

import re
pattern = r"\\?(\w+)\\?(- ?[\\\d]*)?"
res = re.sub(pattern, lambda x: x[0] if x[1] not in name else ' ', x)
hussic
  • 1,816
  • 9
  • 10
  • While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive-feedback/upvotes from users, when the code is explained. – Amit Verma Feb 07 '21 at 18:09
  • Sorry but English is not my language. re.sub permit to use a function to define the replace argument and here is used to test if the text captured is in the name set. – hussic Feb 09 '21 at 00:56