0

在 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;
}

出于某种原因,插入符号似乎被忽略了,因为以下行与正则表达式匹配(并且不应该):

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

我尝试了很多东西,但无法使其正常工作。我究竟做错了什么?

4

3 回答 3

1

If you want to use PCRE_MULTILINE, you pass it the whole buffer as a single string, and it tells you whether there is a match anywhere in the string. The for loop is not only superfluous, it will erroneously make PCRE think it is looking at the beginning of the string (hence also beginning of line) when you pass it a position in the middle of the buffer.

于 2012-05-26T10:05:03.273 回答
0
于 2012-05-26T09:14:39.790 回答
0

For the record, I used @tripleee's answer and this is my final code:

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);

        /* regexp for checking for define and class declarations */
        if (pcre_exec(re, NULL, p, mystrnlen(p, 10000), 0, 0, NULL, 0) > -1)            
                return LANG_PUPPET;

        return LANG_PASCAL;
}
于 2012-05-26T11:10:22.497 回答