regex search loops instead of findall

Waldemar Osuch waldemar.osuch at gmail.com
Tue Aug 5 14:15:27 EDT 2008


On Aug 5, 12:06 pm, brad <byte8b... at gmail.com> wrote:
> 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/regexhttp://docs.python.org/lib/module-re.html
>
> Thanks.

re.finditer may help.
http://docs.python.org/lib/node46.html



More information about the Python-list mailing list