int RegularExpression::split(const std::string& subject, std::string::size_type offset, std::vector<std::string>& strings, int options) const { MatchVec matches; strings.clear(); int rc = match(subject, offset, matches, options); strings.reserve(matches.size()); for (MatchVec::const_iterator it = matches.begin(); it != matches.end(); ++it) { if (it->offset != std::string::npos) strings.push_back(subject.substr(it->offset, it->length)); else strings.push_back(std::string()); } return rc; }
int RegularExpression::match(const std::string& subject, std::string::size_type offset, MatchVec& matches, int options) const { poco_assert (offset <= subject.length()); matches.clear(); int ovec[OVEC_SIZE]; int rc = pcre_exec(_pcre, _extra, subject.c_str(), int(subject.size()), int(offset), options & 0xFFFF, ovec, OVEC_SIZE); if (rc == PCRE_ERROR_NOMATCH) { return 0; } else if (rc == PCRE_ERROR_BADOPTION) { throw RegularExpressionException("bad option"); } else if (rc == 0) { throw RegularExpressionException("too many captured substrings"); } else if (rc < 0) { std::ostringstream msg; msg << "PCRE error " << rc; throw RegularExpressionException(msg.str()); } matches.reserve(rc); for (int i = 0; i < rc; ++i) { Match m; m.offset = ovec[i*2] < 0 ? std::string::npos : ovec[i*2] ; m.length = ovec[i*2 + 1] - m.offset; matches.push_back(m); } return rc; }
int mainStaticMatchStrengths() { bool matchGlobalOrientations = true; // Make images as Mats; convert to IplImage for OpenSURF library actions cv::Mat mimg1, mimg2; mimg1=cv::imread("OpenSURF/imgs/img1.jpg", CV_LOAD_IMAGE_COLOR); mimg2=cv::imread("OpenSURF/imgs/img2.jpg", CV_LOAD_IMAGE_COLOR); IplImage iimg1, iimg2; iimg1=mimg1; iimg2=mimg2; IplImage *img1, *img2; img1 = &iimg1; img2 = &iimg2; IpVec ipts1, ipts2; surfDetDes(img1,ipts1,false,4,4,2,0.0001f,matchGlobalOrientations); surfDetDes(img2,ipts2,false,4,4,2,0.0001f,matchGlobalOrientations); MatchVec matches; getMatchesSymmetric(ipts1,ipts2,matches); IpVec mpts1, mpts2; const int & w = img1->width; for (unsigned int i = 0; i < matches.size(); ++i) { float strengthOverThreshold = 1 - matches[i].second; // /MATCH_THRESHOLD; strengthOverThreshold*=255; CvScalar clr = cvScalar(strengthOverThreshold,strengthOverThreshold,strengthOverThreshold); clr = cvScalar(255,255,255); //drawPoint(img1,matches[i].first.first,clr); //drawPoint(img2,matches[i].first.second,clr), mpts1.push_back(matches[i].first.first); mpts2.push_back(matches[i].first.second); cvLine(img1,cvPoint(matches[i].first.first.x,matches[i].first.first.y),cvPoint(matches[i].first.second.x+w,matches[i].first.second.y), clr,1); cvLine(img2,cvPoint(matches[i].first.first.x-w,matches[i].first.first.y),cvPoint(matches[i].first.second.x,matches[i].first.second.y), clr,1); } drawIpoints(img1,mpts1); drawIpoints(img2,mpts2); std::cout<< "Matches: " << matches.size() << std::endl; cvNamedWindow("1", CV_WINDOW_AUTOSIZE ); cvNamedWindow("2", CV_WINDOW_AUTOSIZE ); cvShowImage("1", img1); cvShowImage("2",img2); cvWaitKey(0); // NOW DO IT AGAIN! cv::Mat mimg3, mimg4; mimg3=cv::imread("OpenSURF/imgs/img1.jpg", CV_LOAD_IMAGE_COLOR); mimg4=cv::imread("OpenSURF/imgs/img2.jpg", CV_LOAD_IMAGE_COLOR); IplImage iimg3, iimg4; iimg3=mimg3; iimg4=mimg4; IplImage *img3, *img4; img3 = &iimg3; img4 = &iimg4; IpVec ipts3, ipts4; surfDetDes(img3,ipts3,false,4,4,2,0.0001f,!matchGlobalOrientations); surfDetDes(img4,ipts4,false,4,4,2,0.0001f,!matchGlobalOrientations); matches.clear(); getMatchesSymmetric(ipts3,ipts4,matches); IpVec mpts3, mpts4; for (unsigned int i = 0; i < matches.size(); ++i) { float strengthOverThreshold = 1 - matches[i].second; // /MATCH_THRESHOLD; strengthOverThreshold*=255; CvScalar clr = cvScalar(strengthOverThreshold,strengthOverThreshold,strengthOverThreshold); clr = cvScalar(255,255,255); //drawPoint(img1,matches[i].first.first,clr); //drawPoint(img2,matches[i].first.second,clr), mpts3.push_back(matches[i].first.first); mpts4.push_back(matches[i].first.second); cvLine(img3,cvPoint(matches[i].first.first.x,matches[i].first.first.y),cvPoint(matches[i].first.second.x+w,matches[i].first.second.y), clr,1); cvLine(img4,cvPoint(matches[i].first.first.x-w,matches[i].first.first.y),cvPoint(matches[i].first.second.x,matches[i].first.second.y), clr,1); } drawIpoints(img3,mpts3); drawIpoints(img4,mpts4); std::cout<< "Matches: " << matches.size() << std::endl; cvNamedWindow("3", CV_WINDOW_AUTOSIZE ); cvNamedWindow("4", CV_WINDOW_AUTOSIZE ); cvShowImage("3", img3); cvShowImage("4",img4); cvWaitKey(0); return 0; }
unsigned OptimizedRegularExpressionImpl<thread_safe>::match(const char * subject, size_t subject_size, MatchVec & matches, unsigned limit) const { matches.clear(); if (limit == 0) return 0; if (limit > number_of_subpatterns + 1) limit = number_of_subpatterns + 1; if (is_trivial) { const char * pos; if (is_case_insensitive) pos = strcasestr(subject, required_substring.data()); else pos = strstr(subject, required_substring.data()); if (pos == nullptr) return 0; else { Match match; match.offset = pos - subject; match.length = required_substring.size(); matches.push_back(match); return 1; } } else { if (!required_substring.empty()) { const char * pos; if (is_case_insensitive) pos = strcasestr(subject, required_substring.data()); else pos = strstr(subject, required_substring.data()); if (nullptr == pos) return 0; } StringPieceType pieces[MAX_SUBPATTERNS]; if (!re2->Match(StringPieceType(subject, subject_size), 0, subject_size, RegexType::UNANCHORED, pieces, limit)) return 0; else { matches.resize(limit); for (size_t i = 0; i < limit; ++i) { if (pieces[i] != nullptr) { matches[i].offset = pieces[i].data() - subject; matches[i].length = pieces[i].length(); } else { matches[i].offset = std::string::npos; matches[i].length = 0; } } return limit; } } }