捕获组中只有1个匹配标准C++;正则表达式和PCRE 我有一个大问题,我似乎解决不了,我正在尝试解析C++中由uWebSosik检索的Frand数据。我已经决定使用正则表达式来实现这一点。在尝试使用C++标准函数后,它根本不起作用,大约花费了5分钟来运行。在尝试用其他语言运行正则表达式之后,我觉得问题是C++(或JavaScript)不允许在捕获组中回溯,因为它在任何其他语言中都很好。切换到PCRE允许进行1次匹配(检索结果的速度大约快10倍),但其余的仍然是空的。您可以在我上面或此处发送的链接中看到PCRE(v2和v1)按预期工作:
以下是一个充分说明问题的示例:捕获组中只有1个匹配标准C++;正则表达式和PCRE 我有一个大问题,我似乎解决不了,我正在尝试解析C++中由uWebSosik检索的Frand数据。我已经决定使用正则表达式来实现这一点。在尝试使用C++标准函数后,它根本不起作用,大约花费了5分钟来运行。在尝试用其他语言运行正则表达式之后,我觉得问题是C++(或JavaScript)不允许在捕获组中回溯,因为它在任何其他语言中都很好。切换到PCRE允许进行1次匹配(检索结果的速度大约快10倍),但其余的仍然是空的。您可以在我上面或此处发送的链接中看到PCRE(v2和v1)按预期工作:,c++,regex,pcre,C++,Regex,Pcre,以下是一个充分说明问题的示例: #include <pcrecpp.h> #include <iostream> int main() { std::string contents = "--------------------------eba4d02620bdb4f6\nContent-Disposition: form-data; name=\"ZIP\"; filename=\"h.png\"\nCont
#include <pcrecpp.h>
#include <iostream>
int main() {
std::string contents = "--------------------------eba4d02620bdb4f6\nContent-Disposition: form-data; name=\"ZIP\"; filename=\"h.png\"\nContent-Type: image/png\n\n--------------------------8c078fed966ff6fe\nContent-Disposition: form-data; name=\"ZIP\"; filename=\"tree-pack.xml\"\nContent-Type: application/xml\n\n<?xml version=\"1.0\"?>\n<Packages>\n <Individual name=\"Designer\">\n <Name>Designer</Name>\n <Description>A BrAIn-API add-on that adds routes to help people design. This makes routes to generate colour palettes, generates fonts and even send previews of those to show how they look.</Description>\n <ID></ID>\n <FilePath>/packages/ID</FilePath>\n </Individual>\n</Packages>\n\n--------------------------8c078fed966ff6fe--\n\n--------------------------eba4d02620bdb4f6--\n";
pcrecpp::RE reg("-+.+\\nContent-Disposition: form-data; name=\"(\\w+| +)\"; filename=\"(.+)\"\\nContent-Type: (\\w+\\/\\w+)\\n\\n((.|\\n)+)\\n-+.+--.+|\\n+", pcrecpp::RE_Options()
.set_caseless(true)
.set_multiline(true));
pcrecpp::StringPiece input(contents);
int count = 0;
std::string match;
std::cout << contents << std::endl;
while (reg.FindAndConsume(&input, &match)) { //This while loop makes sure that it only logs the amount of matches it is able to find; giving it a defined amount of matches it needs to find has the same output.
count++;
std::cout << count << " " << match << std::endl;
}
}
如果您对已经解析formdata的库或任何东西有任何建议,我也很乐意听听
感谢阅读并提前感谢我能得到的所有帮助 没有理由在第一次出现后停止
(.|\\n)+
。从技术上讲,它表示匹配所有内容,直到输入结束。此外,为此使用适当的多部分/表单数据解析器也是谨慎的。可能会起作用。正如regex101.com上的调试器所示,我认为它试图在返回测试前一个正则表达式之前测试正则表达式中的下一个字符。另外,如果是这样的话,那么我看不出它如何能够为name获取正确的值,但为filename获取任何值,如果是这样的话,我希望它能够正确地检索名称、文件名和内容类型,但除此之外的所有内容都不能,事实并非如此。
Content-Disposition: form-data; name="ZIP"; filename="h.png"
Content-Type: image/png
--------------------------8c078fed966ff6fe
Content-Disposition: form-data; name="ZIP"; filename="tree-pack.xml"
Content-Type: application/xml
<?xml version="1.0"?>
<Packages>
<Individual name="Designer">
<Name>Designer</Name>
<Description>A BrAIn-API add-on that adds routes to help people design. This makes routes to generate colour palettes, generates fonts and even send previews of those to show how they look.</Description>
<ID></ID>
<FilePath>/packages/ID</FilePath>
</Individual>
</Packages>
--------------------------8c078fed966ff6fe--
--------------------------eba4d02620bdb4f6--
1 ZIP
2