semantics of Perl’s ..
은 미묘합니다. 아래 코드는 ..
과 while (<>) { ... }
을 모두 에뮬레이트하여 -n
스위치를 perl
으로 변경합니다.
#include <fstream>
#include <iostream>
#include <regex>
#include <vector>
// emulate Perl's .. operator
void flipflop(bool& inside, const std::regex& start, const std::regex& end, const std::string& str)
{
if (!inside && std::regex_match(str, start))
inside = true;
else if (inside && std::regex_match(str, end))
inside = false;
}
int main(int argc, char *argv[])
{
// extra .* wrappers to use regex_match in order to work around
// problems with regex_search in GNU libstdc++
std::regex start(".*START.*"), end(".*END.*"), match(".*test.*");
for (const auto& path : std::vector<std::string>(argv + 1, argv + argc)) {
std::ifstream in(path);
std::string str;
bool inside = false;
int line = 0;
while (std::getline(in, str)) {
++line;
flipflop(inside, start, end, str);
if (inside && std::regex_match(str, match))
std::cout << path << ':' << line << ": " << str << '\n';
// Perl's .. becomes false AFTER the rhs goes false,
// so keep this last to allow match to succeed on the
// same line as end
flipflop(inside, start, end, str);
}
}
return 0;
}
예를 들어, 다음 입력을 고려하십시오.
test ERROR 1
START
test
END
test ERROR 2
START
foo ERROR 3
bar ERROR 4
test 1
baz ERROR 5
END
test ERROR 6
START sldkfjsdflkjsdflk
test 2
END
lksdjfdslkfj
START
dslfkjs
sdflksj
test 3
END dslkfjdsf
샘플 실행 :
$ ./extract.exe file
file:3: test
file:9: test 1
file:14: test 2
file:20: test 3
$ ./extract.exe file file
file:3: test
file:9: test 1
file:14: test 2
file:20: test 3
file:3: test
file:9: test 1
file:14: test 2
file:20: test 3
당신이 http://www.cplusplus.com/reference/regex/ 봤어? –
나는 줄 번호를 얻는 방법을 모른다. –
그럴 경우, 질문에 지금까지 가지고있는 C++을 추가하십시오. (또는 지금 어떻게 작성해야 할지를 적어 두십시오.) 그러면 훨씬 쉽게 응답 할 것입니다. –