Regex on list element in for loop
<p>I have a script that searches through config files and finds all matches of strings from another list as follows:</p>

<pre><code>dstn_dir = "C:/xxxxxx/foobar"
dst_list =[]
files = [fn for fn in os.listdir(dstn_dir)if fn.endswith('txt')]
dst_list = []
for file in files:
    parse = CiscoConfParse(dstn_dir+'/'+file)
    for sfarm in search_str:
        int_objs = parse.find_all_children(sfarm)
        if len(int_objs) &gt; 0:
            dst_list.append(["\n","#" *40,file + " " + sfarm,"#" *40])
            dst_list.append(int_objs)
</code></pre>

<p>I need to change this part of the code:</p>

<pre><code> for sfarm in search_str:
            int_objs = parse.find_all_children(sfarm)
</code></pre>

<p><code>search_str</code> is a list containing strings similar to <code>['xrout:55','old:23']</code> and many others.</p>

<p>So it will only find entries that end with the string from the list I am iterating through in <code>sfarm</code>.  My understanding is that this would require my to use <code>re</code> and match on something like <code>sfarm$</code> but Im not sure on how to do this as part of the loop.</p>

<p>Am I correct in saying that <code>sfarm</code> is an iterable?  If so I need to know how to regex on an iterable object in this context.</p>

<p>Please check this code. Used glob module to get all "*.txt" files in folder.</p>

<p>Please check here for more info on <a href="https://pymotw.com/2/glob/" rel="nofollow">glob module</a>.</p>

<pre><code>import glob
import re
dst_list = []
search_str = ['xrout:55','old:23']
for file_name in glob.glob(r'C:/Users/dinesh_pundkar\Desktop/*.txt'):
    with open(file_name,'r') as f:
        text = f.read()
        for sfarm in search_str:
            regex = re.compile('%s$'%sfarm)
            int_objs = regex.findall(text)
            if len(int_objs) &gt; 0:
                dst_list.append(["\n","#" *40,file_name + " " + sfarm,"#" *40])
                dst_list.append(int_objs)
print dst_list
</code></pre>

<p>Output:</p>

<pre><code> C:\Users\dinesh_pundkar\Desktop&gt;python a.py
[['\n', '########################################', 'C:/Users/dinesh_pundkar\\De
sktop\\out.txt old:23', '########################################'], ['old:23']]

C:\Users\dinesh_pundkar\Desktop&gt;
</code></pre>

python