Troubleshooting: re.finditer() creates object even when no match found

Discussion in 'Python' started by Chris Lasher, Dec 17, 2004.

  1. Chris Lasher

    Chris Lasher Guest

    Hello,
    I really like the finditer() method of the re module. I'm having
    difficulty at the moment, however, because finditer() still creates a
    callable-iterator oject, even when no match is found. This is
    undesirable in cases where I would like to circumvent execution of code
    meant to parse out data from my finditer() object.

    I know that if I place a finditer() object in an iterative for loop,
    the loop will not execute, but is there some way I can test to see if
    the object contains no matches in the first place? I thought about
    using .next() but I don't want to lose the ability to process the first
    (sometimes only) match in the finditer() object.
    Thanks in advance,
    Chris
    Chris Lasher, Dec 17, 2004
    #1
    1. Advertising

  2. Re: Troubleshooting: re.finditer() creates object even when no matchfound

    Chris Lasher wrote:
    > I know that if I place a finditer() object in an iterative for loop,
    > the loop will not execute, but is there some way I can test to see if
    > the object contains no matches in the first place?


    Basically, you want to peek into an interable. See my recipes:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304373

    The short answer is that you can do something like:

    try:
    first, iterable = peek(iterable)
    except StopIteration:
    # do whatever you do if there are no matches
    else:
    # do whatever you do if there are matches

    and you won't lose the first element of the iterable.

    Steve
    Steven Bethard, Dec 17, 2004
    #2
    1. Advertising

  3. Chris Lasher

    Chris Lasher Guest

    Thanks Steve,
    That's odd that there's no built-in method to do this. It seems like
    it would be a common task. Is there any way to request a feature like
    this from the RE module keepers, whomever they may be?
    In the meantime, may I use your code, with accredation to you?
    Thanks,
    Chris
    Chris Lasher, Dec 17, 2004
    #3
  4. Re: Troubleshooting: re.finditer() creates object even when no matchfound

    Chris Lasher wrote:
    > Is there any way to request a feature like
    > this from the RE module keepers, whomever they may be?


    The most direct way would be to go to Python at sourceforge[1] and make
    a feature request to add peek to itertools. (This is probably the most
    reasonable location for it.) Requests accompanied by patches are much
    more likely to be accepted. =) Of course, your feature, if accepted,
    wouldn't be applied until Python 2.5, which is probably about a year and
    a half off.

    I actually thought about doing this myself, but I've got a few other
    things on my stack, and since itertools is (I believe) written in C,
    writing the patch would be a little more work...

    > In the meantime, may I use your code, with accredation to you?


    Help yourself. That's what the Cookbook's for. =)

    [1] http://sourceforge.net/projects/python/
    Steven Bethard, Dec 17, 2004
    #4
  5. Re: Troubleshooting: re.finditer() creates object even when nomatchfound

    Chris Lasher wrote:

    > That's odd that there's no built-in method to do this. It seems like
    > it would be a common task.


    if you do this a lot, maybe you shouldn't use finditer? iterators are
    designed to give you the next item (if any) when you're ready to deal
    with it... if that's not what you want, you can use findall, search loops,
    scanner objects, etc. or you can implement a standard "iterate ahead"
    loop.

    what's your use case?

    </F>
    Fredrik Lundh, Dec 17, 2004
    #5
  6. Chris Lasher

    Nick Coghlan Guest

    Re: Troubleshooting: re.finditer() creates object even when no matchfound

    Chris Lasher wrote:
    > Hello,
    > I really like the finditer() method of the re module. I'm having
    > difficulty at the moment, however, because finditer() still creates a
    > callable-iterator oject, even when no match is found. This is
    > undesirable in cases where I would like to circumvent execution of code
    > meant to parse out data from my finditer() object.


    Take a look at itertools.tee

    Cheers,
    Nick.

    --
    Nick Coghlan | | Brisbane, Australia
    ---------------------------------------------------------------
    http://boredomandlaziness.skystorm.net
    Nick Coghlan, Dec 18, 2004
    #6
  7. Chris Lasher

    Nick Coghlan Guest

    Re: Troubleshooting: re.finditer() creates object even when no matchfound

    Nick Coghlan wrote:
    > Chris Lasher wrote:
    >
    >> Hello,
    >> I really like the finditer() method of the re module. I'm having
    >> difficulty at the moment, however, because finditer() still creates a
    >> callable-iterator oject, even when no match is found. This is
    >> undesirable in cases where I would like to circumvent execution of code
    >> meant to parse out data from my finditer() object.

    >
    >
    > Take a look at itertools.tee


    Bleh - I hit send instead of delete. Tee probably doesn't do what you want.
    Steve's cookbook recipe is likely a better option.

    Cheers,
    Nick.



    --
    Nick Coghlan | | Brisbane, Australia
    ---------------------------------------------------------------
    http://boredomandlaziness.skystorm.net
    Nick Coghlan, Dec 18, 2004
    #7
  8. Re: Troubleshooting: re.finditer() creates object even when no matchfound

    Nick Coghlan wrote:
    > Nick Coghlan wrote:
    >
    >> Chris Lasher wrote:
    >>
    >>> Hello,
    >>> I really like the finditer() method of the re module. I'm having
    >>> difficulty at the moment, however, because finditer() still creates a
    >>> callable-iterator oject, even when no match is found. This is
    >>> undesirable in cases where I would like to circumvent execution of code
    >>> meant to parse out data from my finditer() object.

    >>
    >> Take a look at itertools.tee

    >
    > Bleh - I hit send instead of delete. Tee probably doesn't do what you
    > want. Steve's cookbook recipe is likely a better option.


    Actually, there's an equally valid solution with tee too -- check Peter
    Otten's comments at the bottom of the recipe.

    Steve
    Steven Bethard, Dec 18, 2004
    #8
  9. Re: Troubleshooting: re.finditer() creates object even when no matchfound

    Steven Bethard wrote:

    > first, iterable = peek(iterable)


    I really like this as a general solution to a problem that bothers me
    occasionally. IMHO it's much better than having UndoFiles or similar
    things lying about for every use case.

    Thanks!
    --
    Michael Hoffman
    Michael Hoffman, Dec 18, 2004
    #9
    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. =?Utf-8?B?TG9yaSBNY0RvbmFsZA==?=

    Troubleshooting Object Reference Not Set to Instance of an Object

    =?Utf-8?B?TG9yaSBNY0RvbmFsZA==?=, Apr 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    466
    MS News \(MS ILM\)
    Apr 10, 2004
  2. Robert Oschler

    An ode to re.finditer()

    Robert Oschler, Aug 1, 2004, in forum: Python
    Replies:
    0
    Views:
    332
    Robert Oschler
    Aug 1, 2004
  3. Erik Johnson

    using re.finditer()

    Erik Johnson, Oct 27, 2004, in forum: Python
    Replies:
    4
    Views:
    19,009
    Erik Johnson
    Oct 27, 2004
  4. Erick
    Replies:
    9
    Views:
    641
    Erick
    Feb 3, 2005
  5. kj
    Replies:
    1
    Views:
    121
Loading...

Share This Page