正则表达式实现(Linux C语言版本)
来源:互联网 发布:诺曼底公爵 知乎 编辑:程序博客网 时间:2024/05/17 11:04
#include <stdlib.h>#include <stdio.h>#include <string.h>#include <regex.h>/* The following is the size of a buffer to contain any error messages encountered when the regular expression is compiled. */#define MAX_ERROR_MSG 0x1000/* Compile the regular expression described by "regex_text" into "r". */static int compile_regex (regex_t * r, const char * regex_text){ int status = regcomp (r, regex_text, REG_EXTENDED|REG_NEWLINE); if (status != 0) {char error_message[MAX_ERROR_MSG];regerror (status, r, error_message, MAX_ERROR_MSG); printf ("Regex error compiling '%s': %s\n", regex_text, error_message); return 1; } return 0;}/* Match the string in "to_match" against the compiled regular expression in "r". */static int match_regex (regex_t * r, const char * to_match){ /* "P" is a pointer into the string which points to the end of the previous match. */ const char * p = to_match; /* "N_matches" is the maximum number of matches allowed. */ const int n_matches = 10; /* "M" contains the matches found. */ regmatch_t m[n_matches]; while (1) { int i = 0; int nomatch = regexec (r, p, n_matches, m, 0); if (nomatch) { printf ("No more matches.\n"); return nomatch; } for (i = 0; i < n_matches; i++) { int start; int finish; if (m[i].rm_so == -1) { break; } start = m[i].rm_so + (p - to_match); finish = m[i].rm_eo + (p - to_match); if (i == 0) { printf ("$& is "); } else { printf ("$%d is ", i); } printf ("'%.*s' (bytes %d:%d)\n", (finish - start), to_match + start, start, finish); } p += m[0].rm_eo; } return 0;}int main(int argc, char ** argv){ regex_t r; const char * regex_text; const char * find_text; if (argc != 3) { regex_text = "([[:digit:]]+)[^[:digit:]]+([[:digit:]]+)"; find_text = "This 1 is nice 2 so 33 for 4254"; } else { regex_text = argv[1]; find_text = argv[2]; } printf ("Trying to find '%s' in '%s'\n", regex_text, find_text); compile_regex(& r, regex_text); match_regex(& r, find_text); regfree (& r); return 0;}
0 0
- 正则表达式实现(Linux C语言版本)
- Rob 最小正则表达式C语言实现
- C语言中的正则表达式实现
- linux环境下,C语言,正则表达式
- linux下c语言正则表达式
- C语言正则表达式
- C语言正则表达式
- C语言正则表达式
- 正则表达式 - C语言
- C语言正则表达式
- C语言正则表达式
- C语言使用正则表达式(2)
- C语言正则表达式(修改版)
- 正则表达式实例(c语言)
- 正则表达式(linux/C++)
- linux c 正则表达式
- linux c 正则表达式
- linux C正则表达式
- poj2387 Til the Cows Come Home
- 关于函数参数入栈顺序(完善中)
- 设计第二个弹出窗口(MessageBox)
- Boost智能指针shared_ptr、weak_ptr
- java中volatile关键字的含义
- 正则表达式实现(Linux C语言版本)
- 触摸事件(touchstart、touchmove和touchend)
- ExecutorService的十个使用技巧
- cxf隐藏service列表
- Leetcode 231. Power of Two (Easy) (cpp)
- 手机APP开发之车牌识别SDK移植
- 邮件批处理(业务流程分析)
- 获取Executor提交的并发执行的任务返回结果的两种方式/ExecutorCompletionService使用
- 图片压缩工具