
高中狗,被家长禁写码好久,字符串完全撸不动啊 ...
奖励 10 元,第一位成功解决的同学得 ...
程序要能在 Linux 上运行 ...
文件内容样例如下:
<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
1 phttc Jan 30, 2016 第一反应就是正则匹配下。。。 |
2 spencerqiu OP 哦,对附加一条,输入文字里可能有汉字,不知道有没有干扰 ... 不会再附加了...我知道你们痛恨改需求... |
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; } |
4 spencerqiu OP |
5 spencerqiu OP |
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; } |
9 spencerqiu OP @zhjits 已转, Thx ! |
10 htfy96 Jan 30, 2016 |
11 ilotuo Jan 31, 2016 好吧 本来想练手 结果搞了 45 分钟 T.T 基础太弱了 |
12 icedx Jan 31, 2016 |
13 Wonicon Jan 31, 2016 https://gist.github.com/Wonicon/3fcbb841701aaf16eb2d 假定行缓冲足够大, answer=".*"不会换行...... |
14 msg7086 Jan 31, 2016 via Android 又在重新发明 grep 了? |
15 j3399141 Jan 31, 2016 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; } |
16 j3399141 Jan 31, 2016 /* 需求随便改 */ #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; } |
17 virusdefender Jan 31, 2016 |
19 sagnitude Jan 31, 2016 凑热闹来一个 #include <stdlib.h> int main() { system("cat text.txt | grep -Po 'answer=\"\\K[^\"]*'"); return 0; } |
20 ryd994 Feb 1, 2016 via Android grep+lookaround 就行的事,没事写什么代码? |