使用C正则表达式查找第一场比赛的索引

使用C正则表达式查找第一场比赛的索引,第1张

概述我正在尝试使用正则表达式在C中编写分割函数.到目前为止,我已经想出了这个; vector<string> split(string s, regex r){ vector<string> splits; while (regex_search(s, r)) { int split_on = // index of regex match 我正在尝试使用正则表达式在C中编写分割函数.到目前为止,我已经想出了这个;

vector<string> split(string s,regex r){    vector<string> splits;    while (regex_search(s,r))     {        int split_on = // index of regex match        splits.push_back(s.substr(0,split_on));        s = s.substr(split_on + 1);    }    splits.push_back(s);    return splits;}

我想知道的是如何填写注释行.

解决方法 您只需要多一点,但请参阅下面代码中的注释. man技巧是使用匹配对象,这里是std :: smatch,因为你在std :: string上匹配,要记住你匹配的地方(不仅仅是你所做的):

vector<string> split(string s,regex r){  vector<string> splits;  smatch m; // <-- need a match object  while (regex_search(s,m,r))  // <-- use it here to get the match  {    int split_on = m.position(); // <-- use the match position    splits.push_back(s.substr(0,split_on));    s = s.substr(split_on + m.length()); // <-- also,skip the whole match  }  if(!s.empty()) {    splits.push_back(s); // and there may be one last token at the end  }  return splits;}

这可以这样使用:

auto v = split("foo1bar2baz345qux",std::regex("[0-9]+"));

并会给你“foo”,“bar”,“baz”,“qux”.

std :: smatch是std :: match_results的特化,其参考文档存在here.

总结

以上是内存溢出为你收集整理的使用C正则表达式查找第一场比赛索引全部内容,希望文章能够帮你解决使用C正则表达式查找第一场比赛的索引所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1216460.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存