背景
我正在寻找替换文件中以 --
或 [[:space:]]{ 开头的所有行1、
。从广义上讲,我希望获得类似于 this answer 的结果。 .
代码
/*
* so_question.cpp
* Read text files and remove all lines starting with -- or <space>*--
* Clean text is passed to cout.
*
* Compile and test:
* clang++ -lboost_regex -Wall -std=c++11 so_question.cpp -o so_question && ./so_question tst.sql
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <boost/regex.hpp>
#include <boost/algorithm/string/replace.hpp>
int main(int argc, char *argv[]) {
// Read file to stringstream
std::ifstream file( argv[1] );
if ( file )
{
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
// Create a string variable to apply boost::regex
std::string readText;
readText = buffer.str();
// Regular expression finding comments
boost::regex re_comment("^([[:space:]]{1,}|)--.*(\n|\r|)$");
// Replace desired lines
// boost::replace_all(readText, re_comment, " ");
// Replace via regex replace
std::string result = boost::regex_replace(readText, re_comment, " ");
// Show clean text when using regex_replace
std::cout << "\nClean text:\n" << result << std::endl;
// Show clean text
// std::cout << "Clean text:" << readText << std::endl;
return 0;
}
}
测试数据
-- Query taken from:
-- https://stackoverflow.com/a/12467388/1655567
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++ -lboost_regex -Wall -std=c++11 so_question.cpp -o so_question && ./so_question tst.sql
问题
返回的文本与提供的文件中的完全相同。我认为问题出在我使用的特定正则表达式语法上。然而看完boost regex documentation和测试multiple versions关于那个正则表达式,我不清楚正确的语法应该是什么。
- ALE指出
#includes
没有正确排序。考虑 llvm guidelines什么是正确的顺序(这是旁白,与主要问题无关)?
最佳答案
重新排列括号:
(?m)^(?:--|[[:space:]]+).+
和see a demo on regex101.com .请注意,[[:space:]]{1,}
与 [[:space:]]+
相同。
关于c++ - 通过 boost regex 替换行与相对复杂的 regex 的 boost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48238054/