将字符串中的字符替换为其修改后的版本
Replacing characters by a modified version of them in a string
我想替换下面的字符(或&&在输入字符串中使用正则表达式替换
+ - ! ( ) { } [ ] ^ " ~ * ? : && ||
如何在std::regex的构造中编写这个请求?
例如,如果我有
"(1+1):2"
我想要一个输入:
"(1+1):2"
最后的代码看起来像这样:
std::string s ("(1+1):2");
std::regex e ("???"); // what should I put here ?
std::cout << std::regex_replace (s,e,"\$2"); // is this correct ?
您可以使用std::regex_replace
与capture:
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main() {
regex regex_a("(\+|-|!|\(|\)|\{|\}|\[|\]|\^|"|~|\*|\?|:|\\|&&|\|\|)");
cout << regex_replace("(1+1):2", regex_a, "\$0") << endl;
}
这个打印$ ./a.out
(1+1):2
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 导入库可以跨dll版本工作吗
- 独立读取-修改-写入顺序
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 当系统的卷被修改时,如何修改WASAPI环回捕获卷
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么我可以通过引用修改常量返回
- 对于结构,表达式必须是可修改的ivalue
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 在官方 ZeroMQ 多线程示例的修改版本中崩溃
- 是否有范围::视图::转换的可修改视图版本
- ImageMagick C 版本7在空白图像中修改像素值
- 标题修改在新鲜版本中不会生效
- 是否可以有一个零成本assert(),这样代码就不必在调试和发布版本之间进行修改
- 修改的SSE版本
- 将字符串中的字符替换为其修改后的版本
- 修改gcc/g++版本会导致段故障
- 有没有办法在不修改新版本的情况下(暂时)恢复旧的G++3.2版本
- visual NANT为C++项目修改程序集版本