如何在特殊字符上创建智能拆分正则表达式



我正在努力创建一个特殊的正则表达式,它可以巧妙地标记文本。其中一些规则是:

  • 不要在"或"如果它们出现在两位数之间。例如:";12,3〃;或";12.3〃
  • 拆分除"之外的所有其他特殊字符">

下面是一个输入和我要查找的内容的示例:

输入:";J'aime编码器avec python 3.7,et 3.8。c‘est dûr de coder avec 3.9">

想要输出:["J'aime coder avec python 3.7" , ",", "et 3,8", ".", "c'est dûr de coder avec 3.9", "!"]

我试过这样的东西:

reg = re.compile(r"[^d+,d+]+[^d+.d+]+[^A-Za-z' ôöàçèéêëïâîÀÁÂÄÅÃÆÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝYüÜÚúÙùÛû]")

但它没有起作用。

Try(regex101(:

import re
s = """
J'aime coder avec python 3.7, et 3,8. c'est dûr de coder avec 3.9!
This is other phrase.And this is another."""
pat = re.compile(
r"(?:(.*?)s*([.,!?])s*(?=D|Z))|(?:(.*?D)s*([.,!?])s*(?=d|Z))"
)
out = [v for v in pat.split(s) if v]
print(out)

打印:

["J'aime coder avec python 3.7", ',', 'et 3,8', '.', "c'est dûr de coder avec 3.9", '!', 'This is other phrase', '.', 'And this is another', '.']

最新更新