c++ - 通过 boost regex 替换行与相对复杂的 regex 的 boost

标签 c++ regex string boost

背景

我正在寻找替换文件中以 --[[: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/

相关文章:

c++ - Boost.序列化警告

c++ - 为什么 std::regex_iterator 会导致此数据的堆栈溢出?

python - 如何更改除这两个字符之外的所有字符?

r - 在 R 中提取一组短语后的单词/文本

c++ - 将 Rcpp 与 C 代码链接起来以进行自适应大都会拒绝采样

C++ char 数组与数组进行比较并分配单独的值;

c++ - 纯虚拟的备用参数列表不会继承(需要 "using")

regex - 需要帮助重写规则以使 url 干净且 seo 友好?

string - Net-SNMP:如何在 SET 命令中指定十六进制字节的八位字节字符串?

c++ - 是否有替代 std::string 子字符串的方法?