在C代码中,我需要检查模式是否在文件的至少一行上匹配。我的表情中需要插入符号。我所拥有的是:

const char *disambiguate_pp(SourceFile *sourcefile) {
        char *p = ohcount_sourcefile_get_contents(sourcefile);
  char *eof = p + ohcount_sourcefile_get_contents_size(sourcefile);

        /* prepare regular expressions */
        pcre *re;
        const char *error;
        int erroffset;
        re = pcre_compile("^\\s*(define\\s+[\\w:-]+\\s*\\(|class\\s+[\\w:-]+(\\s+inherits\\s+[\\w:-]+)?\\s*{|node\\s+\\'[\\w:\\.-]+\\'\\s*{)",
                          PCRE_MULTILINE, &error, &erroffset, NULL);

        for (; p < eof; p++) {
                if (pcre_exec(re, NULL, p, mystrnlen(p, 100), 0, 0, NULL, 0) > -1)
                        return LANG_PUPPET;

        }
        return LANG_PASCAL;
}


由于某些原因,插入符号似乎被忽略了,因为以下行与regexp匹配(并且不应该):

  // update the package block define template (the container for all other


我尝试了很多事情,但无法正常工作。我究竟做错了什么?

最佳答案

如果要使用PCRE_MULTILINE,则将整个缓冲区作为单个字符串传递给它,并告诉您字符串中是否有匹配项。 for循环不仅是多余的,而且还会错误地使PCRE认为,当您将其传递到缓冲区中间的位置时,它正在看字符串的开头(因此也是行的开头)。

10-07 22:28