std::正则表达式转义"+"符号
std::regex escape "+" sign
我不明白如何使用正则表达式单词进行转义。 我尝试检测"+"。我知道这也是正则表达式的一个特殊标志,表明后面有一个或多个标志。
据我了解,这些特殊符号需要用"\"转义。对于 +,这似乎适用于"." 但是,如果我用"+ "转义加号,我会得到运行时异常。
"匹配。regex_error(error_badrepeat): 前面没有 *?+{ 中的一个 通过有效的 reguar 表达式。
所以我假设它没有正确转义。
例:
#include <iostream>
#include <string>
#include <regex>
#include <exception>
int main()
try {
std::regex point(".");
std::string s1 = ".";
if (std::regex_match(s1, point))
std::cout << "matched" << s1;
std::regex plus("+");
std::string s2 = "+";
if (std::regex_match(s2, plus))
std::cout << "matched" << s2;
char c;
std::cin >> c;
}
catch (std::runtime_error& e) {
std::cerr << e.what()<<'n';
char c;
std::cin >> c;
}
catch (...) {
std::cerr << "unknown errorn";
char c;
std::cin >> c;
}
您使用的是C++字符串文本,其中是一个特殊字符,应进行转义。所以你应该使用
"\+"
.
为了避免双重转义,您还可以使用原始字符串文字,例如R"(+)"
.
DOT.
,加上符号+
是C++中正则表达式操作的特殊字符。所以你必须像下面这样做:-
regex point("\.");
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 将无符号char*转换为std::istream*C++
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何用转义符替换字符串中的所有特殊字符
- vscode g++链路故障:体系结构x86_64的未定义符号
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 使用gcc从静态链接的文件中查找可选符号
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- VC++本机单元测试,找不到调试符号
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 检查TCHAR数组输入是否为带符号整数C++
- 用符号版本替换对函数的所有调用
- 未解析的外部符号_MsiLocateComponentW@12.
- std::正则表达式转义"+"符号
- 调用 CString::FormatV 时"%"符号转义