Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Assuming there aren't any newlines inside a block</strong> and the <code>BEGIN</code>/<code>END</code> statements are the first non-space of their line, I'd write the regex like this (Perl notation; change the delimiters and remove comments, whitespaces and the <code>/x</code> modifier if you use a different engine)</p> <pre><code>m{ \n \s* BEGIN \s+ \[ # match the beginning ( (?!\n\s*\n) .)*? # match anything that isn't an empty line # checking with a negative look-ahead (?!PATTERN) \n \s* END \s+ ID=X_[^\]]* \] # the ID may not contain "]" }sx # /x: use extended syntax, /s: "." matches newlines </code></pre> <p><strong>If the content may be anything</strong>, it might be best to create a list of all blocks, and then grep through them. This regex matches any block:</p> <pre><code>m{ ( BEGIN \s+ \[ .*? # non-greedy matching is important here END \s+ ID=[^\]]* \] # greedy matching is safe here ) }xs </code></pre> <p>(add newlines if wanted)</p> <p>Then only keep those matches that match this regex:</p> <pre><code>/ID = X_[^\]]* \] $/x # anchor at end of line </code></pre> <p>If we don't do this, <strong>backtracking may prevent a correct match</strong> (<code>[\s\S]*?</code> can contain <code>END ID=X_</code>). Your regex would put anything inside the blocks until it sees a <code>X_.*</code>.</p> <p>So using <code>BEGIN\s+\[([/s/S]*?)END\s+ID=(.*?)\]</code> — note the extra question mark — one match would be:</p> <pre><code>BEGIN [ text b text c END ID=Y_1] text aaa text bbb BEGIN [ text d text e END ID=X_2] </code></pre> <p>…instead of failing at the <code>Y_</code>. A greedy match (your unchanged regex) should result in the whole file being matched: Your <code>(.*)</code> eats up all characters (until the end of file) and then goes back until it finds a <code>]</code>.</p> <h3>EDIT:</h3> <p>Should you be using perls regex engine, we can use the <code>(*FAIL)</code> verb:</p> <pre><code>/BEGIN\s+\[(.*?)END\s+ID=(X_[^\]]*|(*FAIL))\]/s </code></pre> <p>"Either have an ID starting with <code>X_</code> or the match fails". However, this does not solve the problem with <code>END ID=X_1]</code>-like statements inside your data.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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