c++ - 在 C++ 中使用 Boost 正则表达式进入具有特定特征的方括号之间没有得到答案

标签 c++ regex boost boost-regex

我有这样一个字符串:

This is a link [[abcd 1234|xyz 1234]]  [[India]] [[abcd 1234|xyz 1234]]

我想得到:

This is a link abcd 1234 [[India]] abcd 1234

我想用双方括号 |并取出之前的东西 |并用整个双方括号替换它,而不是替换任何没有 | 的双方括号使用 Boost 正则表达式。

我们将不胜感激。

最佳答案

只需使用前瞻,(?!pattern)

#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main()
{
    std::string str = "This is a link [[abcd 1234|xyz 1234]]  [[India]] [[abcd 1234|xyz 1234]]";
    boost::regex re("\\[\\[(((?!\\]\\]).)+)\\|.*?]]");
    std::cout << boost::regex_replace(str, re, "$1") << '\n';
}

演示:http://liveworkspace.org/code/2Mu5cN

关于c++ - 在 C++ 中使用 Boost 正则表达式进入具有特定特征的方括号之间没有得到答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13934998/

相关文章:

c++ - 如何知道 Boost asio 中 SSL 套接字的状态

c++ - 调用程序本身的 LoadLibrary

c++ - 线程安全计数器c++11

c++ - ISO C++11 不允许从字符串转换为字符

javascript - 忽略 Javascript 正则表达式模式中的空格?

regex - 根据 sed 的反向引用将字符串替换为另一个字符串

c++ - 我如何检测拖过我的窗口的文件

用于验证日期格式掩码的正则表达式

c++ - 带有 boost::asio 的 ThreadPool 不退出?

c++ - boost::multi_array View 和子数组之间有什么区别