regex search loops instead of findall

brad byte8bits at gmail.com
Tue Aug 5 14:06:04 EDT 2008


Hi guys... I'm trying to make my Python regex code behave like my C++ 
regex code. In order to search large strings for *all* occurrences of 
the thing I'm searching for, I loop like this in C++:


void number_search(const std::string& portion, const boost::regex& Numbers)
   {

     boost::smatch matches;
     std::string::const_iterator Start = portion.begin();
     std::string::const_iterator End = portion.end();

     while (boost::regex_search(Start, End, matches, Numbers))
       {
       std::cout << matches.str() << std::endl;
       Start = matches[0].second;
       }
   }

I cannot figure out how to do the same in Python. I've read several Py 
regex docs, but none of them go into examples of this, although search 
seems that it should be able to loop on position according to the docs. 
I've resorted to using find_all in python, but that has limitations 
(especially when searching for groups) and seems to be a lot less 
efficient than search. Any suggestions or example code I can look at?

I've read these:
http://www.amk.ca/python/howto/regex
http://docs.python.org/lib/module-re.html

Thanks.



More information about the Python-list mailing list