I have string in which tags like this comes(there are multiple such tags)

|{{nts|-2605.2348}}

I want to use boost regex to remove |{{nts| and }} and replace whole string that i have typed above with

-2605.2348

in original string

To make it more clear:

Suppose string is:

 number is |{{nts|-2605.2348}}

I want string as:

 number is -2605.2348

I am quite new to boost regex and read many things online but not able to get answer to this any help would be appreciated

有帮助吗?

解决方案

It really depends on how specific do you want to be. Do you want to always remove exactly |{{nts|, or do you want to remove pipe, followed by {{, followed by any number of letters, followed by pipe? Or do you want to remove everything that isn't whitespace between the last space and the first part of the number?

One of the many ways to do this would be something like:

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

int main()
{
    std::string str = "number is |{{nts|-2605.2348}}";
    boost::regex re("\\|[^-\\d.]*(-?[\\d.]*)\\}\\}");
    std::cout << regex_replace(str, re, "$1") << '\n';
}

online demo: http://liveworkspace.org/code/2B290X

However, since you're using boost, consider the much simpler and faster parsers generated by boost.spirit.

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