[一杯咖啡求解决字符串操作问题] C++ 读入一个文件,搜索其中所有的 answer="foobar" 字段并输出其中所有 foobar - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
spencerqiu
V2EX    C

[一杯咖啡求解决字符串操作问题] C++ 读入一个文件,搜索其中所有的 answer="foobar" 字段并输出其中所有 foobar

  •  
  •   spencerqiu Jan 30, 2016 2127 views
    This topic created in 3739 days ago, the information mentioned may be changed or developed.

    高中狗,被家长禁写码好久,字符串完全撸不动啊 ...

    奖励 10 元,第一位成功解决的同学得 ...

    程序要能在 Linux 上运行 ...

    样例输入

    文件名: sample.in

    文件内容样例如下:
    <div class="row">
    <div class="span10">
    <p>Remember, the <strong>only thing</strong> before everything, is to have a dogma. A dogma is a list consists by DOs and DOnTanswer="C"s. You make it, stick to it while adapt it to reality, question yourself from time to time: is everything on the list mutuallanswer="A"y exclanswer="B"usive and collectively eanswer="B"xanswer="C"hausive?</p>

    <p>And there is a meta dogma for dogmas:</p>

    <ul>
    <li>Avoid emotional tactics</li>
    <li>Every kindness counts</li>
    <li>Gentleness is the ultimate strength</li>
    </ul>

    样例输出:

    直接输出到屏幕
    C
    A
    B
    B
    C

    20 replies    2016-02-01 09:15:26 +08:00
    phttc
        1
    phttc  
       Jan 30, 2016
    第一反应就是正则匹配下。。。
    spencerqiu
        2
    spencerqiu  
    OP
       Jan 30, 2016
    哦,对附加一条,输入文字里可能有汉字,不知道有没有干扰 ...

    不会再附加了...我知道你们痛恨改需求...
    zhjits
        3
    zhjits  
       Jan 30, 2016
    // 我没学过 C++,随手写的:

    #include <iostream>
    #include <regex>
    using namespace std;
    int main()
    {
    string var;

    std::cin >> var;

    const regex r("answer=\"(.+?)\"");
    const regex r2("\".*\"");
    smatch sm, sm2;

    auto params_it = std::sregex_iterator(var.cbegin(), var.cend(), r);
    auto params_end = std::sregex_iterator();

    while (params_it != params_end) {
    auto param = params_it->str();
    std::regex_match(param, sm, r);
    std::cout << sm[1] << std::endl;
    ++params_it;
    }

    return 0;
    }
    spencerqiu
        4
    spencerqiu  
    OP
       Jan 30, 2016
    @zhjits
    需要从文件里面读,是整个文件读到 EOF ,而不是只读一个字符串 ...

    下一条回复改完留下支付宝吧~
    spencerqiu
        5
    spencerqiu  
    OP
       Jan 30, 2016
    @spencerqiu
    输入文件名: sample.in
    zhjits
        6
    zhjits  
       Jan 30, 2016
    // https://gist.github.com/Jamesits/01abcb0e23b4a4ef53fc
    // 支付宝我写 gist 下面评论了自己找,找不到就算啦

    #include <iostream>
    #include <regex>
    #include <fstream>
    #include <string>
    using namespace std;
    int main() {

    std::ifstream ifs("sample.in");
    std::string var((std::istreambuf_iterator<char>(ifs)),
    (std::istreambuf_iterator<char>()));

    const regex r("answer=\"(.+?)\"");
    const regex r2("\".*\"");
    smatch sm, sm2;

    auto params_it = std::sregex_iterator(var.cbegin(), var.cend(), r);
    auto params_end = std::sregex_iterator();

    while (params_it != params_end) {
    auto param = params_it->str();
    std::regex_match(param, sm, r);
    std::cout << sm[1] << std::endl;
    ++params_it;
    }

    return 0;
    }
    skydiver
        7
    skydiver  
       Jan 30, 2016
    @zhjits 这个 r2 是干嘛用的?
    zhjits
        8
    zhjits  
       Jan 30, 2016
    @skydiver Gist 里面已经删了。我说了我不会 C++ 啊……
    spencerqiu
        9
    spencerqiu  
    OP
       Jan 30, 2016
    @zhjits
    已转, Thx !
    htfy96
        10
    htfy96  
       Jan 30, 2016   1
    ilotuo
        11
    ilotuo  
       Jan 31, 2016
    好吧 本来想练手 结果搞了 45 分钟
    T.T
    基础太弱了
    icedx
        12
    icedx  
       Jan 31, 2016
    https://gist.github.com/anonymous/fa18c4e544d1b72bfca1

    泡杯面的时候写的 肯定有数组越界 就不管了
    Wonicon
        13
    Wonicon  
       Jan 31, 2016
    https://gist.github.com/Wonicon/3fcbb841701aaf16eb2d
    假定行缓冲足够大, answer=".*"不会换行......
    msg7086
        14
    msg7086  
       Jan 31, 2016 via Android
    又在重新发明 grep 了?
    j3399141
        15
    j3399141  
       Jan 31, 2016   1
    std::fstream file("sample.in.txt", std::fstream::in);
    std::stringstream stream;
    stream << file.rdbuf();
    file.close();

    std::string input = stream.str();

    std::regex pattern("answer=\"(.*?)\"");

    std::sregex_token_iterator next(std::begin(input), std::end(input), pattern, 1);
    std::sregex_token_iterator end;

    while (next != end)
    {
    std::cout << *next++ << std::endl;
    }
    j3399141
        16
    j3399141  
       Jan 31, 2016   1
    /*
    需求随便改
    */
    #include <iostream>
    #include <regex>
    #include <fstream>
    #include <sstream>

    int main(void)
    {
    try
    {
    std::fstream file("sample.in.txt", std::fstream::in);
    std::stringstream stream;
    stream << file.rdbuf();
    file.close();

    std::string input = stream.str();

    std::regex pattern("answer=\"(.*?)\"");

    std::sregex_token_iterator next(std::begin(input), std::end(input), pattern, 1);
    std::sregex_token_iterator end;

    std::vector<std::string> result;
    while (next != end)
    {
    result.push_back(*next++);
    }

    copy(std::begin(result), std::end(result), std::ostream_iterator<std::string>(std::cout, "\n"));
    std::vector<std::string>().swap(result);
    }
    catch (const std::exception& exception)
    {
    std::cerr << exception.what() << std::endl;
    }

    system("PAUSE");
    return EXIT_SUCCESS;
    }
    virusdefender
        17
    virusdefender  
       Jan 31, 2016
    这种事就别麻烦 c++了吧

    <script src="https://gist.github.com/virusdefender/f593cd34282c7876d433.js"></script>
    sagnitude
        19
    sagnitude  
       Jan 31, 2016
    凑热闹来一个
    #include <stdlib.h>

    int main() {
    system("cat text.txt | grep -Po 'answer=\"\\K[^\"]*'");
    return 0;
    }
    ryd994
        20
    ryd994  
       Feb 1, 2016 via Android
    grep+lookaround 就行的事,没事写什么代码?
    About     Help     Advertise     Blog     API     FAQ     Solana     4422 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 55ms UTC 04:09 PVG 12:09 LAX 21:09 JFK 00:09
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86