0

I have a text txt = 'The fat \m{cat sat} on \m{the} mat.' which I hope to output 'The fat cat sat on the mat.'

I have tried the following two ways:

re.sub(r'\\m\{(.*)\}', '', txt) 
# output: 'The fat  mat.'

re.sub(r'\\m\{(?=.*)\}', '', txt) 
# output: 'The fat \\m{cat sat} on \\m{the} mat.'

Why is that and how should I do?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Tengerye
  • 1,796
  • 1
  • 23
  • 46

2 Answers2

2

You can modify your own regex a bit to make it work

  • Use backreference to replace value instead of just empty string
  • Also make you regex lazy i.e (.*) -> (.*?) or ([^}]*)

import re
txt = 'The fat \m{cat sat} on \m{the} mat.';
r = re.sub(r'\\m\{(.*?)\}', "\g<1>", txt);
print(r);      

//The fat cat sat on the mat.

Note:- you can use r"\1" or "\\1" instead of \g<1> to back reference the captured group

Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

Maybe this expression

\\m{|}

replaced with an empty string might work.

Test

import re

print(re.sub(r"\\m{|}", '', 'The fat \m{cat sat} on \m{the} mat.'))

Output

The fat cat sat on the mat.
Emma
  • 27,428
  • 11
  • 44
  • 69