
How can I do a regex search that will return me all patterns for the given regular expression? Somehow like preg_match_all in PHP. -- View this message in context: http://www.nabble.com/regex_search-all-tf4292590.html#a12219928 Sent from the Boost - Users mailing list archive at Nabble.com.

izua wrote:
How can I do a regex search that will return me all patterns for the given regular expression? Somehow like preg_match_all in PHP.
Please see my previous reply: http://article.gmane.org/gmane.comp.lib.boost.user/29880 John.

John Maddock <john <at> johnmaddock.co.uk> writes:
izua wrote:
How can I do a regex search that will return me all patterns for the given regular expression? Somehow like preg_match_all in PHP.
Please see my previous reply: http://article.gmane.org/gmane.comp.lib.boost.user/29880
John.
Here is an example: #include <iterator> #include <boost/regex.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost; vector<string> preg_match_all(const string & s, const regex & expr) { typedef regex_iterator<string::const_iterator> iter; vector<string> res; transform ( iter(s.begin(), s.end(), expr), iter(), back_inserter(res), bind(&iter::value_type::str, _1, 0) ); return res; } preg_match_all("abcd123.ef56\tessn", regex("[0-9]+?[\\W]+?")); Roman Perepelitsa.

Thanks! This works, but it's not exactly what I want. The idea is to return all captures in all matches. Roman Perepelitsa-2 wrote:
John Maddock <john <at> johnmaddock.co.uk> writes:
izua wrote:
How can I do a regex search that will return me all patterns for the given regular expression? Somehow like preg_match_all in PHP.
Please see my previous reply: http://article.gmane.org/gmane.comp.lib.boost.user/29880
John.
Here is an example:
#include <iterator> #include <boost/regex.hpp> #include <boost/bind.hpp>
using namespace std; using namespace boost;
vector<string> preg_match_all(const string & s, const regex & expr) { typedef regex_iterator<string::const_iterator> iter; vector<string> res; transform ( iter(s.begin(), s.end(), expr), iter(), back_inserter(res), bind(&iter::value_type::str, _1, 0) ); return res; }
preg_match_all("abcd123.ef56\tessn", regex("[0-9]+?[\\W]+?"));
Roman Perepelitsa.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/regex_search-all-tf4292590.html#a12247983 Sent from the Boost - Users mailing list archive at Nabble.com.

izua wrote:
Thanks! This works, but it's not exactly what I want. The idea is to return all captures in all matches.
That's exactly what enumerating with regex_iterator gives you: everything else is just syntactic sugar storing the the results in an array or whatever. John.

Hmm.. I've checked out the documentation, and the examples are really, really way out of my league. Is there a wrapper function or something like this around it? Or anyone has one handy? I'll really appreciate it. John Maddock wrote:
izua wrote:
Thanks! This works, but it's not exactly what I want. The idea is to return all captures in all matches.
That's exactly what enumerating with regex_iterator gives you: everything else is just syntactic sugar storing the the results in an array or whatever.
John.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/regex_search-all-tf4292590.html#a12267286 Sent from the Boost - Users mailing list archive at Nabble.com.

izua wrote:
Hmm.. I've checked out the documentation, and the examples are really, really way out of my league. Is there a wrapper function or something like this around it? Or anyone has one handy? I'll really appreciate it.
See if you can understand this one then: void find_all(const std::string& s, const boost::regex& e) { boost::sregex_iterator i(s.begin(), s.end(), e), j; while(i != j) { for(unsigned s = 0; s <= i->size(); ++s) { std::cout << "Location of sub-expression " << s << " match is " << i->position(s) << std::endl; std::cout << "Length of sub-expression " << s << " match is " << i->length(s) << std::endl; std::cout << "Text of sub-expression " << s << " match is \"" << i->str(s) << "\"" << std::endl; } ++i; } } John.
participants (3)
-
izua
-
John Maddock
-
Roman Perepelitsa