Why look ahead is returning matches for time-stamp
<p>Trying to write a script in python for some post processing. I have a file that contains messages with a time-stamp. I want to extract all the messages into a list.<br> 
Regex - start from message until next time-stamp. </p>

<pre><code>findallItems = re.findall(r'(?s)((?&lt;=message).*?(?=((\d{4})\-((0[1-9])|(1[0-2]))\-((0[1-9])|(1[0-2]))|\Z)))', fileread)
</code></pre>

<p>This works fine but it also returns time-stamps as matches. How can I only return the message and not include time-stamps ?</p>

<p>If I use look ahead position as text then it works fine. For e.g </p>

<pre><code>findallItems = re.findall(r'(?s)((?&lt;=message).*?(?=message|\Z))',fileread)
</code></pre>

<p>You need to remove unnecessary capturing parentheses and convert others to non-capturing:</p>

<pre><code>findallItems = re.findall(r'(?s)(?&lt;=message).*?(?=(?:\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-2])|\Z))', fileread)
</code></pre>

<p>See <a href="https://regex101.com/r/yE7iF1/1" rel="nofollow">this regex demo</a></p>

<p>However, you may just keep 1 capturing group over your necessary pattern and <code>re.findall</code> will only return this group value:</p>

<pre><code>(?s)message(.*?)(?:\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-2])|\Z)
           ^   ^
</code></pre>

<p>See <a href="https://regex101.com/r/yE7iF1/2" rel="nofollow">another regex demo</a></p>

python regex