使用regex_search获取所有匹配的索引? [英] Get the index of all matches using regex_search?
问题描述
我刚刚开始学习如何使用 regex
进行字符串处理( C ++ 11
新功能)。
目前我应用以下代码来获取所有匹配的索引:
string str =aaabxxxaab;
regex rx(ab);
vector< int> index_matches; // results saved here(should be {2,8})
int track = 0;
smatch sm;
while(regex_search(str,sm,rx))
{
index_matches.push_back(track + sm.position());
string tmp = sm.suffix()。str();
track + = str.length() - tmp.length(); // update base index
str = tmp;
}
它可以正常工作,但我必须更新
(基本索引)每次手动使其正常工作。
同时,我注意到已经有 smatch :: size()
和 smatch :: position()
,我想结合使用来实现目标。以下是我想将它们组合在一起但不能工作的代码(即总是只得到 {2}
)。
string str =aaabxxxaab;
regex rx(ab);
vector< int> index_matches; // results saved here
//(应该是{2,8},但总是只得到{2})
smatch sm;
regex_search(str,sm,rx);
for(int i = 0; i< sm.size(); i ++)
index_matches.push_back(sm.position(i));有人可以告诉我如何正确使用 smatch :: size() / code>和 smatch :: position()
以获取所有匹配的索引?解决方案 regex_search
的单次执行只能为您提供一个匹配(您的查询大小和位置)。
您可以:更改正则表达式以匹配子字符串多次(然后循环捕获组),或者只需使用 regex_iterator
string str =aaabxxxaab;
regex rx(ab);
vector< int> index_matches; //结果保存在这里
//(应该是{2,8},但总是只得到{2})
for(auto it = std :: sregex_iterator(str.begin ),str.end(),rx);
it!= std :: sregex_iterator();
++ it)
{
index_matches.push_back(it->位置());
}
在线演示:http://coliru.stacked-crooked.com/a/4d6e1a44b60b7da5
I just start to learn how to use regex
for string handling (C++11
new feature). Please pardon me if the following question is too silly.
Currently I apply the following code to get the index of all matches:
string str = "aaabxxxaab";
regex rx("ab");
vector<int> index_matches; // results saved here (should be {2, 8})
int track = 0;
smatch sm;
while (regex_search(str, sm, rx))
{
index_matches.push_back(track+sm.position());
string tmp = sm.suffix().str();
track += str.length() - tmp.length(); // update base index
str = tmp;
}
It works OK, but I have to update track
(base index) manually each time to make it work correctly.
At the same time, I noticed that there are already smatch::size()
and smatch::position()
, which I want to combine using to achieve the goal. The following is the code I want to combine them together but cannot work (i.e. always get only {2}
).
string str = "aaabxxxaab";
regex rx("ab");
vector<int> index_matches; // results saved here
// (should be {2, 8}, but always get only {2})
smatch sm;
regex_search(str, sm, rx);
for (int i=0; i<sm.size(); i++)
index_matches.push_back(sm.position(i));
Can someone tell me how to correctly use smatch::size()
and smatch::position()
to get all matched indexes?
解决方案 A single execution of regex_search
gives you only one match (whose size and position you queried).
You could: change your regex to match the substring more than once (and then loop over the capture groups), or just use regex_iterator
string str = "aaabxxxaab";
regex rx("ab");
vector<int> index_matches; // results saved here
// (should be {2, 8}, but always get only {2})
for(auto it = std::sregex_iterator(str.begin(), str.end(), rx);
it != std::sregex_iterator();
++it)
{
index_matches.push_back(it->position());
}
online demo: http://coliru.stacked-crooked.com/a/4d6e1a44b60b7da5
这篇关于使用regex_search获取所有匹配的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!