Need help capturing output of re.search
sjmachin at lexicon.net
Fri Jun 27 01:27:35 CEST 2008
On Jun 27, 8:31 am, joemacbusin... at yahoo.com wrote:
> Hi -
> I need help capturing the output of a RegEx search.
> I dont understand why this conditional fails.
> Here is the code:
> #================================= start snip
> import os
> import re
> dirName = '/home/user/bin/logs'
> files = os.listdir(dirName)
> for myFile in files:
> # print 'checking ...', myFile
> reCheck = ''
> reCheck = re.search(r'\bCC_', myFile)
> # print 'reCheck = ', '=', reCheck, '='
> if reCheck == "None":
> print myFile, ' ..... does not qualify'
> print ' ', myFile, 'qualifies...', reCheck
> #================================= end snip
> The problem is that reCheck never == None.
> So all of the files "qualify". My understanding of the
> re.search (re.search(r'\bCC_', myFile)) is ...
> 1) give me only files that start with a word boundary (\b)
> 2) followed by a (CC_)
> 3) in myFile
> Why doesn't the output of the re.search load reCheck with valid data?
> (like "None")
Because "None" is not "valid data" in this case. re.search returns
None or a MatchObject instance.
>>> None == "None"
You may like to read this: http://www.amk.ca/python/howto/regex/
If you want to check for strings that start with some condition, you
need re.match, not re.search.
You don't need the '\b'; re.match(r'CC_', filename) would do the job.
But then so would filename.startswith('CC_')
More information about the Python-list