regex search loops instead of findall
waldemar.osuch at gmail.com
Tue Aug 5 20:15:27 CEST 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.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
re.finditer may help.
More information about the Python-list