正则表达式

RegexPal(http://regexpal.com/

各种语言的正则表达式可能有区别

c regex

  1. 编译正则表达式 regcomp()

  2. 匹配正则表达式 regexec()

  3. 释放正则表达式 regfree()

  4. 错误处理 regerror()

     #include <stdio.h>
     #include <regex.h>  //标准c不支持,Linux常带有此文件
     
     int main(){
         regex_t reg;    //定义一个正则实例
         const char* pattern = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*.\\w+([-.]\\w+)*$"; //定义模式串
         regcomp(&reg, pattern, REG_EXTENDED);    //编译正则模式串
     
         char* buf = "david19842003@gmail.com";   //定义待匹配串
         const size_t nmatch = 1;    //定义匹配结果最大允许数
         regmatch_t pmatch[1];   //定义匹配结果在待匹配串中的下标范围
         int status = regexec(&reg, buf, nmatch, pmatch, 0); //匹配他
     
         if (status == REG_NOMATCH){ //如果没匹配上
             printf("No Match\n");
         }
         else if (status == 0){  //如果匹配上了
             printf("Match\n");
             for (int i = pmatch[0].rm_so; i < pmatch[0].rm_eo; i++){    //遍历输出匹配范围的字符串
                 printf("%c", buf[i]);
             }
             printf("\n");
         }
         regfree(&reg);  //释放正则表达式
         return 0;
     }
    

1.int regcomp (regex_t *compiled, const char *pattern, int cflags)

  • regex_t 是一个结构体数据类型,用来存放编译后的正则表达式
    • regex_t 的成员re_nsub 用来存储正则表达式中的子正则表达式的个数,子正则表达式就是用圆括号包起来的部分表达式
  • pattern 是指向我们写好的正则表达式的指针
  • cflags 有如下4个值或者是它们或运算(|)后的值:
    • REG_EXTENDED 以功能更加强大的扩展正则表达式的方式进行匹配。
    • REG_ICASE 匹配字母时忽略大小写。
    • REG_NOSUB 不用存储匹配后的结果。
    • REG_NEWLINE 识别换行符,这样’$‘就可以从行尾开始匹配,’^'就可以从行的开头开始匹配

2.int regexec (regex_t *compiled, char *string, size_t nmatch, regmatch_t matchptr [], int eflags)

  • 如果在编译正则表达式的时候没有指定cflags的参数为REG_NEWLINE,则默认情况下是忽略换行符的,也就是把整个文本串当作一个字符串处理。执行成功返回0
    • regmatch_t 是一个结构体数据类型,在regex.h中定义:typedef struct{regoff_t rm_so;regoff_t rm_eo;} regmatch_t;
    • 成员rm_so 存放匹配文本串在目标串中的开始位置,rm_eo存放结束位置
    • 通常我们以数组的形式定义一组这样的结构。因为往往我们的正则表达式中还包含子正则表达式。数组0单元存放主正则表达式位置,后边的单元依次存放子正则表达式位置
  • compiled 是已经用regcomp函数编译好的正则表达式
  • string 是目标文本串
  • nmatchregmatch_t结构体数组的长度
  • matchptr regmatch_t类型的结构体数组,存放匹配文本串的位置信息
  • eflags 有两个值
    • REG_NOTBOL 按我的理解是如果指定了这个值,那么’^’就不会从我们的目标串开始匹配。总之我到现在还不是很明白这个参数的意义
    • REG_NOTEOL 和上边那个作用差不多,不过这个指定结束end of line

3.void regfree (regex_t *compiled)

  • 清空compiled指向的regex_t结构体的内容
  • 如果是重新编译的话,一定要先清空regex_t结构体c

4.size_t regerror (int errcode, regex_t *compiled, char *buffer, size_t length)

  • 当执行regcomp 或者regexec 产生错误的时候,就可以调用这个函数而返回一个包含错误信息的字符串

  • errcode 是由regcompregexec 函数返回的错误代号

  • compiled 是已经用regcomp函数编译好的正则表达式,这个值可以为NULL

  • buffer 指向用来存放错误信息的字符串的内存空间

  • length 指明buffer的长度,如果这个错误信息的长度大于这个值,则regerror 函数会自动截断超出的字符串,但他仍然会返回完整的字符串的长度。所以我们可以用如下的方法先得到错误字符串的长度。

    size_t length = regerror (errcode, compiled, NULL, 0);

转载:
c regex 用法

C++ regex

regex头文件

Regular Expressions (C++)

正则表达式 (C++)

正则表达式 (C++)

微软regex

regex typedefs

  • basic_regex 包装正则表达式。
  • match_results 包含一系列子匹配项。
  • regex_constants 包含各种类型的常量。
  • regex_error 报告错误的正则表达式。
  • regex_iterator 循环访问匹配结果。
  • regex_traits 描述用于匹配的元素的特征。
  • regex_traits<char> 描述用于匹配的 char 的特征。
  • regex_traits<wchar_t> 描述用于匹配的 wchar_t 的特征。
  • regex_token_iterator 循环访问子匹配项。
  • sub_match 介绍子匹配项。

类型定义

  • cmatch char match_results 的类型定义。
  • cregex_iterator char regex_iterator 的类型定义。
  • cregex_token_iterator char regex_token_iterator 的类型定义。
  • csub_match char sub_match 的类型定义。
  • regex char basic_regex 的类型定义。
  • smatch string match_results 的类型定义。
  • sregex_iterator string regex_iterator 的类型定义。
  • sregex_token_iterator string regex_token_iterator 的类型定义。
  • ssub_match string sub_match 的类型定义。
  • wcmatch wchar_t match_results 的类型定义。
  • wcregex_iterator wchar_t regex_iterator 的类型定义。
  • wcregex_token_iterator wchar_t regex_token_iterator 的类型定义。
  • wcsub_match wchar_t sub_match 的类型定义。
  • wregex wchar_t basic_regex 的类型定义。
  • wsmatch wstring match_results 的类型定义。
  • wsregex_iterator wstring regex_iterator 的类型定义。
  • wsregex_token_iterator wstring regex_token_iterator 的类型定义。
  • wssub_match wstring sub_match 的类型定义。

函数

  • regex_match 与正则表达式完全匹配。
  • regex_replace 替换匹配正则表达式。
  • regex_search 搜索正则表达式匹配项。
  • swap 交换 basic_regexmatch_results 对象。

运算符

  • operator== 比较各种对象,相等。
  • operator!= 比较各种对象,不相等。
  • operator< 比较各种对象,小于。
  • operator<= 比较各种对象,小于或等于。
  • operator> 比较各种对象,大于。
  • operator>= 比较各种对象,大于或等于。
  • operator<< 将 sub_match 插入流中。

regex_match

#include "stdafx.h"  
#include <regex>   
#include <iostream>   

using namespace std;  

int _tmain(int argc, _TCHAR* argv[])  
{  

    // (1) with char*  
    // Note how const char* requires cmatch and regex  
    const char *first = "abc";  
    const char *last = first + strlen(first);  
    cmatch narrowMatch;  
    regex rx("a(b)c");  

    bool found = regex_match(first, last, narrowMatch, rx);  

    // (1) with std::wstring  
    // Note how wstring requires wsmatch and wregex.  
    // Note use of const iterators cbegin() and cend().  
    wstring target(L"Hello");  
    wsmatch wideMatch;  
    wregex wrx(L"He(l+)o");  

    if (regex_match(target.cbegin(), target.cend(), wideMatch, wrx))  
        wcout << L"The matching text is:" << wideMatch.str() << endl;   

    // (2) with std::string  
    string target2("Drizzle");  
    regex rx2(R"(D\w+e)"); // no double backslashes with raw string literal  
    found = regex_match(target2.cbegin(), target2.cend(), rx2);  

    // (3) with wchar_t*  
    const wchar_t* target3 = L"2014-04-02";  
    wcmatch wideMatch2;  

    // LR"(...)" is a  raw wide-string literal. Open and close parens  
    // are delimiters, not string elements.  
    wregex wrx2(LR"(\d{4}(-|/)\d{2}(-|/)\d{2})");   
    if (regex_match(target3, wideMatch2, wrx2))  
    {  
        wcout << L"Matching text: " << wideMatch2.str() << endl;  
    }  

     return 0;  
}
</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赤龙绕月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值