C++ 在字符串正则表达式c+中查找模式+;
我有以下字符串:C++ 在字符串正则表达式c+中查找模式+;,c++,regex,boost,C++,Regex,Boost,我有以下字符串:E:\501\u Document\u 60\u 1\u R.xml 我正在尝试查找模式“\u R” 我正在使用以下命令:boost::regex rgx(“[R]”) 但它不起作用:“空匹配” 多谢各位 代码: vector findMono(字符串s) { 向量向量机; boost::regex rgx(“[R]”); 小比赛; sregex_迭代器begin{s.begin(),s.end(),rgx}, 结束{}; for(boost::sregex_迭代器&i=begi
E:\501\u Document\u 60\u 1\u R.xml
我正在尝试查找模式“\u R”
我正在使用以下命令:boost::regex rgx(“[R]”)代码>
但它不起作用:“空匹配”
多谢各位
代码:
vector findMono(字符串s)
{
向量向量机;
boost::regex rgx(“[R]”);
小比赛;
sregex_迭代器begin{s.begin(),s.end(),rgx},
结束{};
for(boost::sregex_迭代器&i=begin;i!=end;++i)
{
boost::smatch m=*i;
向量推回(m.str());
}
返回向量;
}
内部维护()
{
向量m=findMono(“E:\501_Document_60_1_R.xml”);
如果(m.size()>0)不能像我们在评论中讨论的那样,“\R”
将在技术上适用于给定当前数据集的正则表达式
但是,在其他地方,如果你的路径包含了序列<代码>“yr”<代码>,我会强烈地考虑一些更复杂的问题。很容易保护自己不受这个问题的影响,这是一个很好的通用实践,并且将来很可能避免bug。
下面是一个非常基本的工作示例:
#include <iostream>
#include <string>
#include <vector>
#include <boost/regex.hpp>
std::vector<std::string> findMono(const std::string& path)
{
boost::regex rgx("_R");
boost::sregex_iterator begin {path.begin(), path.end(), rgx}, end {};
std::vector<std::string> matches;
for (boost::sregex_iterator& i = begin; i != end; ++i) {
matches.push_back((*i).str());
}
return matches;
}
int main(int argc, char * argv[])
{
const std::string path = "E:\\501_Document_60_1_R.xml";
const std::vector<std::string>& matches = findMono(path);
for (const auto& match : matches) {
std::cout << match << std::endl;
}
return 0;
}
#包括
#包括
#包括
#包括
std::vector findMono(const std::string&path)
{
boost::regex rgx(“_R”);
sregx_迭代器begin{path.begin(),path.end(),rgx},end{};
向量匹配;
for(boost::sregex_迭代器&i=begin;i!=end;++i){
匹配。推回((*i.str());
}
返回比赛;
}
int main(int argc,char*argv[])
{
const std::string path=“E:\\501\u Document\u 60\u 1\u R.xml”;
const std::vector&matches=findMono(路径);
用于(常量自动匹配:匹配){
Std::CUDE定义<代码>它不工作./Calp>你是否得到一个错误?空匹配?你确信代码> String S < /Cord>包含你认为它的路径吗?当我测试它时,我匹配<代码> R>代码>。是的,字符串包含路径。我同意PP关于改变正则表达式,但是考虑“Yr”的情况。发生在路径的其余部分(例如,“E:\501_Document_revieved_61_2_R.xml”
。在这种情况下,您将获得多个匹配项,因此您可能希望将正则表达式更改为更复杂的形式。@HaniGoc,运行此命令:#include#include#include使用std::vector;使用std::string;vector findMono(const string&s){使用名称空间boost;regex rgx(“[R]”);sregex_迭代器begin{s.begin(),s.end(),rgx},end{};vector rt;for(sregex_迭代器&i=begin;i!=end;+++i){rt.push_back((*i.str());}返回rt;}int main(int argc,char*argv[]){const vector&ms=findMono('E:\\501_Document 60_1_R.xml)}for(const auto m&ms){std::cout
#include <iostream>
#include <string>
#include <vector>
#include <boost/regex.hpp>
std::vector<std::string> findMono(const std::string& path)
{
boost::regex rgx("_R");
boost::sregex_iterator begin {path.begin(), path.end(), rgx}, end {};
std::vector<std::string> matches;
for (boost::sregex_iterator& i = begin; i != end; ++i) {
matches.push_back((*i).str());
}
return matches;
}
int main(int argc, char * argv[])
{
const std::string path = "E:\\501_Document_60_1_R.xml";
const std::vector<std::string>& matches = findMono(path);
for (const auto& match : matches) {
std::cout << match << std::endl;
}
return 0;
}