Search code examples
pythonregexsentence

Match everything delimited by another regex?


I'm currently trying to make a regex that will find all the sentences in a block of text, and so far I've got this;

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

Which will find everything that delimits a sentence. I want the regex to find everything that's contained between what this regex finds, but I don't really know where to go from here.


Solution

  • (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 )
               \.
            |  \?
            |  !
          )+
     )