高中狗,被家长禁写码好久,字符串完全撸不动啊 ...
奖励 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 2016-01-30 22:51:06 +08:00
第一反应就是正则匹配下。。。
|
2
spencerqiu OP 哦,对附加一条,输入文字里可能有汉字,不知道有没有干扰 ...
不会再附加了...我知道你们痛恨改需求... |
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; } |
4
spencerqiu OP |
5
spencerqiu OP |
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; } |
9
spencerqiu OP @zhjits
已转, Thx ! |
10
htfy96 2016-01-30 23:57:05 +08:00 1
|
11
ilotuo 2016-01-31 00:03:19 +08:00
好吧 本来想练手 结果搞了 45 分钟
T.T 基础太弱了 |
12
icedx 2016-01-31 00:20:04 +08:00
|
13
Wonicon 2016-01-31 00:46:44 +08:00
https://gist.github.com/Wonicon/3fcbb841701aaf16eb2d
假定行缓冲足够大, answer=".*"不会换行...... |
14
msg7086 2016-01-31 01:02:06 +08:00 via Android
又在重新发明 grep 了?
|
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; } |
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; } |
17
virusdefender 2016-01-31 15:05:03 +08:00
|
18
virusdefender 2016-01-31 15:05:18 +08:00
|
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; } |
20
ryd994 2016-02-01 09:15:26 +08:00 via Android
grep+lookaround 就行的事,没事写什么代码?
|