Regex for format ( HHh MMs SSs ) with optional hours
<p>I am struggling to get a regex working for the below format. Pointers appeciated</p>

<pre><code>( 43m 12s )
( 13m 11s )
( 11h 43m 12s )
( 1h 43m 12s )
</code></pre>

<p>Edit:</p>

<p>The above examples are part of longer strings.</p>

<p>Edit2:</p>

<p>This is what I have now:</p>

<pre><code> \s\(\s\d{1,2}[a-z]\s.*\)
</code></pre>

<p>You don't necessarily need to approach it with regular expressions.</p>

<p>Here is an another option - use a <a href="https://labix.org/python-dateutil" rel="nofollow"><code>dateutil</code></a> datetime parser:</p>

<pre><code>&gt;&gt;&gt; from dateutil.parser import parse
&gt;&gt;&gt; l = ["43m 12s", "13m 11s", "11h 43m 12s", "1h 43m 12s"]
&gt;&gt;&gt; for item in l:
...     dt = parse(item)
...     print(item, dt.hour, dt.minute, dt.second)
... 
('43m 12s', 0, 43, 12)
('13m 11s', 0, 13, 11)
('11h 43m 12s', 11, 43, 12)
('1h 43m 12s', 1, 43, 12)
</code></pre>

<p>Or, you can use <a href="https://docs.python.org/2/library/time.html#time.strptime" rel="nofollow"><code>time.strptime()</code></a> trying out <code>%Hh %Mm %Ss</code> and, if it fails, trying <code>%Mm %Ss</code> format.</p>

python regex