V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
spencerqiu
V2EX  ›  C

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

  •  
  •   spencerqiu · 2016-01-30 22:32:20 +08:00 · 1352 次点击
    这是一个创建于 3013 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    奖励 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 条回复    2016-02-01 09:15:26 +08:00
    phttc
        1
    phttc  
       2016-01-30 22:51:06 +08:00
    第一反应就是正则匹配下。。。
    spencerqiu
        2
    spencerqiu  
    OP
       2016-01-30 22:54:58 +08:00
    哦,对附加一条,输入文字里可能有汉字,不知道有没有干扰 ...

    不会再附加了...我知道你们痛恨改需求...
    zhjits
        3
    zhjits  
       2016-01-30 23:00:56 +08:00
    // 我没学过 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
       2016-01-30 23:07:03 +08:00
    @zhjits
    需要从文件里面读,是整个文件读到 EOF ,而不是只读一个字符串 ...

    下一条回复改完留下支付宝吧~
    spencerqiu
        5
    spencerqiu  
    OP
       2016-01-30 23:07:18 +08:00
    @spencerqiu
    输入文件名: sample.in
    zhjits
        6
    zhjits  
       2016-01-30 23:21:42 +08:00
    // 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  
       2016-01-30 23:24:37 +08:00
    @zhjits 这个 r2 是干嘛用的?
    zhjits
        8
    zhjits  
       2016-01-30 23:33:44 +08:00
    @skydiver Gist 里面已经删了。我说了我不会 C++ 啊……
    spencerqiu
        9
    spencerqiu  
    OP
       2016-01-30 23:36:25 +08:00
    @zhjits
    已转, Thx !
    htfy96
        10
    htfy96  
       2016-01-30 23:57:05 +08:00   ❤️ 1
    ilotuo
        11
    ilotuo  
       2016-01-31 00:03:19 +08:00
    好吧 本来想练手 结果搞了 45 分钟
    T.T
    基础太弱了
    icedx
        12
    icedx  
       2016-01-31 00:20:04 +08:00
    https://gist.github.com/anonymous/fa18c4e544d1b72bfca1

    泡杯面的时候写的 肯定有数组越界 就不管了
    Wonicon
        13
    Wonicon  
       2016-01-31 00:46:44 +08:00
    https://gist.github.com/Wonicon/3fcbb841701aaf16eb2d
    假定行缓冲足够大, answer=".*"不会换行......
    msg7086
        14
    msg7086  
       2016-01-31 01:02:06 +08:00 via Android
    又在重新发明 grep 了?
    j3399141
        15
    j3399141  
       2016-01-31 02:19:04 +08:00   ❤️ 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  
       2016-01-31 02:28:45 +08:00   ❤️ 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  
       2016-01-31 15:05:03 +08:00
    这种事就别麻烦 c++了吧

    <script src="https://gist.github.com/virusdefender/f593cd34282c7876d433.js"></script>
    sagnitude
        19
    sagnitude  
       2016-01-31 16:10:31 +08:00
    凑热闹来一个
    #include <stdlib.h>

    int main() {
    system("cat text.txt | grep -Po 'answer=\"\\K[^\"]*'");
    return 0;
    }
    ryd994
        20
    ryd994  
       2016-02-01 09:15:26 +08:00 via Android
    grep+lookaround 就行的事,没事写什么代码?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   841 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:24 · PVG 04:24 · LAX 13:24 · JFK 16:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.