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
  • 15,737
  • 15
  • 35
  • 52
Tengerye
  • 1,406
  • 1
  • 19
  • 36

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
  • 35,187
  • 4
  • 31
  • 54
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
  • 26,487
  • 10
  • 35
  • 65