C++ Regex Search如何匹配多次文本?

x_airsky 2017-10-23 11:15:01
如110110121110110110211011101010110

我要多次匹配110并取出

请问应该如何写?
...全文
1081 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
楼上正解:

#include <iostream>
#include <regex>
#include <string>
using namespace std;

int main() {
	string str;

	str = "110110121110110110211011101010110";
	regex reg("\\b(110)([^ ]*)");
	std::smatch sm;    // same as std::match_results<string::const_iterator> sm;  

	if (regex_match(str, reg))
		cout << "string object matched\n";

	regex_match(str, sm, reg);
	cout << "string object with " << sm.size() << " matches\n";
	for (int i = 0; i < sm.size(); i++) {
		cout << (sm[i]) << endl;
	}

	return 0;
}
hongwenjun 2017-10-23
  • 打赏
  • 举报
回复
  // regex_search example
 #include <iostream>
 #include <string>
 #include <regex>
 
 int main()
 {
     std::string s("this subject has a submarine as a subsequence");
     std::smatch m;
     std::regex e("\\b(sub)([^ ]*)");    // matches words beginning by "sub"  //GCC未支持
 
 
     std::cout << "目标序列: " << s << std::endl;
     std::cout << "正则公式: /\\b(sub)([^ ]*)/" << std::endl;
     std::cout << "The following matches and submatches were found:" << std::endl;
 
     while (std::regex_search(s, m, e)) {
         // 输出方式1
         for (auto i = 0 ; i != m.size() ; ++i)
             std::cout << m[i] << " ";
         std::cout << std::endl;
         // 输出方式2
         for (auto it = m.begin() ; it != m.end() ; ++it)
             std::cout << *it << " ";
         std::cout << std::endl;
 
 //      for (auto x:m) std::cout << x << " ";   // VC2010 不支持
 
         // 格式化输出
         std::cout << m.format("公式匹配: [$0].\n");
         std::cout << m.format("子公司S1和S2匹配:  [$1] and [$2].\n\n");
 
         s = m.suffix().str();  // 返回末端,作为新的搜索的开始
     }
 
     return 0;
 }
xiaohuh421 2017-10-23
  • 打赏
  • 举报
回复
hongwenjun 2017-10-23
  • 打赏
  • 举报
回复


https://www.youtube.com/watch?v=_79j_-2xMrQ&t=132


使用正则迭代器 好像很不错
hongwenjun 2017-10-23
  • 打赏
  • 举报
回复
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <regex>
using namespace std;

int main()
{
    ifstream infile("main.cpp"); // 对本CPP源文件处理
    ofstream fcout("OK.html");
    stringstream oss;
    oss << infile.rdbuf();

    string s(oss.str());
    regex reg_beg("<a href=\"http://topic\\.csdn\\.net");  // 匹配 <a href="http://topic.csdn.net
    regex reg_end("</a>");
    smatch m;
    smatch m2;

    string::const_iterator it = s.begin();
    string::const_iterator end = s.end();

    while (regex_search(it, end, m, reg_beg)) { // 以迭代器区间搜索正则式
        it = m[0].second;  // 前次匹配的末尾迭代器传给it,当下次搜索搜索的开始
        if (regex_search(m[0].first, end, m2, reg_end))
            fcout << string(m[0].first, m2[0].second) << "<p>" << endl;
        cout << string(m[0].first, m2[0].second) << "<p>" << endl;
    }
    infile.close(); fcout.close();
    return 0;
}


#if(0)  // 示例 HTML文本
{
<a href="http://topic.csdn.net/u/20111116/11/96be7f40-497f-4ab6-aacc-d3391bbbf237.html"
target="_blank">
学习分享:C++   00x直接使用boost正则使用方法
</a><p>
<a href="http://topic.csdn.net/u/20110831/18/07df11c3-817f-4163-b092-e8fc04d34137.html"
target="_blank">
发个简单的代码,因为很多人   分不清char   c[]   和   char   *c
</a><p>
<a href="http://topic.csdn.net/u/20110328/19/ace9d89a-88c7-4721-9d83-574b56c7564b.html"
target="_blank">
Code::BLOCKS   语法高亮   C/C++   标准库   NAMESPACE   STD   WinAPI   WX_API
</a><p>
}
#endif
你也可以使用迭代器当参数了,这样避免了 string 的 复制
hongwenjun 2017-10-23
  • 打赏
  • 举报
回复
我觉得性能应该还过的去吧,你可以按实际的程序 试试看
x_airsky 2017-10-23
  • 打赏
  • 举报
回复
引用 2 楼 hongwenjun 的回复:
  // regex_search example
 #include <iostream>
 #include <string>
 #include <regex>
 
 int main()
 {
     std::string s("this subject has a submarine as a subsequence");
     std::smatch m;
     std::regex e("\\b(sub)([^ ]*)");    // matches words beginning by "sub"  //GCC未支持
 
 
     std::cout << "目标序列: " << s << std::endl;
     std::cout << "正则公式: /\\b(sub)([^ ]*)/" << std::endl;
     std::cout << "The following matches and submatches were found:" << std::endl;
 
     while (std::regex_search(s, m, e)) {
         // 输出方式1
         for (auto i = 0 ; i != m.size() ; ++i)
             std::cout << m[i] << " ";
         std::cout << std::endl;
         // 输出方式2
         for (auto it = m.begin() ; it != m.end() ; ++it)
             std::cout << *it << " ";
         std::cout << std::endl;
 
 //      for (auto x:m) std::cout << x << " ";   // VC2010 不支持
 
         // 格式化输出
         std::cout << m.format("公式匹配: [$0].\n");
         std::cout << m.format("子公司S1和S2匹配:  [$1] and [$2].\n\n");
 
         s = m.suffix().str();  // 返回末端,作为新的搜索的开始
     }
 
     return 0;
 }
这样搜索的话性能消耗有点凶,而且匹配大量字符串的话效率有点低。 请问大神是否有优化方案呢
x_airsky 2017-10-23
  • 打赏
  • 举报
回复
引用 2 楼 hongwenjun 的回复:
  // regex_search example
 #include <iostream>
 #include <string>
 #include <regex>
 
 int main()
 {
     std::string s("this subject has a submarine as a subsequence");
     std::smatch m;
     std::regex e("\\b(sub)([^ ]*)");    // matches words beginning by "sub"  //GCC未支持
 
 
     std::cout << "目标序列: " << s << std::endl;
     std::cout << "正则公式: /\\b(sub)([^ ]*)/" << std::endl;
     std::cout << "The following matches and submatches were found:" << std::endl;
 
     while (std::regex_search(s, m, e)) {
         // 输出方式1
         for (auto i = 0 ; i != m.size() ; ++i)
             std::cout << m[i] << " ";
         std::cout << std::endl;
         // 输出方式2
         for (auto it = m.begin() ; it != m.end() ; ++it)
             std::cout << *it << " ";
         std::cout << std::endl;
 
 //      for (auto x:m) std::cout << x << " ";   // VC2010 不支持
 
         // 格式化输出
         std::cout << m.format("公式匹配: [$0].\n");
         std::cout << m.format("子公司S1和S2匹配:  [$1] and [$2].\n\n");
 
         s = m.suffix().str();  // 返回末端,作为新的搜索的开始
     }
 
     return 0;
 }
灰常感谢!

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