Note that there are some explanatory texts on larger screens.

plurals
  1. POMatch overlapping patterns with capture using a MATLAB regular expression
    text
    copied!<p>I'm trying to parse a log file that looks like this:</p> <pre><code>%%%% 09-May-2009 04:10:29 % Starting foo this is stuff to ignore %%%% 09-May-2009 04:10:50 % Starting bar more stuff to ignore %%%% 09-May-2009 04:11:29 ... </code></pre> <p>This excerpt contains two time periods I'd like to extract, from the first delimiter to the second, and from the second to the third. I'd like to use a regular expression to extract the start and stop times for each of these intervals. This mostly works:</p> <pre><code>p = '%{4} (?&lt;start&gt;.*?)\n% Starting (?&lt;name&gt;.*?)\n.*?%{4} (?&lt;stop&gt;.*?)\n'; times = regexp(c,p,'names'); </code></pre> <p>Returning:</p> <pre><code>times = 1x16 struct array with fields: start name stop </code></pre> <p>The problem is that this only captures every other period, since the second delimiter is consumed as part of the first match.</p> <p>In other languages, you can use lookaround operators (lookahead, lookbehind) to solve this problem. The <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-42649.html" rel="nofollow noreferrer">documentation on regular expressions</a> explains how these work in MATLAB, but I haven't been able to get these to work while still capturing the matches. That is, I not only need to be able to match every delimiter, but also I need to extract part of that match (the timestamp).</p> <p>Is this possible?</p> <p>P.S. I realize I can solve this problem by writing a simple state machine or by matching on the delimiters and post-processing, if there's no way to get this to work.</p> <p><strong>Update:</strong> Thanks for the workaround ideas, everyone. I heard from the developer and there's currently no way to do this with the regular expression engine in MATLAB.</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