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

[C/C++11]_[初级]_[使用正则表达式分组来获取动态字符串]

场景

  1. 之前使用Objective-C获取所有的分组[1], 只不过ObjC只能在macOS上使用。如果是在Windows上,可以使用C++11标准库的正则库来进行分组。 如果是跨平台实现,可以使用utf-8编码。

说明

  1. C++11的正则查找第一个分组字符串,可以使用regex_search函数。 如果需要遍历字符串里的所有分组,那么需要创建sregex_iterator枚举变量。

  2. 注意,如果是要给字符串里的click here加超链接样式。

  • Windows创建CLinkCtrl时,需要给字符串加上<a href=''>xx</a>才会有超链接效果。使用matches.position(i)来获取字符串的位置,创建一个新的string,并通过append方法来组合其他字符串和分组字符串来形成新的字符串。

    string str;
    ...
    str.append("<a href='https://'").append(matches[i]).append("</a>");
    
  • macOSCocoa是通过创建分组字符串的NSAttributedString*来封装分组字符串matches[i]

例子

#include <iostream>
#include <regex>
#include <string>using namespace std;void TestSearchFirstGroup(const string& text,const char* pattern1)
{regex pattern(pattern1);smatch matches;if (!regex_search(text, matches, pattern))return;for (int i = 0; i < matches.size(); i++){if (i) {cout << "Group str is: " << matches[i] << "-> position: " << matches.position(i) << endl;}//else {//	cout << "Whole Group str is: " << matches[i] << endl;//}}
}void TestSearchAllGroup(const string& text,const char* pattern1)
{regex pattern(pattern1);auto begin = std::sregex_iterator(text.begin(), text.end(), pattern);auto end = std::sregex_iterator();for (std::sregex_iterator i = begin; i != end; ++i) {std::smatch matches = *i;for (int i = 0; i < matches.size(); i++){if (i) {cout << "Group str is: " << matches[i] << "-> position: " << matches.position(i) << endl;}//else {//	cout << "Whole Group str is: " << match[i] << endl;//}}}}int main() 
{    cout << "========== TestSearchFirstGroup =============" << endl;TestSearchFirstGroup("Please |click here$ to erase all system |settings$ and data.","[|]([^|$]+)[$]");cout << "========== TestSearchGroup2 =============" << endl;TestSearchAllGroup("Please |click here$ to erase all system |settings$ and data.","[|]([^|$]+)[$]");return 0;
}

输出

========== TestSearchFirstGroup =============
Group str is: click here-> position: 8
========== TestSearchGroup2 =============
Group str is: click here-> position: 8
Group str is: settings-> position: 41

参考

  1. 使用正则表达式分组来获取动态字符串

  2. 使用正则表达式库进行分组查询

  3. std::regex_search

  4. std::regex_iterator

  5. std::match_results

http://www.lryc.cn/news/572825.html

相关文章:

  • tkinter 的 grid() 布局管理器学习指南
  • Flowise工作流引擎的本地部署与远程访问实践
  • 算法-每日一题(DAY11)每日温度
  • King’s LIMS 系统引领汽车检测实验室数字化转型
  • 【Wi-Fi天气时钟】网络授时
  • uniapp评价组件
  • net程序-Serilog 集成 SQL Server LocalDB 日志记录指南
  • Vue框架深度解析:从Vue2到Vue3的技术演进与实践指南
  • C++11 右值引用(Rvalue Reference)
  • SM3算法C语言实现(无第三方库,带测试)
  • 全面掌握 C++ 基础:关键特性与进化
  • 【C++】哈希表的实现(开放定址法)
  • 语音相关-浏览器的自动播放策略研究和websocket研究
  • RPGMZ游戏引擎 如何手动控制文字显示速度
  • 开疆智能ModbusTCP转EtherCAT网关连接IVO编码器配置案例
  • APP CPU测试--Android Profiler实操(入门版)
  • Flutter ListTile 深度解析
  • 【二进制安全作业】250617课上作业4 - start
  • springboot整合规则引擎(liteflow)使用
  • 解决Docker网络与虚拟机桥接冲突的实践指南
  • Docker环境部署
  • Dilworth 定理 学习笔记
  • html和css实现文本打断换行、自动换行
  • leetcode hot100 两数之和
  • Unity2D 街机风太空射击游戏 学习记录 #12环射道具的引入
  • C语言:实现杨辉三角的种方法
  • Linux命令合集
  • LVS负载均衡群集:Nginx+Tomcat负载均衡群集
  • 云宏信息轻量云平台:解锁金融业IT架构优化之路
  • Postman接口测试完整版