python regex find matched string
<p>I am trying to find the matched string in a string using regex in Python. The <code>string</code> looks like this:</p>

<pre><code>band   1 # energy  -53.15719532 # occ.  2.00000000

ion      s      p      d    tot
  1  0.000  0.995  0.000  0.995
  2  0.000  0.000  0.000  0.000
tot  0.000  0.996  0.000  0.996

band   2 # energy  -53.15719532 # occ.  2.00000000

ion      s      p      d    tot
  1  0.000  0.995  0.000  0.995
  2  0.000  0.000  0.000  0.000
tot  0.000  0.996  0.000  0.996

band   3 # energy  -53.15719532 # occ.  2.00000000
</code></pre>

<p>My goal is to find the string after <code>tot</code>. So the matched string will be something like:</p>

<pre><code>['0.000  0.996  0.000  0.996', 
'0.000  0.996  0.000  0.996']
</code></pre>

<p>Here is my current code:</p>

<pre><code>pattern = re.compile(r'tot\s+(.*?)\n', re.DOTALL)
pattern.findall(string)
</code></pre>

<p>However, the output gives me:</p>

<pre><code>['1  0.000  0.995  0.000  0.995',
 '0.000  0.996  0.000  0.996',
 '1  0.000  0.995  0.000  0.995',
 '0.000  0.996  0.000  0.996']
</code></pre>

<p>Any idea of what I am doing wrong?</p>

<p>You don't want the <code>DOTALL</code> flag. Remove it and use <a href="https://docs.python.org/2/library/re.html#re.MULTILINE" rel="nofollow"><code>MULTILINE</code></a> instead.</p>

<pre><code>pattern = re.compile(r'^\s*tot(.*)', re.MULTILINE)
</code></pre>

<p>This matches all lines that start with <code>tot</code>. The rest of the line will be in group 1.</p>

<p>Citing the <a href="https://docs.python.org/2/library/re.html#re.DOTALL" rel="nofollow">documentation</a>, emphasis mine:</p>

<blockquote>
  <p><code>re.DOTALL</code></p>
  
  <p>Make the <code>'.'</code> special character match any character at all, <strong>including
  a newline</strong>; without this flag, <code>'.'</code> will match anything except a
  newline.</p>
</blockquote>

<p>Note that you can easily do this without regex.</p>

<pre><code>with open("input.txt", "r") as data_file:
    for line in data_file:
        items = filter(None, line.split(" "))
        if items[0] == "tot":
            # etc
</code></pre>

python regex