修改正则表达式以包含逗号 [英] modify regex to include comma
问题描述
我有以下字符串:
arg1('value1') arg2('value '')2') arg3('user\'~!@#$%^&*_~!@#$%^&"*_-=+[{]}\|;:<.>?21')
用于提取值的正则表达式如下:
The regex to extract the value looks like:
boost::regex re_arg_values("('[^']*(?:''[^']*)*'[^)]*)");
上面的正则表达式正确地提取了值.但是,当我包含逗号时,代码将失败.例如:
The above regex properly extracts the values. BUT when I include a comma , the code fails. For eg:
arg1('value1') arg2('value '')2') arg3('user\'~!@#$%^&*_~!@#$%^&"*_-=+[{]}\|;:<.>?21**,**')
如何修改此正则表达式以包含逗号? 供参考.该值可以包含空格,特殊字符以及制表符.该代码在CPP中.
How shall I modify this regex to include the comma? FYI. The value can contain spaces, special characters, and also tabs. The code is in CPP.
谢谢.
推荐答案
在这里我不使用正则表达式.
I'd not use a regex here.
目标必须是解析值,并且毫无疑问,它们将具有您需要解释的有用值.
The goal MUST be to parse values, and no doubt they will have useful values, that you need interpreted.
我会设计一个像这样的数据结构:
I'd devise a datastructure like:
#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>;
}
为此,您可以使用Boost Spirit编写1:1解析器:
For this you can write 1:1 a parser using Boost Spirit:
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
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_];
}
请注意如何
- 正确解释非字符串值
- 指定键的外观并对其进行解析
- 解释字符串转义,因此在映射中的
Value
包含转义后的真实"字符串 - 忽略空白值以外的空格(如果您也希望将换行符也视为空白,请使用
space_type
)
- interprets non-string values correctly
- specifies what keys look like and parses them too
- interprets string escapes, so the
Value
in the map contains the "real" string, after un-escaping - ignores whitespace outside values (use
space_type
if you want to ignore newlines as whitespace as well)
您可以像这样使用它:
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";
}
}
哪些印刷品
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";
}
}
奖金
为了进行比较,这是相同"但使用正则表达式:
BONUS
For comparison, here's the "same" but using regex:
#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**,**'}
注意:
- 它不再解释和转换任何值
- 它不再处理转义符
- 它需要对boost_regex的其他运行时库依赖性
这篇关于修改正则表达式以包含逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!