linux C 正则表达式 blog.chinaunix.net/uid-20729605-id-1884365.html
来源:互联网 发布:xp系统查找mac地址 编辑:程序博客网 时间:2024/06/06 20:16
linux C 正则表达式2010-07-21 10:25:57
分类: LINUX
最近有这么个需求,c搞搞正则,毕竟Regular Expression so强大^_^
标准的C和C++都不支持正则表达式,但有一些函数库可以辅助C/C++程序员完成这一功能,其中最著名的当数Philip Hazel的Perl-Compatible Regular Expression库,许多Linux发行版本都带有这个函数库。
编译正则表达式
为了提高效率,在将一个字符串与正则表达式进行比较之前,首先要用regcomp()函数对它进行编译,将其转化为regex_t结构:
int regcomp(regex_t *preg, const char *regex, int cflags);
参数regex是一个字符串,它代表将要被编译的正则表达式;参数preg指向一个声明为regex_t的数据结构,用来保存编译结果;参数cflags决定了正则表达式该如何被处理的细节。
如果函数regcomp()执行成功,并且编译结果被正确填充到preg中后,函数将返回0,任何其它的返回结果都代表有某种错误产生。
匹配正则表达式
一旦用regcomp()函数成功地编译了正则表达式,接下来就可以调用regexec()函数完成模式匹配:
int regexec(const regex_t *preg, const char *string, size_t nmatch,regmatch_t pmatch[], int eflags);
typedef struct {
regoff_t rm_so;
regoff_t rm_eo;
} regmatch_t;
参数preg指向编译后的正则表达式,参数string是将要进行匹配的字符串,而参数nmatch和pmatch则用于把匹配结果返回给调用程序,最后一个参数eflags决定了匹配的细节。
在调用函数regexec()进行模式匹配的过程中,可能在字符串string中会有多处与给定的正则表达式相匹配,参数pmatch就是用来保存这些匹配位置的,而参数nmatch则告诉函数regexec()最多可以把多少个匹配结果填充到pmatch数组中。当regexec()函数成功返回时,从 string+pmatch[0].rm_so到string+pmatch[0].rm_eo是第一个匹配的字符串,而从 string+pmatch[1].rm_so到string+pmatch[1].rm_eo,则是第二个匹配的字符串,依此类推。
释放正则表达式
无论什么时候,当不再需要已经编译过的正则表达式时,都应该调用函数regfree()将其释放,以免产生内存泄漏。
void regfree(regex_t *preg);
函数regfree()不会返回任何结果,它仅接收一个指向regex_t数据类型的指针,这是之前调用regcomp()函数所得到的编译结果。
如果在程序中针对同一个regex_t结构调用了多次regcomp()函数,POSIX标准并没有规定是否每次都必须调用regfree()函数进行释放,但建议每次调用regcomp()函数对正则表达式进行编译后都调用一次regfree()函数,以尽早释放占用的存储空间。
报告错误信息
如果调用函数regcomp()或regexec()得到的是一个非0的返回值,则表明在对正则表达式的处理过程中出现了某种错误,此时可以通过调用函数regerror()得到详细的错误信息。
size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_terrbuf_size);
参数errcode是来自函数regcomp()或regexec()的错误代码,而参数preg则是由函数regcomp()得到的编译结果,其目的是把格式化消息所必须的上下文提供给regerror()函数。在执行函数regerror()时,将按照参数errbuf_size指明的最大字节数,在 errbuf缓冲区中填入格式化后的错误信息,同时返回错误信息的长度。
应用正则表达式
最后给出一个具体的实例,介绍如何在C语言程序中处理正则表达式。
regexp.c:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
/* 取子串的函数 */
static char *
substr (const char *str, unsigned start, unsigned end)
{
unsigned n = end - start;
static char stbuf[256];
strncpy (stbuf, str + start, n);
stbuf[n] = 0;
return stbuf;
}
/* 主程序 */
int
main (int argc, char **argv)
{
char *pattern;
int x, z, lno = 0, cflags = 0;
char ebuf[128], lbuf[256];
regex_t reg;
regmatch_t pm[10];
const size_t nmatch = 10;
pattern = argv[1];
z = regcomp (®, pattern, cflags);
if (z != 0)
{
regerror (z,®, ebuf, sizeof (ebuf));
fprintf (stderr, "%s: pattern '%s' \n", ebuf, pattern);
return 1;
}
/* 逐行处理输入的数据 */
while (fgets (lbuf, sizeof (lbuf), stdin))
{
++lno;
if ((z = strlen (lbuf)) > 0 && lbuf[z - 1] == '\n')
lbuf[z - 1] = 0;
/*对每一行应用正则表达式进行匹配 */
z = regexec (®, lbuf, nmatch, pm, 0);
if (z == REG_NOMATCH)
continue;
else if (z != 0)
{
regerror (z,®, ebuf, sizeof (ebuf));
fprintf (stderr, "%s: regcom('%s')\n", ebuf, lbuf);
return 2;
}
/* 输出处理结果 */
for (x = 0; x < nmatch && pm[x].rm_so != -1; ++x)
{
if (!x)
printf ("%04d: %s\n", lno, lbuf);
printf (" $%d='%s'\n", x, substr (lbuf, pm[x].rm_so, pm[x].rm_eo));
}
}
/* 释放正则表达式 */
regfree (®);
return 0;
}
Makefile:
CC = gcc
CFLAGS = -g -Wall
TARGET = regexp
EXECOBJS = ${TARGET}.o
$(TARGET): $(EXECOBJS)
$(CC) $(CFLAGS) -o $(TARGET) $(EXECOBJS)
%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $<
clean:
rm -rf *.o *.a *.so $(TARGET)
[root@zj:~/C_parm/regexp]# make
gcc -g -Wall -o regexp.o -c regexp.c
gcc -g -Wall -o regexp regexp.o
[root@zj:~/C_parm/regexp]# ./c_regexp 'regex[a-z]*' < c_regexp.c
0004: #include <regex.h>
$0='regex'
0023: regex_t reg;
$0='regex'
0043: z = regexec (®, lbuf, nmatch, pm, 0);
$0='regexec'
不要说为什么一个文件还Makefile...以前我也很少用Makefile,win下的习惯!!!BS下自己,最近写linux code写大半年了,我就没自己敲过gcc了^_^.
其实关键是code总要不定的debug,你不能每次debug后都gcc -o *** -l -L....吧.filename没改变,我just make^_^
标准的C和C++都不支持正则表达式,但有一些函数库可以辅助C/C++程序员完成这一功能,其中最著名的当数Philip Hazel的Perl-Compatible Regular Expression库,许多Linux发行版本都带有这个函数库。
编译正则表达式
为了提高效率,在将一个字符串与正则表达式进行比较之前,首先要用regcomp()函数对它进行编译,将其转化为regex_t结构:
int regcomp(regex_t *preg, const char *regex, int cflags);
参数regex是一个字符串,它代表将要被编译的正则表达式;参数preg指向一个声明为regex_t的数据结构,用来保存编译结果;参数cflags决定了正则表达式该如何被处理的细节。
如果函数regcomp()执行成功,并且编译结果被正确填充到preg中后,函数将返回0,任何其它的返回结果都代表有某种错误产生。
匹配正则表达式
一旦用regcomp()函数成功地编译了正则表达式,接下来就可以调用regexec()函数完成模式匹配:
int regexec(const regex_t *preg, const char *string, size_t nmatch,regmatch_t pmatch[], int eflags);
typedef struct {
regoff_t rm_so;
regoff_t rm_eo;
} regmatch_t;
参数preg指向编译后的正则表达式,参数string是将要进行匹配的字符串,而参数nmatch和pmatch则用于把匹配结果返回给调用程序,最后一个参数eflags决定了匹配的细节。
在调用函数regexec()进行模式匹配的过程中,可能在字符串string中会有多处与给定的正则表达式相匹配,参数pmatch就是用来保存这些匹配位置的,而参数nmatch则告诉函数regexec()最多可以把多少个匹配结果填充到pmatch数组中。当regexec()函数成功返回时,从 string+pmatch[0].rm_so到string+pmatch[0].rm_eo是第一个匹配的字符串,而从 string+pmatch[1].rm_so到string+pmatch[1].rm_eo,则是第二个匹配的字符串,依此类推。
释放正则表达式
无论什么时候,当不再需要已经编译过的正则表达式时,都应该调用函数regfree()将其释放,以免产生内存泄漏。
void regfree(regex_t *preg);
函数regfree()不会返回任何结果,它仅接收一个指向regex_t数据类型的指针,这是之前调用regcomp()函数所得到的编译结果。
如果在程序中针对同一个regex_t结构调用了多次regcomp()函数,POSIX标准并没有规定是否每次都必须调用regfree()函数进行释放,但建议每次调用regcomp()函数对正则表达式进行编译后都调用一次regfree()函数,以尽早释放占用的存储空间。
报告错误信息
如果调用函数regcomp()或regexec()得到的是一个非0的返回值,则表明在对正则表达式的处理过程中出现了某种错误,此时可以通过调用函数regerror()得到详细的错误信息。
size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_terrbuf_size);
参数errcode是来自函数regcomp()或regexec()的错误代码,而参数preg则是由函数regcomp()得到的编译结果,其目的是把格式化消息所必须的上下文提供给regerror()函数。在执行函数regerror()时,将按照参数errbuf_size指明的最大字节数,在 errbuf缓冲区中填入格式化后的错误信息,同时返回错误信息的长度。
应用正则表达式
最后给出一个具体的实例,介绍如何在C语言程序中处理正则表达式。
regexp.c:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
/* 取子串的函数 */
static char *
substr (const char *str, unsigned start, unsigned end)
{
unsigned n = end - start;
static char stbuf[256];
strncpy (stbuf, str + start, n);
stbuf[n] = 0;
return stbuf;
}
/* 主程序 */
int
main (int argc, char **argv)
{
char *pattern;
int x, z, lno = 0, cflags = 0;
char ebuf[128], lbuf[256];
regex_t reg;
regmatch_t pm[10];
const size_t nmatch = 10;
pattern = argv[1];
z = regcomp (®, pattern, cflags);
if (z != 0)
{
regerror (z,®, ebuf, sizeof (ebuf));
fprintf (stderr, "%s: pattern '%s' \n", ebuf, pattern);
return 1;
}
/* 逐行处理输入的数据 */
while (fgets (lbuf, sizeof (lbuf), stdin))
{
++lno;
if ((z = strlen (lbuf)) > 0 && lbuf[z - 1] == '\n')
lbuf[z - 1] = 0;
/*对每一行应用正则表达式进行匹配 */
z = regexec (®, lbuf, nmatch, pm, 0);
if (z == REG_NOMATCH)
continue;
else if (z != 0)
{
regerror (z,®, ebuf, sizeof (ebuf));
fprintf (stderr, "%s: regcom('%s')\n", ebuf, lbuf);
return 2;
}
/* 输出处理结果 */
for (x = 0; x < nmatch && pm[x].rm_so != -1; ++x)
{
if (!x)
printf ("%04d: %s\n", lno, lbuf);
printf (" $%d='%s'\n", x, substr (lbuf, pm[x].rm_so, pm[x].rm_eo));
}
}
/* 释放正则表达式 */
regfree (®);
return 0;
}
Makefile:
CC = gcc
CFLAGS = -g -Wall
TARGET = regexp
EXECOBJS = ${TARGET}.o
$(TARGET): $(EXECOBJS)
$(CC) $(CFLAGS) -o $(TARGET) $(EXECOBJS)
%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $<
clean:
rm -rf *.o *.a *.so $(TARGET)
[root@zj:~/C_parm/regexp]# make
gcc -g -Wall -o regexp.o -c regexp.c
gcc -g -Wall -o regexp regexp.o
[root@zj:~/C_parm/regexp]# ./c_regexp 'regex[a-z]*' < c_regexp.c
0004: #include <regex.h>
$0='regex'
0023: regex_t reg;
$0='regex'
0043: z = regexec (®, lbuf, nmatch, pm, 0);
$0='regexec'
不要说为什么一个文件还Makefile...以前我也很少用Makefile,win下的习惯!!!BS下自己,最近写linux code写大半年了,我就没自己敲过gcc了^_^.
其实关键是code总要不定的debug,你不能每次debug后都gcc -o *** -l -L....吧.filename没改变,我just make^_^
0 0
- linux C 正则表达式 blog.chinaunix.net/uid-20729605-id-1884365.html
- linux常用c函数 进程操作篇 http://blog.chinaunix.net/uid-25906157-id-3136757.html
- http://blog.chinaunix.net/uid-22666718-id-1771703.html
- http://blog.chinaunix.net/uid-20322254-id-145835.html
- http://blog.chinaunix.net/uid-25547034-id-3155778.html
- http://blog.chinaunix.net/uid-25750954-id-2956084.html
- http://blog.chinaunix.net/uid-22342564-id-3053393.html
- http://blog.chinaunix.net/uid-25082381-id-3242162.html
- http://blog.chinaunix.net/uid-25737580-id-3182286.html
- http://blog.chinaunix.net/uid-25835268-id-3055356.html--makefile
- http://blog.chinaunix.net/uid-8874157-id-2012678.html
- http://blog.chinaunix.net/uid-21289517-id-3081793.html
- http://blog.chinaunix.net/uid-20543672-id-3244832.html
- http://blog.chinaunix.net/uid-21222282-id-3244532.html
- http://blog.chinaunix.net/uid-21501855-id-4490453.html
- http://blog.chinaunix.net/uid-20484604-id-1941290.html
- http://blog.chinaunix.net/uid-20569459-id-335214.html
- http://blog.chinaunix.net/uid-21411227-id-1826986.html
- Python函数式编程指南(一):概述
- PAT (Basic Level) Practise (中文)1040. 有几个PAT(25) C语言
- C++类保护函数外部调用
- POJ1190(搜索加剪枝)
- java 基础之枚举
- linux C 正则表达式 blog.chinaunix.net/uid-20729605-id-1884365.html
- 从头到尾彻底解析哈希表算法
- 八大排序算法-冒泡排序
- OpenCV-自适应阈值化
- github简易使用笔记
- 聊聊HTTPS和SSL/TLS协议
- linux c与正则表达式 blog.sina.com.cn/s/blog_62b8329101010dpu.html
- Thread和Runnable
- Python函数式编程指南(二):函数