创建正则表达式以提升"nam12 = 34.24"

create a regular expression to "nam12 = 34.24" boost

本文关键字:nam12 正则表达式 创建      更新时间:2023-10-16

请帮助创建一个正则表达式"nam12 = 34.24"

boost::regex regex();
boost::cmatch result;
std::string identifier;
std::string value;
if (boost::regex_match(assign.c_str(), result, regex))
{
    identifier = std::string(result[1].first, result[1].second);
    value = std::string(result[2].first, result[2].second);
}

我不知道是不是太过了,但我喜欢用Spirit来完成这样的简单任务:

#include <boost/spirit/include/qi.hpp>
int main() {
    std::string input;
    while (std::getline(std::cin, input)) 
    {
        std::string name;
        double value;
        using namespace boost::spirit::qi;
        if (phrase_parse(input.begin(), input.end(), lexeme[+alnum] >> '=' >> double_, space, name, value))
            std::cout << "Parsed: name = '" << name << "' and value = " << value << "n";
    }
}

打印。

Parsed: name = 'nam12' and value = 34.24
Parsed: name = 'nam56' and value = 43.65

看它Live On Wandbox