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

2016-01-30 22:32:20 +08:00
 spencerqiu

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

奖励 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

1362 次点击
所在节点    C
20 条回复
phttc
2016-01-30 22:51:06 +08:00
第一反应就是正则匹配下。。。
spencerqiu
2016-01-30 22:54:58 +08:00
哦,对附加一条,输入文字里可能有汉字,不知道有没有干扰 ...

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

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

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

<script src="https://gist.github.com/virusdefender/f593cd34282c7876d433.js"></script>
virusdefender
2016-01-31 15:05:18 +08:00
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
2016-02-01 09:15:26 +08:00
grep+lookaround 就行的事,没事写什么代码?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/254523

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX