RegexPal(http://regexpal.com/)
各种语言的正则表达式可能有区别
c regex
-
编译正则表达式
regcomp() -
匹配正则表达式
regexec() -
释放正则表达式
regfree() -
错误处理
regerror()#include <stdio.h> #include <regex.h> //标准c不支持,Linux常带有此文件 int main(){ regex_t reg; //定义一个正则实例 const char* pattern = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*.\\w+([-.]\\w+)*$"; //定义模式串 regcomp(®, pattern, REG_EXTENDED); //编译正则模式串 char* buf = "david19842003@gmail.com"; //定义待匹配串 const size_t nmatch = 1; //定义匹配结果最大允许数 regmatch_t pmatch[1]; //定义匹配结果在待匹配串中的下标范围 int status = regexec(®, 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(®); //释放正则表达式 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是目标文本串nmatch是regmatch_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是由regcomp和regexec函数返回的错误代号 -
compiled是已经用regcomp函数编译好的正则表达式,这个值可以为NULL -
buffer指向用来存放错误信息的字符串的内存空间 -
length指明buffer的长度,如果这个错误信息的长度大于这个值,则regerror函数会自动截断超出的字符串,但他仍然会返回完整的字符串的长度。所以我们可以用如下的方法先得到错误字符串的长度。size_t length = regerror (errcode, compiled, NULL, 0);
转载:
c regex 用法
C++ regex
类
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介绍子匹配项。
类型定义
cmatchchar match_results的类型定义。cregex_iteratorchar regex_iterator的类型定义。cregex_token_iteratorchar regex_token_iterator的类型定义。csub_matchchar sub_match的类型定义。regexchar basic_regex的类型定义。smatchstring match_results的类型定义。sregex_iteratorstring regex_iterator的类型定义。sregex_token_iteratorstring regex_token_iterator的类型定义。ssub_matchstring sub_match的类型定义。wcmatchwchar_t match_results的类型定义。wcregex_iteratorwchar_t regex_iterator的类型定义。wcregex_token_iteratorwchar_t regex_token_iterator的类型定义。wcsub_matchwchar_t sub_match的类型定义。wregexwchar_t basic_regex的类型定义。wsmatchwstring match_results的类型定义。wsregex_iteratorwstring regex_iterator的类型定义。wsregex_token_iteratorwstring regex_token_iterator的类型定义。wssub_matchwstring sub_match的类型定义。
函数
regex_match与正则表达式完全匹配。regex_replace替换匹配正则表达式。regex_search搜索正则表达式匹配项。swap交换basic_regex或match_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;
}
</

最低0.47元/天 解锁文章
9万+

被折叠的 条评论
为什么被折叠?



