C/C++中使用正则表达式
头文件regex.h中定义了c/c++使用正则表达式的函数: regcomp(), regexec(), regerror(), and regfree() 。regcomp()编译正则表达式,regexec()匹配正则表达式, regfree()释放正则表达式,regerror()报告正则表达式错误信息。使用方法如下代码所示:
static string merge_path(const string base, const string path)
{
static regex_t re;
static int initialized = 0;
regmatch_t pm[1];
string p, s;
size_t len;
assert(*path != '/');
if (base && (p = strrchr(base, '/'))) {
newarray(s, (p - base) + strlen(path) + 2);
strncpy(s, base, (p - base) + 1);
strcpy(s + ((p - base) + 1), path);
} else {
newarray(s, strlen(path) + 2);
#if 0
s[0] = '/';
strcpy(s + 1, path);
#else
strcpy(s, path);
#endif
}
/* Replace all substrings of form "/xxx/../" with "/" */
if (! initialized) {
assert(regcomp(&re, "/[^/]+/\\.\\./", REG_EXTENDED) == 0);
initialized = 1;
}
len = strlen(s);
while (regexec(&re, s, 1, pm, 0) == 0) {
memmove(s + pm[0].rm_so, s + (pm[0].rm_eo - 1), len - pm[0].rm_eo + 2);
len -= pm[0].rm_eo - pm[0].rm_so - 1;
}
/* Replace all substrings of the form "/./" with "/" */
/*len = strlen(s);*/
for (p = s; (p = strstr(p, "/./")); ) {
memmove(p, p + 2, len - (p - s) - 1);
len -= 2;
}
return s;
}
关于C/C++中正则表达式的规则,现将opengroup.org的教程摘抄如下:(原文链接http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03_06)
<<< Previous
Home
Next >>>
The Open Group Base Specifications Issue 7
IEEE Std 1003.1-2008
Copyright © 2001-2008 The IEEE and The Open Group
9. Regular Expressions
Regular Expressions (REs) provide a mechanism to select specific strings from a set of character strings.
Regular expressions are a context-independent syntax that can represent a wide variety of character sets and character set orderings, where these character sets
相关文档:
用C语言写的一个简单的字节型查询器,主要功能是实现对 signed int float unsigned int double s ......
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include<linux/config.h>
#include<linux/module.h>
#include<linux/version.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/errno.h>
#include<linux/sche ......
Delphi 与 C/C++ 数据类型对照表
Delphi数据类型C/C++
ShorInt
8位有符号整数
char
Byte
8位无符号整数
BYTE,unsigned short
SmallInt
16位有符号整数
short
Word
16位无符号整数
unsigned short
Integer,LongInt
32位有符号整数
int,long
Cardinal,LongWord/DWORD
32位无符号整数
unsigned long
Int6 ......
1.介绍一下STL,详细说明STL如何实现vector。
Answer:
STL (标准模版库,Standard Template Library)它由容器算法迭代器组成。
STL有以下的一些优点:
可以方便容易地实现搜索数据或对数据排序等一系列的算法;
&nb ......
转帖:一辉的文章
在Linux下做开发难免要接触makefile,整个项目的构建都依赖于它。100个developer有100种makefile的写法,在一个较大的项目中,各种各样的makefile无论在开发、后期维护还是整个系统的持续集成都是一个负担。
有幸参与重构一个遗留系统的makefile,以下是一些心得和一个makefile模板。
重构目的:
1.清 ......