当前位置: 首页 > news >正文

判断字符串是否包含正则表达式默认的特殊字符c++

判断字符串是否包含正则表达式默认的特殊字符

业务描述:

上层配置的字符列表中,既有准确的字符串,又有可以进行正则匹配的字符串,这时候需要区分出来那些是正则匹配的字符串。

思路:

判断字符串中,是否存在正则表达式默认的特殊字符,如*^${}等。

代码:


#include <iostream>
#include <regex>using namespace std;// check if code contains regex special chars:*$^+?\{}|
static const string regexLimit = {".*[*$^+?\\\\{}|]+.*"};
static const std::regex pattern(regexLimit);
bool isRegex(const std::string& str) {try {return std::regex_match(str, pattern);}catch(const std::exception& e) {std::cout << "regex error:" << e.what() << endl;return false;}
}int main(int argc, char const *argv[])
{std::cout<< "hello world!" << std::endl;string temp1 = "*demo.*";string temp2 = "^com.*.demo.*";string temp3 = "car$";string temp4 = "com.d+dmode";string temp5 = "com.d?dmode";string temp6 = "com.d\\dmode";string temp7 = "com.d{dmode";string temp8 = "com.d}dmode";string temp9 = "com.d|dmode";string temp41 = "com.demo.test";vector<std::string> list;list.push_back(temp1);list.push_back(temp2);list.push_back(temp3);list.push_back(temp4);list.push_back(temp5);list.push_back(temp6);list.push_back(temp7);list.push_back(temp8);list.push_back(temp9);list.push_back(temp41);for (size_t i = 0; i < list.size(); i++){bool result = isRegex(list[i]);printf("i:%ld isRegex?:%d\n", i, result);}printf("hello world end.");return 0;
}

输出:

hello world!
i:0 isRegex?:1
i:1 isRegex?:1
i:2 isRegex?:1
i:3 isRegex?:1
i:4 isRegex?:1
i:5 isRegex?:1
i:6 isRegex?:1
i:7 isRegex?:1
i:8 isRegex?:1
i:9 isRegex?:0
hello world end.
http://www.lryc.cn/news/294877.html

相关文章:

  • 【蓝桥杯选拔赛真题64】python数字塔 第十五届青少年组蓝桥杯python 选拔赛比赛真题解析
  • javaEE - 23( 21000 字 Servlet 入门 -1 )
  • 【sentinel流量卫兵搭建与微服务整合】
  • Linux环境下配置mysql主从复制
  • 生物素-PEG4-酪胺,Biotin-PEG4-TSA,应用于酶联免疫吸附实验
  • Android:文件读写
  • 2024/2/5
  • 政安晨:示例演绎Python的函数与获取帮助的方法
  • 88 docker 环境下面 前端A连到后端B + 前端B连到后端A
  • k8s学习-Service Account和RBAC授权
  • SpringMVC-响应数据
  • 数学建模:数据相关性分析(Pearson和 Spearman相关系数)含python实现
  • 使用pandas将excel转成json格式
  • 双向链表的插入、删除、按位置增删改查、栈和队列区别、什么是内存泄漏
  • Linux 驱动开发基础知识——总线设备驱动模型(七)
  • RTthread线程间通信(邮箱,消息队列,信号/软件中断)---03信号(软件中断)源码分析
  • 老版本labelme如何不保存imagedata
  • vscode 如何修改c/c++格式化风格,大括号不换行
  • IP协议(2) 和 数据链路层协议基础
  • Flink-1.18.1环境搭建
  • deepin20.9安装及配置
  • 2-2 动手学深度学习v2-损失函数-笔记
  • 非springboot 使用aop 切面
  • MongoDB 字段中数据类型不一致序列化异常排查与处理
  • 网络安全简介
  • 【Docker】.NET Core 6.0 webapi 发布上传到Docker Desktop并启动运行访问,接口返回数据乱码解决方法
  • 【Android Gradle 插件】自定义 Gradle 插件模块 ⑤ ( 完整总结 )
  • 浅析现代计算机启动流程
  • 七月论文审稿GPT第2.5和第3版:分别微调GPT3.5、Llama2 13B以扩大对GPT4的优势
  • Android Studio导入项目 下载gradle很慢或连接超时