我想在缩写后面添加逗号和空格,
,缩写的定义是单个或多个字母后跟一个圆点,后跟一个或多个字母重复两次或更多次.例如,这些被认为是缩写A.b.C.
a.b.
ab.cd.
ab.cde.
ab.cd.ef.gh.
,而不是缩写a.b
或A. B
我不想加逗号:
- 如果缩写的最后一点是给定文本的结尾,
- 如果缩写后面有可选的空格和大写字母,或者
- 如果缩写后面有可选的空格和另一个标点符号.
给出以下测试句子:
test_str = """This is an example e.g. sentence and this is with i.e. text and two abbreviations S.T.R. and K.LM.NO.P. as example with acronym.
but in here it shouldn't catch it because after that there is space and dot g.k. . Also here it shouldn't detect because the next sentence starts with capital A.BC.D.
And this is a normal sentence. Followed by another normal sentence. This contains only one letter A. and is not abbreviation.
This shouldn't match i.e., since it contains already a comma. I like to read books such as e.g. book 1 or i.e. book2.
A.B.C. is an abbreviation that should match. A.B.! is an abbreviation that shouldn't match because it has ! after the abbreviation.
A.B.? is an abbreviation that shouldn't match because it has ? after the abbreviation.
A.B. ; is an abbreviation that shouldn't match because it has a space and ; after the abbreviation.
a.b.c.d. is an abbreviation that should match.
a.b.c., is an abbreviation that shouldn't match because it already has a comma. A.B is not an abbreviation because it contains only one dot.
Another abbreviation that should not match j.j.L.o.U.h."""
我希望输出如下:
output_text = """This is an example e.g., sentence and this is with i.e., text and two abbreviations S.T.R., and K.LM.NO.P., as example with acronym.
but in here it shouldn't catch it because after that there is space and dot g.k. . Also here it shouldn't detect because the next sentence starts with capital A.BC.D.
And this is a normal sentence. Followed by another normal sentence. This contains only one letter A. and is not abbreviation.
This shouldn't match i.e., since it contains already a comma. I like to read books such as e.g., book 1 or i.e., book2.
A.B.C., is an abbreviation that should match. A.B.! is an abbreviation that shouldn't match because it has ! after the abbreviation.
A.B.? is an abbreviation that shouldn't match because it has ? after the abbreviation.
A.B. ; is an abbreviation that shouldn't match because it has a space and ; after the abbreviation.
a.b.c.d., is an abbreviation that should match.
a.b.c., is an abbreviation that shouldn't match because it already has a comma. A.B is not an abbreviation because it contains only one dot.
Another abbreviation that should not match j.j.L.o.U.h."""
我现在使用的是以下内容:
regex = r"(\b(?:[A-Za-z]\.){2,}(?!\s*[,.;?!-]))"
但它会产生以下输出:
This is an example e.g., sentence and this is with i.e., text and two abbreviations S.T.R., and K.LM.NO.P. as example with acronym. but in here it shouldn't catch it because after that there is space and dot g.k. . Also here it shouldn't detect because the next sentence starts with capital A.BC.D. And this is a normal sentence. Followed by another normal sentence. This contains only one letter A. and is not abbreviation. This shouldn't match i.e., since it contains already a comma. I like to read books such as e.g., book 1 or i.e., book2. A.B.C., is an abbreviation that should match. A.B.! is an abbreviation that shouldn't match because it has ! after the abbreviation. A.B.? is an abbreviation that shouldn't match because it has ? after the abbreviation. A.B. ; is an abbreviation that shouldn't match because it has a space and ; after the abbreviation. a.b.c.d., is an abbreviation that should match. a.b., c., is an abbreviation that shouldn't match because it already has a comma. A.B is not an abbreviation because it contains only one dot.
Another abbreviation that should not match j.j.L.o.U.h.,
我的正则表达式失败的 case 以粗体显示.它们应该是K.LM.NO.P.,
、a.b.c.,
和j.j.L.o.U.h.
,因为第一个应该被检测为缩写,第二个应该在最后一个点之后包含一个标点符号,最后一个是给定文本的结尾.
有没有办法实现这一点?如有任何帮助,不胜感激!