Searching and capturing a character using regular expressions Python <p>While going through one of the problems in <a href="http://www.pythonchallenge.com/" rel="nofollow">Python Challenge</a>, I am trying to solve it as follows:</p> <p>Read the input in a text file with characters as follows:</p> <pre><code>DQheAbsaMLjTmAOKmNsLziVMenFxQdATQIjItwtyCHyeMwQTNxbbLXWZnGmDqHhXnLHfEyvzxMhSXzd BEBaxeaPgQPttvqRvxHPEOUtIsttPDeeuGFgmDkKQcEYjuSuiGROGfYpzkQgvcCDBKrcYwHFlvPzDMEk MyuPxvGtgSvWgrybKOnbEGhqHUXHhnyjFwSfTfaiWtAOMBZEScsOSumwPssjCPlLbLsPIGffDLpZzMKz jarrjufhgxdrzywWosrblPRasvRUpZLaUbtDHGZQtvZOvHeVSTBHpitDllUljVvWrwvhpnVzeWVYhMPs kMVcdeHzFZxTWocGvaKhhcnozRSbWsIEhpeNfJaRjLwWCvKfTLhuVsJczIYFPCyrOJxOPkXhVuCqCUgE luwLBCmqPwDvUPuBRrJZhfEXHXSBvljqJVVfEGRUWRSHPeKUJCpMpIsrV....... </code></pre> <p>What I need is to go through this text file and pick all lower case letters that are enclosed by only three upper-case letters on each side.</p> <p>The python script that I wrote to do the above is as follows:</p> <pre><code>import re pattern = re.compile("[a-z][A-Z]{3}([a-z])[A-Z]{3}[a-z]") f = open('/Users/Dev/Sometext.txt','r') for line in f: result = pattern.search(line) if result: print result.groups() f.close() </code></pre> <p>The above given script, instead of returning the capture(list of lower case characters), returns all the text blocks that meets the regular expression criteria, like</p> <pre><code>aXCSdFGHj vCDFeTYHa nHJUiKJHo ......... ......... </code></pre> <p>Can somebody tell me what exactly I am doing wrong here? And instead of looping through the entire file, is there an alternate way to run the regular expression search on the entire file?</p> <p>Thanks</p>