Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 949455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:24:18+00:00 2026-05-15T23:24:18+00:00

I am looking through some text file for a certain string with the method.

  • 0

I am looking through some text file for a certain string with the method.

re.finditer(pattern,text) I would like to know when this returns nothing. meaning that it could find nothing in the passed text.

I know that callable iterators, have next() and __iter__

I would like to know if I could get the size or find out if it returns no string matching my pattern.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:24:18+00:00Added an answer on May 15, 2026 at 11:24 pm

    EDIT 3: The answer by @hynekcer is much much better than this.

    EDIT 2: This will not work if you have an infinite iterator, or one which consumes too many Gigabytes (in 2010 1 Gigabyte is still a large amount of ram/ disk space) of RAM/disk space.

    You have already seen a good answer, but here is an expensive hack that you can use if you want to eat a cake and have it too 🙂 The trick is that we have to clone the cake, and when you are done eating, we put it back into the same box. Remember, when you iterate over the iterator, it usually becomes empty, or at least loses previously returned values.

    >>> def getIterLength(iterator):
        temp = list(iterator)
        result = len(temp)
        iterator = iter(temp)
        return result
    
    >>>
    >>> f = xrange(20)
    >>> f
    xrange(20)
    >>> 
    >>> x = getIterLength(f)
    >>> x
    20
    >>> f
    xrange(20)
    >>> 
    

    EDIT: Here is a safer version, but using it still requires some discipline. It does not feel quite Pythonic. You would get the best solution if you posted the whole relevant code sample that you are trying to implement.

    >>> def getIterLenAndIter(iterator):
        temp = list(iterator)
        return len(temp), iter(temp)
    
    >>> f = iter([1,2,3,7,8,9])
    >>> f
    <listiterator object at 0x02782890>
    >>> l, f = getIterLenAndIter(f)
    >>> 
    >>> l
    6
    >>> f
    <listiterator object at 0x02782610>
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the preg_split function: $array = preg_split("/\^+/", $str); May 16, 2026 at 6:49 pm
  • Editorial Team
    Editorial Team added an answer This query solved my problem: $sql = "SELECT wposts.ID AS… May 16, 2026 at 6:49 pm
  • Editorial Team
    Editorial Team added an answer have you tried using the keyUp event? May 16, 2026 at 6:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I've been looking through some similar questions without any luck. What I'd like to
In one of my current side projects, I am scanning through some text looking
I'm looking for a way to (hopefully) create a text file which lists all
I have an application that is looking through some files for old data. In
I am looking for some way to code a function (I'm open to any
I am looking for some advice on the best way to retrieve information from
I need to read a large space-seperated text file and count the number of
I don't have a specific question here but I'm more looking for some guidance
I have some VB code which makes use of a COM api and results
I am trying, and failing, to create a list of labels and text boxes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.