本文介绍了如何获得std :: regex的所有可能匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到所有可能的正则表达式匹配,这怎么可能?

I would like to find all possible matches of regex, how is it possible?

regex rx("(2|25)");
string s = "2225";
for (sregex_iterator it(s.begin(), s.end(), rx), end; it != end; ++it) {
    cout << it->position() << ": " << it->str() << endl;
}

给出输出:

0: 2
1: 2
2: 25

但是找不到确切的第三个 2:2 .我更喜欢使用正则表达式,因为 O(n)的复杂性用于同时搜索多个令牌.

But can't find third 2: 2 exactly. I prefer to use regex because of O(n) complexity for searching several tokens at same time.

更新:

也许将令牌列表拆分为非前缀列表并创建多个正则表达式?例如:(2 | 4 | 25 | 45 | 251 | 455 | 267) => (2 | 4)(25 | 45 | 267)(251 | 455),这将使复杂性增加到 O(n log(m))

Maybe split token list to non-prefixable lists and create several regexes? For example: (2|4|25|45|251|455|267) => (2|4), (25|45|267), (251|455) This will grow complexity to something like O(n log(m))

更新2:

请提供基于STL的简短算法,将令牌向量拆分为非前缀向量,以回答此问题.

Please, provide short STL-based algorithm of splitting token vector to non-prefixable vectors to answer this question.

推荐答案

我认为使用迭代器和单个正则表达式是不可能的.这是它的工作方式.

I dont think it's possible with an iterator and a single regexp. Here's how it works.

您的正则表达式搜索子字符串为"2" "25".现在,您可以使用 sregex_iterator 开始搜索.它以字符串的第一个符号开头,并尝试查找与您的正则表达式匹配的内容.如果存在匹配项,则将其记录",并将迭代器前进到匹配项之后的位置.如果没有匹配项,则迭代器将前进1个位置.这个过程一直持续到到达字符串末尾为止.

Your regexp searches for a substring that is either "2" or "25". Now, you start the search with sregex_iterator. It starts with the first symbol of the string, and tries to find match with your regular expression. If there is a match, it is "recorded", and the iterator is advanced to the position after the match. If there is no match, the iterator is advanced 1 position forward. This process continues until the end of the string is reached.

现在,每次找到匹配项时,它都会尝试从您的正则表达式中找到最佳(即最长)匹配项.因此,如果子字符串同时匹配 2 25 ,则它将花费 25 ,因为它更长.所以我说你需要2个正则表达式.

Now, each time it finds a match it will try to find the best (i.e., longest) match from your regular expression. So if a substring matches both 2 and 25, it will take 25 since it's longer. So I'd say you need 2 regular expressions.

这篇关于如何获得std :: regex的所有可能匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 18:43
查看更多