【问题标题】:split text but keep the delimiter at end of sentence with regex拆分文本,但使用正则表达式将分隔符保留在句末
【发布时间】:2021-03-16 20:51:08
【问题描述】:

我有一个类似"Hi.My name is jeff!How are you?fine" 的文本。

我想根据.? 使用正则表达式拆分此文本以获得如下输出:

['Hi.', 'My name is jeff!How are you?', 'fine']

我尝试过$\Z,但没有成功。

【问题讨论】:

    标签: python python-3.x regex


    【解决方案1】:

    使用

    import re
    string = "Hi.My name is jeff!How are you?fine"
    print(re.split(r"(?<=[.?])", string))
    

    Python proof。另请参阅regex proof

    解释

    --------------------------------------------------------------------------------
      (?<=                     look behind to see if there is:
    --------------------------------------------------------------------------------
        [.?]                     any character of: '.', '?'
    --------------------------------------------------------------------------------
      )                        end of look-behind
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-24
      • 2012-06-29
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      相关资源
      最近更新 更多