I have the following function to compile a pcre
regex:
/**
* common options: PCRE_DOTALL, PCRE_EXTENDED, PCRE_CASELESS, PCRE_MULTILINE
* full options located at: https://man7.org/linux/man-pages/man3/pcre_compile.3.html
*/
pcre* pcre_compile_pattern(const char* pattern, int options)
{
const char *pcre_error;
int error_offset;
pcre *re_compiled = pcre_compile(pattern, options, &pcre_error, &error_offset, NULL);
if (re_compiled == NULL) {
fprintf(stderr, "ERROR: '%s' occurs at pattern position %d\n", pcre_error, error_offset);
}
return re_compiled;
}
Is there a place where the pcre
struct is described? For example, I'm looking to see if it contains the pattern
(as a normal string) inside it or whether I have to keep the pattern separately. I've seen a lot of references in the man pages to pcre*
but I haven't really been able to get more details on that struct.
In searching github here was one place I was able to find it, which seems like it might be what I'm using: https://github.com/luvit/pcre/blob/e2a236a5737b58d43bf324208406a60fe0dd95f4/pcre_internal.h#L2317. Everything is private though so you cannot access part of the struct, for example to read/print it directly.
Is there a place where the
pcre
struct is described?
The include file defining the interface is pcre.h
for version 1 or pcre2.h
for version 2.
Much in the same way that we don't need to know how stdio's FILE struct is designed, we don't need to know how pcre is defined. We also will not need the pattern after we have received a pcre struct.
Shawn, in comments, pointed out the importance of using pcre2 for new code. It is also noted on the website: pcre is end of life with 8.45 the last version, use pcre2 for new projects.
The primary change for pcre2 is more aggressive pattern validation.
A demonstration of pcre2 is available here.