Python 3 regex How to use grouping properly?
<p><strong>EDIT: Sorry, I am just allowed to insert 2 links, the other ones are as plain text here.</strong></p>

<p>Hi
I am very new to Python and regex. I searched at regex101 and many other sites how to use it properly but it just isn't working and I don't know what to do any more.</p>

<p>I have some IP cameras at home and they send over ftp pictures. 
They have a names like (I removed MAC and serial from the strings):</p>

<ul>
<li>image_20130225_175225_9.jpg</li>
<li>AABBCCDDEEFF(UserNameOfCam)_0_20150905215835_0.jpg</li>
<li>112233445566(0000serial)_0_20130625223148_1337167.jpg</li>
<li>012345-6789ABCDEF01-234567_20160724_180030.jpg</li>
<li>AA_BB_CC_DD_EE_FF_OPI-012345-QWERT_0_20130724133101_8.jpg</li>
</ul>

<p>To free up space once a day all those jpeg pictures will be converted to a mp4 over mencoder with a batch file (windows). That is already working BUT in the image is not burned in the time information. It's just a plain picture without a OSD. </p>

<p>So I tried making a .srt file to show the time and date as subtitle in the video player. Because I just learned C in school I hardcoded a ugly way to produce it with looking at each filename and searched manually what part in the string determines each cam e.g. the first cam always starts with "image_" the second with the name of the cam, the third would be the serial in the filename and so on.</p>

<p>It looks like that:</p>

<pre><code>if(strstr(temp_line, "image"))  //for the first cam
        { do extracting info }
else if(strstr(temp_line, "(UserNameOfCam)_")) //for the second
        { do extracting info }
else if(...
</code></pre>

<p>It was very unflexible and if I want to rename the 2nd cam, I had to change the sourcecode of the srt generator manually in C and recompile it every time. </p>

<p>Then I thought "Hell, lets try Python, it was made for scripting like that" and after several days and hour of hours programming and googling I made the script. Then I wanted to do it with regex because it would fit perfect.</p>

<p>Because of the beginner I am, I use <strong>debuggex</strong> to visualize the regex making and <strong>pythex</strong> for the matching. So far it can recognise the first camera and I was happy :) but after now several hours of trying getting it recognise more then 1 pattern I don't know what I am doing wrong. I tried the \g the (?P= and many other methodes of those but none are working. I am doing something completly wrong and don't know what.</p>

<p>Here is the pattern of the first cam</p>

<blockquote>
  <p>www.debuggex.com/r/kvd5IZc760Z-cZmz</p>
</blockquote>

<p><a href="http://pythex.org/?regex=(%3FP%3Ccam00%3E%20%5E%20(%3F%3Aimage_)%20(%3FP%3Cyear%3E(%3F%3A19%7C20)%5Cd%5Cd)%20(%3FP%3Cmonth%3E0%5B1-9%5D%7C1%5B012%5D)%20(%3FP%3Cday%3E0%5B1-9%5D%7C%5B12%5D%5B0-9%5D%7C3%5B01%5D)%20(%3F%3A_)%20(%3FP%3Chour%3E0%5B0-9%5D%7C1%5B0-9%5D%7C2%5B0-4%5D)%20(%3FP%3Cminute%3E%5B0-5%5D%5B0-9%5D)%20(%3FP%3Csecond%3E%5B0-5%5D%5B0-9%5D)%20(%3F%3A_%5Cd%7B1%2C2%7D.jpg)%20%24%20)&amp;test_string=image_20130225_175225_9.jpg&amp;ignorecase=0&amp;multiline=0&amp;dotall=0&amp;verbose=1" rel="nofollow">Here is the matching of the first cam</a></p>

<p>Here is the not working try for the first two cams</p>

<blockquote>
  <p>www.debuggex.com/r/C0TwsxHS9QZoXIFc</p>
</blockquote>

<p>And here is the Python script of making the srt if needed.</p>

<blockquote>
  <p>pastebin.com/kZPQnu9T</p>
</blockquote>

<p>Any hints or recommendation what to do to make it work, or where I made the wrong steps?</p>

<p><strong>EDIT2:</strong> I forgot to mention that I need information extracted from the regex as the time and dates are stores IN the filename. </p>

<p><strong>EDIT3:</strong> Thanks for the replya. I thought placing everything in one regex would make it faster, as it has to parse up to 100k lines. Also I tried to extract information out of the regex, as it is possible to name pattern as groups like "year". The Year information is always the same, so I thought, well make the year pattern once and just reuse it when needed, the extraction is then also handled....though it's not working that way. It can extract all the usefull information out from the first cam, but I don't get any match for the 2nd one if I try to reuse the pattern from the first cam. The "(?P=year)" line isnt matching and if I replase it with the same line as the first cam, it has a error because the year group is now doubled. Though it works IF i remove the whole pattern of the first cam.</p>

<p>You don't need to make one gigantic regex that matches everything. It's better to make several smaller regex that matches the specific camera. </p>

<p>So if you have want to process the camera 1 and camera 2 the same your code could look something like this. </p>

<pre><code>import re

cam1 = re.compile(r'some regex')
cam2 = re.compile(r'something else')

if cam1.match(filename) and cam2.match(filename):
    process_data()
</code></pre>

<p>Also, you'll only need grouping if you want to extract information from the regex. Otherwise grouping isn't really needed (unless perhaps you want to make the regex more legible). </p>

python scripting subtitle