Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在字符串的反面使用正则表达式?_C++_Regex_String_Reverse_Reverse Iterator - Fatal编程技术网

C++ 如何在字符串的反面使用正则表达式?

C++ 如何在字符串的反面使用正则表达式?,c++,regex,string,reverse,reverse-iterator,C++,Regex,String,Reverse,Reverse Iterator,我想在字符串的反面使用regex 我可以执行以下操作,但所有的sub\u匹配都是反向的: string foo("lorem ipsum"); match_results<string::reverse_iterator> sm; if (regex_match(foo.rbegin(), foo.rend(), sm, regex("(\\w+)\\s+(\\w+)"))) { cout << sm[1] << ' ' << sm[2]

我想在
字符串的反面使用
regex

我可以执行以下操作,但所有的
sub\u匹配都是反向的:

string foo("lorem ipsum");
match_results<string::reverse_iterator> sm;

if (regex_match(foo.rbegin(), foo.rend(), sm, regex("(\\w+)\\s+(\\w+)"))) {
    cout << sm[1] << ' ' << sm[2] << endl;
}
else {
    cout << "bad\n";
}
编辑:

我需要更新问题以澄清我想要的:


字符串
上向后运行
正则表达式
并不是要颠倒匹配项的查找顺序。
regex
要复杂得多,在这里发布会很有价值,但是向后运行它可以避免我需要提前查看。此问题是关于如何处理从
匹配结果中获得的子匹配。我需要能够将它们从输入中取出,这里是
foo
。我不想为每个子匹配构造一个临时的
字符串
并在其上运行
reverse
。如何避免这样做。

您可以颠倒使用结果的顺序:

#include <regex>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    string foo("lorem ipsum");
    smatch sm;

    if (regex_match(foo, sm, regex("(\\w+)\\s+(\\w+)"))) {
        cout << sm[2] << ' ' << sm[1] << endl; // use second as first
    }
    else {
        cout << "bad\n";
    }
}

这是绝对可能的!关键在于这样一个事实。由于
sub\u match
es将从以下位置获得:
match\u results sm
,因此
a
sub\u match
继承的元素将是
string::reverse\u迭代器
s

因此,对于
sm
中的任何给定
sub\u匹配
,您可以获得从
second.base()
first.base()的正向范围。您不必为流范围构造
字符串,但需要构造:

ostream\u迭代器输出(cout);
复制(sm[1].second.base(),sm[1].first.base(),output);
输出='';
复制(sm[2].second.base(),sm[2].first.base(),output);

尽管如此,振作起来,有一个更好的解决方案即将出现!讨论
string\u literal
s到目前为止还没有对它们采取任何行动,但它们已经进入了“进化子组”。

所以你想找到一个匹配项,然后颠倒匹配片段的词序,然后将其写入cout/where?@MichaelMcPherson我想在字符串的背面运行
regex
,是的。但我不希望我的比赛被逆转。。。如果可能的话。好的,那么您想对一个已逐字符反转的字符串运行正则表达式,然后输出常规(非反转)单词吗?无论哪种方式,这似乎都是一个难以解决的问题。如果有更多关于正则表达式或目标字符串的信息,人们可能会给你一个更好的答案。但是对于这里提供的数据(“一个字符串已被反转。我有n个反转形式的匹配项。如何取消反转?”)我认为运行反转是唯一的实际答案。如果您对输入字符串运行反转迭代器,您将得到反转的字符串匹配项。依我看,你有两个选择。a) 把火柴倒过来。b) 使用匹配中的反向迭代器,不管你想对它们做什么。这并不能实现我想要的。正则表达式比我在示例中发布的要复杂得多。事实上,如果没有regex库中的前瞻性支持,就无法实现这个regex。我可以通过向后迭代来解决一些复杂问题,但我不想为每个子匹配构造
string
s和
reverse
。@JonathanMee也许你可以尝试发布实际的正则表达式?或者至少是和你的问题一样棘手的东西?
#include <regex>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    string foo("lorem ipsum");
    smatch sm;

    if (regex_match(foo, sm, regex("(\\w+)\\s+(\\w+)"))) {
        cout << sm[2] << ' ' << sm[1] << endl; // use second as first
    }
    else {
        cout << "bad\n";
    }
}
ipsum lorem
ostream_iterator<char> output(cout);

copy(sm[1].second.base(), sm[1].first.base(), output);
output = ' ';
copy(sm[2].second.base(), sm[2].first.base(), output);