Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this:</p> <pre><code>$pattern='~\A(.+?) {2,}(.+?)\R{2,}(.+?)\R{2,}(.+?)(?:\R{2,}|\Z)~s'; preg_match($pattern, $subject, $match); </code></pre> <p><strong><a href="http://ideone.com/oSQDx" rel="nofollow">See it in action on ideone.com</strong></a></p> <p>I'm assuming all those <code>&amp;nbsp;</code>'s in your sample text represent regular spaces, and you only used them so we could see that there was more than one space. If you been using SO's code formatting from the beginning, that wouldn't have been necessary. That's the <em>indentation</em> style of formatting; in text formatted with backticks, whitespace still gets collapsed.</p> <p>I'm also assuming you're reading the whole file into memory, not processing it line-by-line. The regex is pretty straightforward. Starting at the beginning of the text (<code>\A</code>), it reluctantly matches and captures everything it sees (<code>(.+?)</code>, in single-line mode) until it sees two or more consecutive spaces (<code>{2,}</code>).</p> <p>After that, it reluctantly matches and captures until it sees two or more newlines in a row (<code>(.+?)\R{2,}</code>). Then it does the same thing twice more to capture the second and third paragraphs. The final <code>(?:\R{2,}|\Z)</code> is there in case there's no more text after the third paragraph.</p> <p><code>\R</code>, if you're not familiar with it, is a shorthand for any kind of line separator: <code>\n</code>, <code>\r</code>, <code>\r\n</code> and a few other, less common ones. It's supported by Perl, PHP (PCRE), Ruby 1.9+ (Oniguruma) and a few other flavors, but not (so far) by JavaScript, Python, Java or .NET.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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