IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 【笔记】c++ - 正则表达式: GNU Regex Library、PCRE PCRE++、Boost.Regex -> 正文阅读

[C++知识库]【笔记】c++ - 正则表达式: GNU Regex Library、PCRE PCRE++、Boost.Regex

C/C++ 中常用的正则表达式库1有:

  1. GNU Regex Library
  2. Boost.Regex
  3. PCRE
  4. PCRE++

这四个库中,后面两个是有关系,其它都是各自己独立的,是不同的实现。

GNU Regex Library

GNU Regex Library 是glibc(GNU C Library)的一部分,它提供与POSIX标准兼容的正则表达式匹配的接口。

更多:http://www.wuzesheng.com/?p=929

PCRE, PCRE++

PCRE2 (Perl Compatible Regular Expressions)是与 Perl3 中正则表达式相兼容的一个正则表达式库。

PCRE++是一个对PCRE库的C++封装,它提供了更加方便、易用的C++接口。

更多:http://www.wuzesheng.com/?p=994

Boost.Regex

boost::regexC++ 提供了完整的正则表达式支持

表达式语法

一般规则

符号解释
. 除换行符以外的所有字符。
^ 字符串开头。
$ 字符串结尾。
\d,\w,\s 匹配数字、字符、空格。
\D,\W,\S 匹配非数字、非字符、非空格。
[abc] 匹配 a、b 或 c 中的一个字母。
[a-z] 匹配 a 到 z 中的一个字母。
[^abc] 匹配除了 a、b 或 c 中的其他字母。
aa|bb匹配 aa 或 bb。
? 0 次或 1 次匹配。
* 匹配 0 次或多次。
+ 匹配 1 次或多次。
{n} 匹配 n次。
{n,} 匹配 n次以上。
{m,n} 最少 m 次,最多 n 次匹配。
(expr) 捕获 expr 子模式,以 \1 使用它。
(?:expr) 忽略捕获的子模式。
(?=expr) 正向预查模式 expr。
(?!expr) 负向预查模式 expr。

懒惰限定符

代码/语法说明
*?重复任意次,但尽可能少重复
+?重复1次或更多次,但尽可能少重复
??重复0次或1次,但尽可能少重复
{n,m}?重复n到m次,但尽可能少重复
{n,}?重复n次以上,但尽可能少重复

编译

编译介绍:http://www.wuzesheng.com/?p=965

C++11 以后,Boot.Regex 已被接收为C++0x标准库4

因此现在只需要加入 regex 头文件,就可以在 std namespace 中使用它们的类和方法了。

#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main()
{
  std::string s = "Boost Libraries";
  boost::regex expr{"\\w+\\s\\w+"};
  std::cout << std::boolalpha << boost::regex_match(s, expr) << '\n';
}

使用

参考:https://theboostcpplibraries.com/boost.regex#ex.regex_01

Boost.Regex 中,两个最重要的类是 boost::regexboost::smatch,前者定义正则表达式,后者保存搜索结果。

namespace boost{

typedef match_results<const char*>                  cmatch;
typedef match_results<const wchar_t*>               wcmatch;
typedef match_results<std::string::const_iterator>  smatch;
typedef match_results<std::wstring::const_iterator> wsmatch; 

}

Boost.Regex 中,提供了三个方法(function)来使用查找(search)字符串:

  • boost::regex_match()
  • boost::regex_search()
  • boost::regex_replace()

搜索 boost::regex_match()

官方文档 - https://www.boost.org/doc/libs/1_80_0/libs/regex/doc/html/boost_regex/ref/regex_match.html

#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main()
{
  std::string s = "Boost Libraries";
  boost::regex expr{"\\w+\\s\\w+"};
  std::cout << std::boolalpha << boost::regex_match(s, expr) << '\n';
}

boost::regex_match() 当匹配到字符串会返回 true

搜索 boost::regex_search()

