C++ 修改正则表达式以包含逗号
我有以下字符串:C++ 修改正则表达式以包含逗号,c++,regex,boost,C++,Regex,Boost,我有以下字符串: arg1('value1') arg2('value '')2') arg3('user\'~!@#$%^&*_~!@#$%^&"*_-=+[{]}\|;:<.>?21') 上面的正则表达式正确地提取了这些值。但是当我包含逗号时,代码就失败了。例如: arg1('value1') arg2('value '')2') arg3('user\'~!@#$%^&*_~!@#$%^&"*_-=+[{]}\|;:<.>?21
arg1('value1') arg2('value '')2') arg3('user\'~!@#$%^&*_~!@#$%^&"*_-=+[{]}\|;:<.>?21')
上面的正则表达式正确地提取了这些值。但是当我包含逗号时,代码就失败了。例如:
arg1('value1') arg2('value '')2') arg3('user\'~!@#$%^&*_~!@#$%^&"*_-=+[{]}\|;:<.>?21**,**')
arg1('value1')arg2('value')2')arg3('user\'~!@$%^&*.!@$%^&“*.-=+[{]}\\\\\;:?21**,**”)
如何修改此正则表达式以包含逗号?
仅供参考。该值可以包含空格、特殊字符和制表符。代码采用CPP格式
提前谢谢。我不会在这里使用正则表达式 目标必须是解析值,毫无疑问,这些值将具有您需要解释的有用值 我会设计一个数据结构,如:
#include <map>
namespace Config {
using Key = std::string;
using Value = boost::variant<int, std::string, bool>;
using Setting = std::pair<Key, Value>;
using Settings = std::map<Key, Value>;
}
哪张照片
config setting {arg1, value1}
config setting {arg2, value ')2}
config setting {arg3, user'~!@#$%^&*_~!@#$%^&"*_-=+[{]}|;:<.>?21**,**}
config设置{arg1,value1}
配置设置{arg2,value')2}
配置设置{arg3,用户'~!@#$%^&*#~!@#$%^&“*#-=+[{]}|:?21**,**
现场演示
#include <boost/spirit/include/qi.hpp>
#include <map>
#include <boost/fusion/adapted/std_pair.hpp>
namespace Config {
using Key = std::string;
using Value = boost::variant<int, std::string, bool>;
using Setting = std::pair<Key, Value>;
using Settings = std::map<Key, Value>;
}
namespace Parser {
using It = std::string::const_iterator;
using namespace Config;
namespace qi = boost::spirit::qi;
using Skip = qi::blank_type;
qi::rule<It, std::string()> quoted_ = "'" >> *(
"'" >> qi::char_("'") // double ''
| '\\' >> qi::char_ // any character escaped
| ~qi::char_("'") // non-quotes
) >> "'";
qi::rule<It, Key()> key_ = +qi::char_("a-zA-Z0-9_"); // for example
qi::rule<It, Value()> value_ = qi::int_ | quoted_ | qi::bool_;
qi::rule<It, Setting(), Skip> setting_ = key_ >> '(' >> value_ >> ')';
qi::rule<It, Settings()> settings_ = qi::skip(qi::blank) [*setting_];
}
int main() {
std::string const input = R"( arg1('value1') arg2('value '')2') arg3('user\'~!@#$%^&*_~!@#$%^&"*_-=+[{]}\|;:<.>?21**,**'))";
Config::Settings map;
if (parse(input.begin(), input.end(), Parser::settings_, map)) {
for(auto& entry : map)
std::cout << "config setting {" << entry.first << ", " << entry.second << "}\n";
}
}
#include <boost/regex.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
#include <map>
namespace Config {
using Key = std::string;
using RawValue = std::string;
using Settings = std::map<Key, RawValue>;
Settings parse(std::string const& input) {
Settings settings;
boost::regex re(R"((\w+)\(('.*?')\))");
auto f = boost::make_regex_iterator(input, re);
for (auto& match : boost::make_iterator_range(f, {}))
settings.emplace(match[1].str(), match[2].str());
return settings;
}
}
int main() {
std::string const input = R"( arg1('value1') arg2('value '')2') arg3('user\'~!@#$%^&*_~!@#$%^&"*_-=+[{]}\|;:<.>?21**,**'))";
Config::Settings map = Config::parse(input);
for(auto& entry : map)
std::cout << "config setting {" << entry.first << ", " << entry.second << "}\n";
}
#包括
#包括
#包括
名称空间配置{
使用Key=std::string;
使用Value=boost::variant;
使用设置=std::pair;
使用设置=std::map;
}
名称空间分析器{
使用它=std::string::const_迭代器;
使用名称空间配置;
名称空间qi=boost::spirit::qi;
使用Skip=qi::blank_类型;
qi::引用规则\=“””>>*(
“'”>>qi::char_uuz(“”)//double”
|'\\'>>qi::char\//任何转义的字符
|~qi::字符(“”)//非引号
) >> "'";
qi::rule key=+qi::char_uuz(“a-zA-Z0-9”);//例如
qi::规则值=qi::int|quoted|qi::bool|;
qi::规则设置u=键>>”(“>>值>>”);
qi::规则设置\=qi::跳过(qi::空白)[*设置\];
}
int main(){
std::string const input=R“(arg1('value1')arg2('value'')2')arg3('user\'~!@$%^&*.!@$%^&“*.-=+[{]}\\\\;:?21**,**”);
配置::设置映射;
if(解析(input.begin(),input.end(),Parser::settings,map)){
用于(自动输入:地图(&E)
std::cout我不确定,你是否只是在re_arg_值((\('[^']*(?:''[^']*'[^]*))”
?@WiktorStribiżew,我更喜欢修改这个:boost::regex re_arg_值(“('[^']*(?:“[^']*)*)*”[^)]*)*);这不能包括逗号或闭括号。你能帮我一下吗?实际上,这是接受:arg('user()~!@#$%^&*.-=+[{]}\\\\\:2“请注意反斜杠。当您在常规字符串文字中使用它们时,必须使用两个反斜杠来定义文字反斜杠。现在,如果您不提供您正在测试的精确的小提琴/代码段(您将不断回写它不起作用),我将帮不了您多少忙。”如果您的字符串文字是用单反斜杠编写的,并且您没有使用原始字符串文字。您当前的('[^']*(?:'[^']*)*'[^]*))
模式与外括号不匹配,因为它现在是一个捕获组。您可以删除外括号,仍然可以得到相同的结果。添加了一个使用Boost正则表达式进行公平比较的
config setting {arg1, value1}
config setting {arg2, value ')2}
config setting {arg3, user'~!@#$%^&*_~!@#$%^&"*_-=+[{]}|;:<.>?21**,**}
#include <boost/spirit/include/qi.hpp>
#include <map>
#include <boost/fusion/adapted/std_pair.hpp>
namespace Config {
using Key = std::string;
using Value = boost::variant<int, std::string, bool>;
using Setting = std::pair<Key, Value>;
using Settings = std::map<Key, Value>;
}
namespace Parser {
using It = std::string::const_iterator;
using namespace Config;
namespace qi = boost::spirit::qi;
using Skip = qi::blank_type;
qi::rule<It, std::string()> quoted_ = "'" >> *(
"'" >> qi::char_("'") // double ''
| '\\' >> qi::char_ // any character escaped
| ~qi::char_("'") // non-quotes
) >> "'";
qi::rule<It, Key()> key_ = +qi::char_("a-zA-Z0-9_"); // for example
qi::rule<It, Value()> value_ = qi::int_ | quoted_ | qi::bool_;
qi::rule<It, Setting(), Skip> setting_ = key_ >> '(' >> value_ >> ')';
qi::rule<It, Settings()> settings_ = qi::skip(qi::blank) [*setting_];
}
int main() {
std::string const input = R"( arg1('value1') arg2('value '')2') arg3('user\'~!@#$%^&*_~!@#$%^&"*_-=+[{]}\|;:<.>?21**,**'))";
Config::Settings map;
if (parse(input.begin(), input.end(), Parser::settings_, map)) {
for(auto& entry : map)
std::cout << "config setting {" << entry.first << ", " << entry.second << "}\n";
}
}
#include <boost/regex.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
#include <map>
namespace Config {
using Key = std::string;
using RawValue = std::string;
using Settings = std::map<Key, RawValue>;
Settings parse(std::string const& input) {
Settings settings;
boost::regex re(R"((\w+)\(('.*?')\))");
auto f = boost::make_regex_iterator(input, re);
for (auto& match : boost::make_iterator_range(f, {}))
settings.emplace(match[1].str(), match[2].str());
return settings;
}
}
int main() {
std::string const input = R"( arg1('value1') arg2('value '')2') arg3('user\'~!@#$%^&*_~!@#$%^&"*_-=+[{]}\|;:<.>?21**,**'))";
Config::Settings map = Config::parse(input);
for(auto& entry : map)
std::cout << "config setting {" << entry.first << ", " << entry.second << "}\n";
}
config setting {arg1, 'value1'}
config setting {arg2, 'value ''}
config setting {arg3, 'user\'~!@#$%^&*_~!@#$%^&"*_-=+[{]}\|;:<.>?21**,**'}