如果句子以句号“。”结尾,如何编写与模式匹配的正则表达式

时间:2018-08-09 05:38:12

标签: python regex

我有一组字符串,如下所示:

a phrase containing spaces
A sentence contains spaces as well, but end by period.

我想找到一个正则表达式来匹配第二行中的空格(例如[\ t \ f]),以'。'结尾。

我环顾四周,没有找到解决方法。所以我来这里寻求帮助。

我正在使用Python,但是即使Python不可能也不知道pcre解决方案。

我找到了一些正则表达式,但不能排除第一行。

my regex

1 个答案:

答案 0 :(得分:0)

这里是一个正则表达式模式,如果将其重复应用于每行,则假设行以句点结尾,应该能够匹配该行中的空格:

\s+(?=.*\.$)

Demo

这是我尝试使用Python脚本的尝试。找到匹配项后,我不会打印空格,因为我们看不到它。相反,我打印一些可见的东西:

input = 'A sentence contains spaces as well, but end by period.'
spaces = re.findall(r'\s+(?=.*\.$)', input)

for space in spaces:
    print('found a space')

found a space (printed 9 times)