#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main()
{
  std::string s = "Boost Libraries";
  boost::regex expr{"(\\w+)\\s(\\w+)"};
  boost::smatch what;
  if (boost::regex_search(s, what, expr))
  {
    std::cout << what[0] << '\n';
    std::cout << what[1] << "_" << what[2] << '\n';
  }
}

boost::regex_search() 需要一个 boost::smatch 作为参数,来保存匹配结果

结果可以直接迭代

optional<std::vector<string>> regex_match(const string target ,const string &pattern )
{
  boost::regex reg(pattern);
  boost::smatch sm;
  std::vector<string> res;
  if ( boost::regex_search(target,sm,reg)) {
    for (auto str : sm)
      res.push_back(str);
    return res;
  }
  return {}; // return nil
}

替换 boost::regex_replace()

#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main()
{
  std::string s = " Boost Libraries ";
  boost::regex expr{"\\s"};
  std::string fmt{"_"};
  std::cout << boost::regex_replace(s, expr, fmt) << '\n';
}

boost::regex_replace 会替换所有匹配到的字符串,得到 "_Boost_Libraries_."

#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main()
{
  std::string s = "Boost Libraries";
  boost::regex expr{"(\\w+)\\s(\\w+)"};
  std::string fmt{"\\2 \\1"};
  std::cout << boost::regex_replace(s, expr, fmt) << '\n';
}

在结果表达式中,可以用数字只带匹配到的结果,得到 "Libraries Boost"

#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main()
{
  std::string s = "Boost Libraries";
  boost::regex expr{"(\\w+)\\s(\\w+)"};
  std::string fmt{"\\2 \\1"};
  std::cout << boost::regex_replace(s, expr, fmt,
    boost::regex_constants::format_literal) << '\n';
}

也可以用 boost::regex_constants::format_literal 取消这种指代,得到 "\2 \1."

例子:信用卡 💳

参考:https://www.boost.org/doc/libs/1_37_0/libs/regex/doc/html/boost_regex/introduction_and_overview.html

搜索 - regex_match 🔗详情

校验信用卡为例:数字,16位长度,每4位分成一组,以 -_ 连接

(\\d{4}[- ]){3}\\d{4}

bool validate_card_format(const std::string& s)
{
   static const boost::regex e("(\\d{4}[- ]){3}\\d{4}");
   return regex_match(s, e);
}

替换 - regex_replace 🔗详情

上述信用卡格式是人读的,还有机器格式:数字,15到16位长度,不分组,中间每连接符

于是需要两套识别和转换的代码

// match any format with the regular expression:
const boost::regex e("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z");
const std::string machine_format("\\1\\2\\3\\4");
const std::string human_format("\\1-\\2-\\3-\\4");

std::string machine_readable_card_number(const std::string s)
{
   return regex_replace(s, e, machine_format, boost::match_default | boost::format_sed);
}

std::string human_readable_card_number(const std::string s)
{
   return regex_replace(s, e, human_format, boost::match_default | boost::format_sed);
}

CSDN话题挑战赛第2期
参赛话题:学习笔记


  1. 正则表达式(Regular Expressions)
    是一种十分简便、灵活的文本处理工具,可以用来精确地找出某文本中匹配某种指定规则的内容。
    (在linux下,grep, sed, awk等工具都支持正则表达式) ??

  2. PCRE(Perl Compatible Regular Expressions, perl 兼容的正则表达式库) ??

  3. Perl(Practical Extraction and Report Language, 实用报表提取语言)
    在这里插入图片描述
    其前身是Unix系统管理的一个工具,被用在无数的小任务里。后逐渐发展成为一种功能强大的程序设计语言,用作Web编程、数据库处理、XML处理以及系统管理 ??

  4. Boost.Regex 在2010年时还不是C++标准库的一部分 ??

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-09-25 23:05:06  更:2022-09-25 23:06:43 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 6:40:03-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码