Find index position of characters in string
<p>I am trawling through a storage area and the paths look a lot like this: 
storagearea/storage1/ABC/ABCDEF1/raw/2013/05/ABCFGM1 </p>

<p>I wont always know what year is it. I need to find the starting index position of the year </p>

<p>Therefor I am looking for where I find the following in the file name (2010, 2011, 2012, 2013, 2014 etc...) </p>

<p>I have set up a list as follows:</p>

<pre><code>list_ = ['2010', '2011','2012','2013','2014', '2015', '2016']
</code></pre>

<p>and I can find if it is in the file name</p>

<pre><code>if any(word in file for word in list_): 
    print 'Yahooo'
</code></pre>

<p>But how do I find the character index of the year in the absolute path? </p>

<p>I'd suggest <code>join</code>ing those years to a <a href="https://docs.python.org/3/library/re.html" rel="nofollow">regular expression</a> using <code>'|'</code> as a delimiter...</p>

<pre><code>&gt;&gt;&gt; list_ = ['2010', '2011','2012','2013','2014', '2015', '2016']
&gt;&gt;&gt; p = "|".join(list_)
&gt;&gt;&gt; p
'2010|2011|2012|2013|2014|2015|2016'
</code></pre>

<p>... and then using <a href="https://docs.python.org/3/library/re.html#re.search" rel="nofollow"><code>re.search</code></a> to find a match and <code>span()</code> and <code>group()</code> to find the position of that match and the matched year itself:</p>

<pre><code>&gt;&gt;&gt; filename = "storagearea/storage1/ABC/ABCDEF1/raw/2013/05/ABCFGM1"
&gt;&gt;&gt; m = re.search(p, filename)
&gt;&gt;&gt; m.group()
'2013'
&gt;&gt;&gt; m.span()
(37, 41)
</code></pre>

python