Note that there are some explanatory texts on larger screens.

plurals
  1. POCan this regex be further optimized?
    text
    copied!<p>I wrote this regex to parse entries from srt files.</p> <pre><code>(?s)^\d++\s{1,2}(.{12}) --&gt; (.{12})\s{1,2}(.+)\r?$ </code></pre> <p>I don't know if it matters, but this is done using Scala programming language (Java Engine, but literal strings so that I don't have to double the backslashes).</p> <p>The <code>s{1,2}</code> is used because some files will only have line breaks <code>\n</code> and others will have line breaks and carriage returns <code>\n\r</code> The first <code>(?s)</code> enables <code>DOTALL</code> mode so that the third capturing group can also match line breaks.</p> <p>My program basically breaks a srt file using <code>\n\r?\n</code> as a delimiter and use Scala nice pattern matching feature to read each entry for further processing:</p> <pre><code>val EntryRegex = """(?s)^\d++\s{1,2}(.{12}) --&gt; (.{12})\s{1,2}(.+)\r?$""".r def apply(string: String): Entry = string match { case EntryRegex(start, end, text) =&gt; Entry(0, timeFormat.parse(start), timeFormat.parse(end), text); } </code></pre> <p>Sample entries:</p> <p>One line:</p> <pre><code>1073 01:46:43,024 --&gt; 01:46:45,015 I am your father. </code></pre> <p>Two Lines: </p> <pre><code>160 00:20:16,400 --&gt; 00:20:19,312 &lt;i&gt;Help me, Obi-Wan Kenobi. You're my only hope.&lt;/i&gt; </code></pre> <p>The thing is, the profiler shows me that this parsing method is by far the most time consuming operation in my application (which does intensive time math and can even reencode the file several times faster than what it takes to read and parse the entries).</p> <p>So any regex wizards can help me optimize it? Or maybe I should sacrifice regex / pattern matching succinctness and try an old school <code>java.util.Scanner</code> approach?</p> <p>Cheers,</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