regex search loops instead of findall

B

brad

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.
 
W

Waldemar Osuch

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top