python regex matching between multiple lines and every other match
<p>So I've been playing around with this for a few days and here is what I am looking for and the regex I have now. I have a file in this  format (there are some other fields but I have omitted those:</p>

<p>I just want to match the bold text </p>

<pre><code>ADDR 1 - XXXXXX   ADDR 1 - **XXXXXX**

ADDR 2 - XXXXXX   ADDR 2 - XXXXXX

ADDR 1 - XXXXXX   ADDR 1 - **XXXXXX**

ADDR 2 - XXXXXX   ADDR 2 - XXXXXX
</code></pre>

<p>The regex I have  written only matches the first ADDR 1 - XXXXX, but I need to match all instances of the bolded XXXXX.</p>

<pre><code>re.findall(r'ADDR 1- .*? ADDR 1-(.*?)(?=ADDR 2-)', lines, re.DOTALL)
</code></pre>

<p>Any suggestions? I feel like I might be  missing something simple, but not sure.</p>

<p>If wanting to capture every other instance of something then splitting or slicing the string is going to be <strong>much faster</strong> than using regex â the following demonstrates a very <em>basic</em> example:</p>

<p><strong><em>split()</em></strong> method:</p>

<pre><code>&gt;&gt;&gt; [i.split('ADDR 1 - ')[-1] for i in s.split('\n')[::2]]
&gt;&gt;&gt; ['AXXXXZ', 'AXXXXY']
&gt;&gt;&gt; ''' 18.3057999611 seconds - 10000000 iterations '''
</code></pre>

<p><strong><em>findall()</em></strong> method:</p>

<pre><code>&gt;&gt;&gt; re.findall(".*ADDR\s+1\s+-\s+(.*)", s)
&gt;&gt;&gt; ['AXXXXZ', 'AXXXXY']
&gt;&gt;&gt; ''' 77.5003650188 seconds - 10000000 iterations '''
</code></pre>

<p>In situations where you know regex isn't absolutely necessary consider using an alternative. Also the regex shown in the accepted answer could be optimized to cut the time nearly in half (eg. <code>re.findall("ADDR 1 .+ - (.+)", s</code>) - <code>37.0185003658 seconds - 10000000 iterations</code>.</p>

python regex