[英]Python regex replace with return match
Given the following string给定以下字符串
p='12.04.2020 - 17:00 - 13.04.2020 10:00'
How do I replace the first dash (-), which is a mistake, with an empty space?如何用空格替换第一个破折号 (-),这是错误的?
I tried with我试过了
re.sub("(20\d*) - (\d*):","\1 \2:",p)
'12.04.\x01 \x02:00 - 13.04.2020 10:00'
but it doesn't return the matches.但它不返回匹配项。
Edit: there may be multiple such patterns and there may not be a dash in that position, so it has to be specific, I cannot just replace the first found dash.编辑:可能有多种这样的模式,position 中可能没有破折号,所以它必须具体,我不能只替换第一个找到的破折号。
You need to use r
modifier to prevent escaping \d to d and \1 to 1您需要使用
r
修饰符来防止 escaping \d 到 d 和 \1 到 1
re.sub(r"(20\d*) - (\d*):",r"\1 \2:",p)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.