[英]Using Boost C++ library to do a regex replace with a custom replacement
[英]replace string through regex using boost C++
我有這樣的標簽在其中的字符串(有多個這樣的標簽)
|{{nts|-2605.2348}}
我想使用boost正則表達式刪除|{{nts|
和}}
並替換為我在上面輸入的整個字符串
-2605.2348
用原始字符串
為了更清楚一點:
假設字符串是:
number is |{{nts|-2605.2348}}
我想要字符串為:
number is -2605.2348
我是新手,可以提高正則表達式並在線閱讀許多內容,但是無法獲得解答,我們將不勝感激
這實際上取決於您想要的具體程度。 是否要始終精確刪除|{{nts|
,還是要刪除管道,然后刪除{{
,然后刪除任意數量的字母,然后刪除管道? 還是要刪除數字的最后一個空格和第一部分之間不是空格的所有內容?
執行此操作的許多方法之一是:
#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';
}
在線演示: http : //liveworkspace.org/code/2B290X
但是,由於您正在使用boost,因此請考慮boost.spirit生成的更加簡單快捷的解析器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.