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>Strings in python are iterable, so <code>sfarm</code> is an iterable, but that has little meaning in this case. From reading what <a href="http://www.pennington.net/py/ciscoconfparse/api_CiscoConfParse.html#ciscoconfparse.CiscoConfParse.find_all_children" rel="nofollow"><code>CiscoConfParse.find_all_children()</code></a> does, it is apparent that your <code>sfarm</code> is the <code>linespec</code>, which is a regular expression string. You do not need to explicitly use the <code>re</code> module here; just pass <code>sfarm</code> concatenated with <code>'$'</code>:</p>

<pre><code>search_string = ['xrout:55','old:23']
...
for sfarm in search_str:
    int_objs = parse.find_all_children(sfarm + '$')  # one of many ways to concat
...
</code></pre>

python