regcomp ()- 编译正则表达式
格式
#include <regex.h>
int regcomp(regex_t *preg, const char *pattern, int cflags);语言级别
XPG4
线程安全
是
语言环境敏感
此函数的行为可能受当前语言环境的 LC_CTYPE 和 LC_COLLATE 类别影响。 当在编译命令上指定 LOCALETYPE (*CLD) 时,此功能不可用。 有关更多信息,请参阅 了解 CCSID 和语言环境。
描述
regcomp() 函数将 模式 指向的源正则表达式编译为可执行版本,并将其存储在 preg指向的位置。 然后,可以使用 regexec() 函数将正则表达式与其他字符串进行比较。
cflags 标志定义编译进程的属性:
| Cflag | 描述字符串 |
|---|---|
| REG_ALT_NL |
注: 对于 UTF-8 和 UTF-32,集成文件系统的换行符与数据库换行符相同。
|
| 已扩展 | 支持扩展正则表达式。 |
| REG_NEWLINE | 将换行符视为特殊的行尾字符; 然后,它将建立由] 和 $模式匹配的行边界,并且只能在使用 \n 显式的字符串中进行匹配。 (如果省略此标志,那么会将换行符视为任何其他字符。) |
| REG_ICASE | 忽略匹配中的大小写。 |
| REG_NOSUB | 忽略 pattern中指定的子表达式数。 将字符串与已编译的模式 (使用 regexec()) 进行比较时,该字符串必须与整个模式匹配。 然后, regexec() 函数将返回一个值,该值仅指示是否找到匹配项; 它不指示匹配项在字符串中的哪个点开始,或者匹配字符串是什么。 |
正则表达式是独立于上下文的语法,可表示各种各样的字符集和字符集排序,可根据当前语言环境以不同方式进行解释。 函数 regcomp(), regerror(), regexec()和 regfree() 以类似于 UNIX awk , ed , grep 和 egrep 命令的方式使用正则表达式。
返回值
如果 regcomp() 函数成功,那么它将返回 0。 否则,它将返回可在 regerror() 函数调用中使用的错误代码,并且未定义 preg 的内容。
示例
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
regex_t preg;
char *string = "a very simple simple simple string";
char *pattern = "\\(sim[a-z]le\\) \\1";
int rc;
size_t nmatch = 2;
regmatch_t pmatch[2];
if (0 != (rc = regcomp(&preg, pattern, 0))) {
printf("regcomp() failed, returning nonzero (%d)\n", rc);
exit(EXIT_FAILURE);
}
if (0 != (rc = regexec(&preg, string, nmatch, pmatch, 0))) {
printf("Failed to match '%s' with '%s',returning %d.\n",
string, pattern, rc);
}
else {
printf("With the whole expression, "
"a matched substring \"%.*s\" is found at position %d to %d.\n",
pmatch[0].rm_eo - pmatch[0].rm_so, &string[pmatch[0].rm_so],
pmatch[0].rm_so, pmatch[0].rm_eo - 1);
printf("With the sub-expression, "
"a matched substring \"%.*s\" is found at position %d to %d.\n",
pmatch[1].rm_eo - pmatch[1].rm_so, &string[pmatch[1].rm_so],
pmatch[1].rm_so, pmatch[1].rm_eo - 1);
}
regfree(&preg);
return 0;
/****************************************************************************
The output should be similar to :
With the whole expression, a matched substring "simple simple" is found
at position 7 to 19.
With the sub-expression, a matched substring "simple" is found
at position 7 to 12.
****************************************************************************/
}