加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C 11正则表达式:在替换字符串中捕获组后的数字

发布时间:2020-12-16 05:21:37 所属栏目:百科 来源:网络整理
导读:我的 regex_replace表达式在替换字符串中的’0’字符之前使用组$1,如下所示: #include iostream#include string#include regexusing namespace std;int main() { regex regex_a( "(.*)bar(.*)" ); cout regex_replace( "foobar0x1",regex_a,"$10xNUM" ) end
我的 regex_replace表达式在替换字符串中的’0’字符之前使用组$1,如下所示:
#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main() {
    regex regex_a( "(.*)bar(.*)" );
    cout << regex_replace( "foobar0x1",regex_a,"$10xNUM" ) << endl;
    cout << regex_replace( "foobar0x1","$1 0xNUM" ) << endl;
}

输出为:

xNUM
foo 0xNUM

我试图获得输出foo0xNUM没有中间的空格.

如何从替换字符串中的下一个字符保护组名称$1?

解决方法

您可以指定$n或$nn来引用捕获的文本,因此您可以使用$nn格式(这里为$01)来避免抓取0.
cout << regex_replace( "foobar0x1","$010xNUM" ) << endl;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读