我想删除标有--
或[:space:]{1,}--
的所有行和换行符,并返回一行文件,例如下面的示例数据
-- Query taken from:
-- some stack page http://www.blablabla.somethingelse.com
SELECT country.name as country, country.headofstate
from country
-- Worst place to add comment ever
-- Here is even uglier
inner join city on city.id = country.capital
where city.population > 100000
-- this comment makes no sense here and would break sql parser buyt hey
and country.headofstate like 'A%'
-- WOW!
返回的文本应对应于:
SELECT country.name as country, country.headofstate from country inner join city on city.id = country.capital where city.population > 100000 and country.headofstate like 'A%'
尝试使用以下代码编译以下代码时
clang++ -Wall -std=c++11 line_skip.cpp -o lineskip && ./lineskip tst.sql
我收到错误消息:
ndefined symbols for architecture x86_64:
"boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)", referenced from:
boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int) in line_skip-771400.o
ld: symbol(s) not found for architecture x86_64
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)
/*
* Trivial example of a simple file skipping lines. The lines to be skipped
* should have ## as first non-blank characters everything else should be later
* returned as one line text.
*
* To copy (test and run):
* clang++ -Wall -std=c++11 line_skip.cpp -o lineskip && ./lineskip tst.sql
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <boost/regex.hpp>
#include <boost/algorithm/string/replace.hpp>
using namespace std;
int main(int argc, char *argv[]) {
cout << "Reading file: " << argv[1] << endl;
// Read file to stringstream, as per:
// https://stackoverflow.com/a/132394/1655567
std::ifstream file( argv[1] );
if ( file )
{
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
// Declare string variable to loop trhough and delete carriage returns
std::string readText;
readText = buffer.str();
// Regular expressions to pass to replace_all to remove all comments and line breaks
boost::regex re_comment("^(\t{1,}|[:space:]{1,}|)--.*(\n|\r|$)");
boost::regex re_lineend("(\n|\r)");
// Remove line breaks and return clean string
boost::replace_all(readText, re_comment, " ");
boost::replace_all(readText, re_lineend, " ");
// Show current text
cout << "Text with no spaces and no comments:\n" << readText << endl;
return 0;
}
}