Note that there are some explanatory texts on larger screens.

plurals
  1. POYet Another MultiLine Group Regex Query
    text
    copied!<p>So I'm trying to write a regex to filter text files for specific snippets (looking for speed rather than brute force).</p> <p>I want to get three things from each match:</p> <ul> <li>a <strong>key</strong> to identify a snippet</li> <li>an optional <strong>language</strong> to format each snippet</li> <li>the <strong>value</strong> which represents all rows between the start and end tags</li> </ul> <p>This is the regex (a failing test harness can be found here - <a href="https://gist.github.com/shiftkey/5236161" rel="nofollow">https://gist.github.com/shiftkey/5236161</a>):</p> <p><code>(?s)code start (?&lt;key&gt;[A-Za-z-]*) (?&lt;language&gt;[A-Za-z]*).*[\n](?&lt;value&gt;.*?)[\n].*end code (?&lt;key&gt;[A-Za-z-]*)</code></p> <p>Using these options: <code>RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Multiline</code>.</p> <p>So a file like this:</p> <pre><code>// code start foo csharp var x = 1; // end code foo </code></pre> <p>Gives me the expected values:</p> <ul> <li><strong>key</strong>: foo</li> <li><strong>language</strong>: csharp</li> <li><strong>value</strong>: <code>var x = 1;</code></li> </ul> <p>But when I introduce a snippet with a second row I only get the last row:</p> <pre><code>// code start foo csharp var x = 1; var y = 2; // end code foo </code></pre> <ul> <li><strong>key</strong>: foo</li> <li><strong>language</strong>: csharp</li> <li><strong>value</strong>: <code>var y = 2;</code></li> </ul> <p>Instead of </p> <ul> <li><strong>value</strong>: <code>var x = 1;\r\nvar y = 2;</code> (or similar)</li> </ul> <p>What have I missed with getting groups to cover multiple lines? </p> <p>I've looked at similar scenarios raised on SO but they haven't worked for my usage.</p>
 

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