In a C code, I need to check if a pattern matches on at least one line of a file. I need a caret in my expression. What I have is:
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;
}
For some reason, the caret seems ignored, as the following line matches the regexp (and should not):
// update the package block define template (the container for all other
I tried a lot of things, but could not get this working. What am I doing wrong?
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. Theforloop 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.