Re: Need help capturing output of re.search

Discussion in 'Python' started by John Machin, Jun 27, 2008.

  1. John Machin

    John Machin Guest

    On Jun 27, 8:31 am, 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
    > ======================================
    > #!/usr/local/bin/python
    >
    > import os
    > import re
    >
    > dirName = '/home/user/bin/logs'
    > os.chdir(dirName)
    > 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'
    > else:
    > 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.

    >>> type(None)

    <type 'NoneType'>
    >>> type("None")

    <type 'str'>
    >>> None == "None"

    False
    >>>


    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_')

    HTH,
    John
    John Machin, Jun 27, 2008
    #1
    1. Advertising

  2. John Machin

    John Machin Guest

    On Jun 27, 10:01 am, wrote:
    > >You may like to read this:http://www.amk.ca/python/howto/regex/

    >
    > This is a good resource. Thank you.
    > Someone else pointed out that I needed to change the
    >
    > if reCheck == "None":
    >
    > to
    >
    > if reCheck == None: # removed the "s


    "Somebody else" should indeed remain anonymous if they told you that.
    Use
    if reCheck is None:
    or even better:
    if not reCheck:

    It's not obvious from your response if you got these points:
    (1) re.match, not re.search
    (2) filename.startswith does your job simply and more understandably
    John Machin, Jun 27, 2008
    #2
    1. Advertising

  3. John Machin

    John Machin Guest

    On Jun 27, 10:45 am, wrote:
    > On Jun 26, 5:12 pm, John Machin <> wrote:
    >
    >
    >
    > > On Jun 27, 10:01 am, wrote:

    >
    > > > >You may like to read this:http://www.amk.ca/python/howto/regex/

    >
    > > > This is a good resource. Thank you.
    > > > Someone else pointed out that I needed to change the

    >
    > > > if reCheck == "None":

    >
    > > > to

    >
    > > > if reCheck == None: # removed the "s

    >
    > > "Somebody else" should indeed remain anonymous if they told you that.
    > > Use
    > > if reCheck is None:
    > > or even better:
    > > if not reCheck:

    >
    > > It's not obvious from your response if you got these points:
    > > (1) re.match, not re.search
    > > (2) filename.startswith does your job simply and more understandably

    >
    > Understood. I replaced re.search with re.match (although both work)


    Not so. Consider a filename that starts with some non-alphanumeric
    characters followed by CC_ e.g. "---CC_foo". re.search will score a
    hit but re.match won't. It's quite simple: if you want hits only at
    the beginning of the string, use re.match. Please don't say "I don't
    have any filenames like that, so it doesn't matter" ... it's like
    saying "They couldn't hit an elephant at this distance".

    > can filename.startswith be used in a conditional? ala:
    >
    > if filename.startswith('CC_'):
    > processtheFile()


    Of course. *Any* expression can be used in a condition. This one
    returns True or False -- a prime candidate for such use. Why are you
    asking?
    John Machin, Jun 27, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Strahl [MVP]

    Capturing ASPX output from another page

    Rick Strahl [MVP], Jul 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    469
    Steve C. Orr, MCSD
    Jul 9, 2003
  2. Andy Fish

    capturing the output of a JSP

    Andy Fish, Feb 11, 2004, in forum: Java
    Replies:
    4
    Views:
    1,992
    Chris Smith
    Feb 11, 2004
  3. Replies:
    0
    Views:
    376
  4. Replies:
    1
    Views:
    327
    mlimber
    May 22, 2006
  5. Abby Lee
    Replies:
    5
    Views:
    378
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page