regexec ()- 执行编译的正则表达式

格式

#include <regex.h>
int regexec(const regex_t *preg, const char *string,
            size_t nmatch, regmatch_t *pmatch, int eflags);

语言级别

XPG4

线程安全

语言环境敏感

此函数的行为可能受当前语言环境的 LC_CTYPE 和 LC_COLLATE 类别影响。 当在编译命令上指定 LOCALETYPE (*CLD) 时,此功能不可用。 有关更多信息,请参阅 了解 CCSID 和语言环境

描述

regexec() 函数将空结束的 string 与编译的正则表达式 preg 进行比较,以查找两者之间的匹配。

nmatch 值是 string regexec() 函数应尝试与 preg中的子表达式匹配的子字符串数。 您为 pmatch 提供的数组必须至少具有 nmatch 元素。

regexec() 函数使用 string 中的子串的偏移量来填充数组 pmatch 的元素,这些偏移量与提供给 regcomp() 函数以创建 preg的原始模式的括号内子表达式相对应。 数组的零元素对应于整个模式。 如果存在多个 nmatch 子表达式,那么仅存储第一个 nmatch-1 子表达式。 如果 nmatch 是 0 ,或者如果在使用 regcomp() 函数创建 preg 时设置了 REG_NOSUB 标志,那么 regexec() 函数将忽略 pmatch 自变量。

eflags 标志 定义 regexec() 函数的可定制行为:

错误标志 描述字符串
REG_NOTBOL 指示 string 的第一个字符不是行首。
REG_NOTEOL 指示 string 的第一个字符不是行尾。

当基本或扩展正则表达式匹配时,原始模式的任何给定括号内的子表达式都可以参与 string的多个不同子串的匹配。 以下规则确定在 pmatch中报告哪些子串:

  1. 如果正则表达式中的子表达式 i 未包含在另一个子表达式中,并且它多次参与了匹配,那么 pmatch [i] 中的字节偏移量将对最后一个此类匹配进行定界。
  2. 如果子表达式 i 不包含在另一个子表达式中,并且没有参与其他成功匹配,则 pmatch[i] 中的字节偏移量将为 -1 。 当满足下列任何条件时,子表达式不参与匹配:
    • *\{ \} 将立即显示在基本正则表达式中的子表达式之后。
    • *?{ } 立即显示在扩展正则表达式中的子表达式之后,并且该子表达式不匹配 (匹配 0 次)。
    • | 在扩展正则表达式中用于选择此子表达式或其他子表达式,并与其他子表达式匹配。
  3. 如果子表达式 i 包含在另一个子表达式 j中,而 i 未包含在 j中包含的任何其他子表达式中,并且在 pmatch [j]中报告了子表达式 j 的匹配,那么在 pmatch [i] 中报告的子表达式 i 的匹配或非匹配将如 1 中所述。 和 2。 但在 pmatch [j] 中报告的子串内,而不是整个字符串。
  4. 如果子表达式 i 包含在子表达式 j 中,并且 pmatch[j] 中的字节偏移量是 -1 ,那么 pmatch[i] 中的偏移量也将是 -1 。
  5. 如果子表达式 i 与零长度字符串匹配,那么 pmatch [i] 中的两个字节偏移量都将是紧跟在零长度字符串之后的字符或空终止符的字节偏移量。
如果在 regcomp() 函数创建 preg 时设置了 REG_NOSUB 标志,那么未指定 pmatch 的内容。 如果在创建 preg 时设置了 REG_NEWLINE 标志,那么允许在字符串中使用换行符。

返回值

如果找到匹配项,那么 regexec() 函数将返回 0。 如果找不到匹配项,那么 regexec() 函数将返回 REG_NOMATCH。 否则,它将返回指示错误的非零值。 非零返回值可以在对 regerror() 函数的调用中使用。

示例

#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.
   ****************************************************************************/
}

相关信息