Note that there are some explanatory texts on larger screens.

plurals
  1. PORegex to match all sentences with quotes in them
    text
    copied!<p>I am trying to match all sentences that contain quotes, independent of the length of the quote or the number of sentences within the quote.</p> <p>As Alfe point out, getting a perfect regex is maybe not viable, but I would like to improve the one I am using if possible.</p> <p>Right now I am doing this to find the quote:</p> <pre><code>def split_by_quotes(text): pattern = r'([A-Z].*?\".*?\".*?\.)' quotes = re.findall(pattern, text) return(quotes) </code></pre> <p>But I want to make sure that the quote is appearing in a sentence and then capture that entire sentence.</p> <p>By a sentence I mean a piece of text that:</p> <ol> <li>is usually preceded by a space</li> <li>starts with an upper case character or a quote</li> <li>ends with either a . or ! or ? or (sometimes directly followed by a " or ')</li> <li>is usually followed by a space</li> </ol> <p>This won't capture all sentences as Alfe points out, but it would be good enough if I could match on those conditions.</p> <p>For example:</p> <blockquote> <p>"This is a quote, it should be matched"</p> <p>This is text without a quote on a new line after multiple carriage returns, it should not be matched.</p> </blockquote> <p>More complex example:</p> <blockquote> <p>Charles Babbage said: "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."</p> </blockquote> <p>That whole sentence would match.</p> <p>But,</p> <blockquote> <p>They called Garfield Minus Garfield and Lolcats, but when Johnson saw what he considered to be a particularly hilarious clip of someone falling down and then being "played off stage" by a cat with a keyboard, his friends thought it was lame. "I said, this is going to be big.", he says, "My friends were like, 'Nah, it's just a cat.'"</p> </blockquote> <p>Would be matched as follows:</p> <blockquote> <p>They called Garfield Minus Garfield and Lolcats, but when Johnson saw what he considered to be a particularly hilarious clip of someone falling down and then being "played off stage" by a cat with a keyboard, his friends thought it was lame.</p> </blockquote> <p>and</p> <blockquote> <p>"I said, this is going to be big.", he says, "My friends were like, 'Nah, it's just a cat.'"</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload