Match everything delimited by another regex?

允我心安 提交于 2019-12-02 04:42:20

(Moved from your closed newer question)
In your case, the lookbehinds should come before the periods.
Condensing your expression, it is

Update - Between it you could just split discarding delimiters

 # (?:(?<!mr)(?<!mrs)\.|\?|!)+

 (?:
      (?<! mr )
      (?<! mrs )
      \.
   |  \?
   |  !
 )+

Or, split keeping delimiters

 # ((?:(?<!mr)(?<!mrs)\.|\?|!)+)

 (
      (?:
           (?<! mr )
           (?<! mrs )
           \.
        |  \?
        |  !
      )+
 )

What about this:

import re

pattern = r'(?=(?<!mr)\.|(?<!mrs)\.|\?|!)+' # I'm assuming this does what you say it does :)
text_block = """long block of sentences"""

sentences = re.split(pattern, text_block)

sentences will be a list containing the resulting substrings. re.split will split text_block up into different elements of the returned list. It splits at each point where pattern matches.

Read about re here:

https://docs.python.org/2/howto/regex.html

EDIT(data imported from your closed newer question):

If you are getting the symbols like ?, ! etc. captured into your returned list aswell, you should try removing the outer parens, like this:

re.split(r"\.(?<!mr)|\.(?<!mrs)|\?|!", somestring)

Ex:

sentences = [s for s in re.split(r"\.(?<!mr)|\.(?<!mrs)|\?|!", somestring) if s]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!