getting between square bracket having specific characteristic using Boost regex in C++ not getting answer

StackOverflow https://stackoverflow.com/questions/13934998

  •  10-12-2021
  •  | 
  •  

I have a string like this:

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

and I want to get :

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

I want to take double square brackets having | and take out things that are before | and replace it with whole double square bracket thing and not replace any double square bracket not having | using Boost Regex.

Any help will be appreciated.

有帮助吗?

解决方案

Just use the look-ahead, (?!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';
}

demo: http://liveworkspace.org/code/2Mu5cN

其他提示

"\\[\\[(.*?)\|.*?]]"

This will match text between [[ and ]] with a | separator. $1 is the text between the [[ and the |.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top