C语言使用正则表达式(2)
GNU/Linux有两套库可用于正则表达式编程:POSIX库和PCRE库。前者不需要单独安装,一般需求还是能满足的,速度稍慢些。后者是久负盛名的Perl正则表达式库,功能强大,匹配速度快,不过可能需要单独安装。
我们先用一个例子来介绍如何使用POSIX库。
#i nclude <stdio.h>
#i nclude <sys/types.h>
#i nclude <regex.h>
char *get_regerror (int errcode, regex_t *compiled)
{
size_t length = regerror (errcode, compiled, NULL, 0);
char *buffer = malloc(length);
if (!buffer) return NULL;
(void) regerror (errcode, compiled, buffer, length);
return buffer;
}
int regtest(const char*pattern, const char* string)
{
regex_t reg;
regmatch_t *subexprs = NULL;
int ret;
int i;
if (0 != (ret=regcomp(?, pattern, REG_EXTENDED))) {
char *buffer = get_regerror(ret, ?);
if (buffer) {
fprintf(stderr, "regcomp:[%d]%s\n", ret, buffer);
free(buffer);
}
return -1;
}
subexprs = malloc((reg.re_nsub+1)*sizeof(regmatch_t));
if (!subexprs) {
fprintf(stderr, "error malloc subexprs\n");
regfree(?);
return -1;
}
if (0 != (ret=regexec(?, string, reg.re_nsub+1, subexprs, 0))) {
char *buffer = get_regerror(ret, ?);
if (buffer) {
fprintf(stderr, "regexec:[%d]%s\n", ret, buffer);
free(buffer);
}
regfree(?);
return -1;
}
for (i =&
相关文档:
http://en.wikipedia.org/wiki/C_preprocessor
C preprocessor
from Wikipedia, the free encyclopedia
Jump to:navigation, search
The C preprocessor (cpp) is the preprocessor for the C programming language. In many C implementations, it is a separate program invoked by the compiler as the first part ......
今天是5月的最后一天了,2010年的5月,倒霉的一个月终于要过去了。今天抽空我又读完了第三章,顺便做下笔记。
这一章的题目叫做---语义“陷阱”
程序也有可能表面看上去是一个意思,实际上的意思却相去甚远。
对于数组:
C语言中只有一维数组, ......
1) goto
goto 只能在一个函数内跳转。建议少用,使得程序维护起来容易出错;但是,在有多个循环情况下跳转,有时用goto可以使得问题变得简单。
class A
{
public:
A(){}
~A(){}
};
&nbs ......
linux常用头文件如下:
POSIX标准定义的头文件
<dirent.h>
目录项
<fcntl.h> 文件控制
<fnmatch.h> 文件名匹配类型
<glob.h>
路径名模式匹配类型
< ......
这三章主要讲了C语言的链接、库函数、预处理。还是有点晦涩难懂啊。
一个C程序是可以有多个部分组成的,但是编译器每次只能编译一个文件,找出其中的错误。某些C语言实现提供了一个称为lint的程序,可以捕获大量的此类错误。连接器一般是与编译器分离的,编译器是把C程序“翻译”成对连 ......