[英]Python re.sub() with dots
I would like to put a space before and after a dot in text but only if its not a part of a date. 我想在文本中的点之前和之后放置一个空格,但前提是它不是日期的一部分。 So far I have this, and i figured out I have to do something with \\D.
到目前为止,我已经有了这个,而且我发现我必须对\\ D做一些事情。 but it gives back the letter before the dot not only the dot:
但是它不仅会返回点,还会返回点前的字母:
string = re.sub("\.", " . ", string)
For example: 例如:
Input text: 1992.01.04 is my birthday. 输入文本:1992.01.04是我的生日。
Required output: 1992.01.04 is my birthday . 要求的输出:1992.01.04是我的生日。
there is a space before the end of string dot. 字符串点的末尾有一个空格。
Other question is the same with colon and time, 其他问题与冒号和时间相同,
Input text: The time is 11:48, reported by: Tom. 输入文字:时间为11:48,由Tom报道。
Required output: The time is 11:48, reported by : Tom. 必需的输出:时间是11:48,由Tom报道。
there is a space after text 'reported by' before the colon 冒号前的“报告人”后面有一个空格
Thanks in advance 提前致谢
You can use this regex which does a negative look ahead and negative look behind to check if dot/colon is surrounded by digits and replace it with ' \\1 ' 您可以使用此正则表达式对前面的结果进行否定检查,然后在后面的结果进行否定检查,以检查点/冒号是否被数字包围,并用'\\ 1'代替
(?<!\d\d)([.:])(?!\d+)
Demo, https://regex101.com/r/hr6slz/4 演示, https://regex101.com/r/hr6slz/4
This regex works for both colon and dot and as you can replace it by ' \\1 ' 此正则表达式适用于冒号和点,您可以将其替换为'\\ 1'
You need _ positive lookbehind assertion._ (or negative, with \\d
). 您需要_肯定断言后面的_。(或使用
\\d
否定的)。 Look into https://docs.python.org/3/library/re.html for details. 查看https://docs.python.org/3/library/re.html了解详细信息。
re.sub("(?<=\D)\.(\D?)", " . ", '1992.01.04 is my birthday.')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.