title: Unexpected result from Python re.search for command line output
body: <p>I'm using <code>subprocess.Popen</code> to run an ffmpeg command (in Windows) and then extract the part of the output that has the frame count with a regex expression using <code>re.search</code>. Sometimes, not always, I get the wrong result from search even if the printed command output string clearly shows what I expect.</p>
  
  <p>When I use <code>re.findall</code> I get 2 results, the "wrong" one and the expected one, but in the output string of the command I still only see one option. I'd like to understand why this is happening.</p>
  
  <p>Here's the code I'm running:</p>
  
  <pre><code>import re
  import subprocess
  
  # path to video with 300 frames
  cmd = r'ffmpeg -i C:\...\300frames_HUD.avi -map 0:v:0 -c copy -f null -'
  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  output_info = p.communicate()[0]
  
  regex = r'(frame=\s*)([0-9]+)'
  search_result = re.search(regex, output_info)
  findall_result = re.findall(regex, output_info)
  print "SEARCH"
  print '0', search_result.group(0)
  print '1', search_result.group(1)
  print '2', search_result.group(2)
  
  print "FIND ALL"
  print findall_result
  </code></pre>
  
  <p>Here are the results I get:</p>
  
  <pre><code>SEARCH
  0 frame=  293
  1 frame=  
  2 293
  FIND ALL
  [('frame=  ', '293'), ('frame=  ', '300')]
  </code></pre>
  
  <p>And here is the printed <code>output_info</code>, the ffmpeg command output I'm searching on:</p>
  
  <pre><code>ffmpeg version git-2020-03-15-c467328 Copyright (c) 2000-2020 the FFmpeg developers
    built with gcc 9.2.1 (GCC) 20200122
    configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-ffnvcodec --enable-cuda-llvm --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt --enable-amf
    libavutil      56. 42.100 / 56. 42.100
    libavcodec     58. 75.100 / 58. 75.100
    libavformat    58. 41.100 / 58. 41.100
    libavdevice    58.  9.103 / 58.  9.103
    libavfilter     7. 77.100 /  7. 77.100
    libswscale      5.  6.101 /  5.  6.101
    libswresample   3.  6.100 /  3.  6.100
    libpostproc    55.  6.100 / 55.  6.100
  Input #0, avi, from 'C:\...\300frames_HUD.avi':
    Duration: 00:00:10.00, start: 0.000000, bitrate: 373255 kb/s
      Stream #0:0: Video: rawvideo, bgr24, 960x540, 374496 kb/s, 30 fps, 30 tbr, 30 tbn, 30 tbc
      Metadata:
        title           : V
  Output #0, null, to 'pipe:':
    Metadata:
      encoder         : Lavf58.41.100
      Stream #0:0: Video: rawvideo, bgr24, 960x540, q=2-31, 374496 kb/s, 30 fps, 30 tbr, 30 tbn, 30 tbc
      Metadata:
        title           : V
  Stream mapping:
    Stream #0:0 -&gt; #0:0 (copy)
  Press [q] to stop, [?] for help
  frame=  300 fps=0.0 q=-1.0 Lsize=N/A time=00:00:10.00 bitrate=N/A speed=19.4x    
  video:455625kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
  </code></pre>
  
  <p>I'm essentially looking for the 300 number in <code>frame=  300</code>.
  I can reproduce this easily when I execute it inside my IDE (pycharm) twice in a row quickly.</p>