首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >升压精神分析

升压精神分析
EN

Stack Overflow用户
提问于 2011-12-08 10:54:11
回答 1查看 891关注 0票数 1

我正在尝试取一个字符串,然后从它中检索所需的元素。目前,我似乎无法找到这样做的方法,因为输入字符串会不时变化,尽管元素不会。

到目前为止,我的代码如下。我想要做的是在解析时从字符串中提取'.V/'

到目前为止,我的代码还可以工作,但是我需要它更通用,因为在输入字符串中有许多元素。

  • i.e .

.v/ER/12/FRG/45S/16 FRG .E/45/SHAM/CAMP/2

我需要检索.V/.E/

代码语言:javascript
复制
std::vector<std::string>elements;
std::string input = ".V/ER/12/FRG/45S/16JAN ";

bool result = qi::parse(input.begin(),input.end(),
        *(*(qi::char_ - " /ER/12/FRG/45S/16JAN\n") >> " /ER/12/FRG/45S/16JAN\n"),
        elements
        );  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-08 11:02:56

我真的建议对这项工作使用正则表达式(boost regex或std::regex)。

正则表达式可能看起来像

代码语言:javascript
复制
std::regex re("^(\\.[EV]/).*?$"); // the first submatch is the part you are looking for

这里有一点精神,以防你真的需要它:

代码语言:javascript
复制
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;

typedef std::string::const_iterator It;

int main()
{
    std::vector<std::string>elements;
    std::string input = 
        ".V/ER/12/FRG/45S/16JAN\n"
        ".E/45/SHAM/CAMP/2";

    It first(input.begin()), last(input.end());

    bool ok = qi::parse(first, last,
            (
              '.' > qi::char_("EV") > '/' 
                 >> qi::omit [ *(qi::char_ - qi::eol) ] 
            ) % qi::eol,
            elements);

    if (ok)
    {
        for (int i=0; i<elements.size(); ++i)
            std::cout << elements[i] << std::endl;
    } else
    {
        std::cerr << "Parse failed at '" << std::string(first, last) << std::endl;
    }

}

这将输出

代码语言:javascript
复制
V
E

如果你想展示“.e/”,有很多种方法。

代码语言:javascript
复制
bool ok = qi::parse(first, last,
        (
          (qi::char_('.') > qi::char_("EV") > qi::char_('/' )
             >> qi::omit [ *(qi::char_ - qi::eol) ] )
        ) % qi::eol,
        elements);

产出:

代码语言:javascript
复制
.V/
.E/

奖金

要显示如何包含行的“尾部”,可能需要将其存储到地图中:

代码语言:javascript
复制
#include <map>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
namespace qi = boost::spirit::qi;

typedef std::string::const_iterator It;

int main()
{
    typedef std::map<std::string, std::string> result_t;
    // or use a list to allow duplicate keys:
    // typedef std::list<std::pair<std::string, std::string> > result_t;
    result_t mappings;

    std::string input = 
        ".V/ER/12/FRG/45S/16JAN\n"
        ".E/45/SHAM/CAMP/2";

    It first(input.begin()), last(input.end());

    bool ok = qi::parse(first, last, (
                  qi::raw [ '.' > qi::char_("EV") > '/' ]
                > qi::raw [ *(qi::char_ - qi::eol) ]
            ) % qi::eol,
            mappings);

    if (ok)
    {
        for (result_t::const_iterator it=mappings.begin();
             it!=mappings.end(); ++it)
        {
            std::cout << it->first << " maps to " << it->second << std::endl;
        }
    } else
    {
        std::cerr << "Parse failed at '" << std::string(first, last) << std::endl;
    }

}

会输出

代码语言:javascript
复制
.E/ maps to 45/SHAM/CAMP/2
.V/ maps to ER/12/FRG/45S/16JAN
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8429824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档