6 Module PCRE
gullradriel edited this page 2026-10-29 20:54:40 +01:00

PCREs: regex matching helpers

Structures

Name
struct N_PCRE
N_PCRE structure.

Functions

Name
N_STR * npcre_replace(N_PCRE * pcre, const char * subject, size_t subject_len, const char * replacement)
thread-safe regex substitute: replace every match in subject (binary-safe, subject_len bytes) using a replacement template with $N / ${N} capture-group references (pcre2_substitute, global).
N_PCRE * npcre_new(char * str, int flags)
pcre helper, compile and optimize regexp
int npcre_match_capture(char * str, N_PCRE * pcre)
thread-safe pcre match with captures stored in pcre->match_list
int npcre_match(char * str, N_PCRE * pcre)
thread-safe pcre match, only returns TRUE/FALSE, no captures stored
int npcre_delete(N_PCRE ** pcre)
pcre helper, delete/free a compiled regexp
int npcre_clean_match(N_PCRE * pcre)
clean match results from a previous npcre_match_capture
int _npcre_print_error(int LOG_LEVEL, const char * file, const char * func, int line, int rc)
print error helper

Functions Documentation

function npcre_replace

N_STR * npcre_replace(
    N_PCRE * pcre,
    const char * subject,
    size_t subject_len,
    const char * replacement
)

thread-safe regex substitute: replace every match in subject (binary-safe, subject_len bytes) using a replacement template with $N / ${N} capture-group references (pcre2_substitute, global).

Returns a new N_STR (free with free_nstr), or NULL on error.

function npcre_new

N_PCRE * npcre_new(
    char * str,
    int flags
)

pcre helper, compile and optimize regexp

Parameters:

  • str The string containing the regexp
  • flags pcre_compile flags

Return: a filled N_PCRE struct or NULL

From pcre doc, the flag bits are: PCRE_ANCHORED Force pattern anchoring PCRE_AUTO_CALLOUT Compile automatic callouts PCRE_BSR_ANYCRLF \R matches only CR, LF, or CRLF PCRE_BSR_UNICODE \R matches all Unicode line endings PCRE_CASELESS Do caseless matching PCRE_DOLLAR_ENDONLY $ not to match newline at end PCRE_DOTALL .

pcre helper, compile and optimize regexp

matches anything including NL PCRE_DUPNAMES Allow duplicate names for subpatterns PCRE_EXTENDED Ignore white space and # comments PCRE_EXTRA PCRE extra features (not much use currently) PCRE_FIRSTLINE Force matching to be before newline PCRE_JAVASCRIPT_COMPAT JavaScript compatibility PCRE_MULTILINE ^ and $ match newlines within data PCRE_NEWLINE_ANY Recognize any Unicode newline sequence PCRE_NEWLINE_ANYCRLF Recognize CR, LF, and CRLF as newline sequences PCRE_NEWLINE_CR Set CR as the newline sequence PCRE_NEWLINE_CRLF Set CRLF as the newline sequence PCRE_NEWLINE_LF Set LF as the newline sequence PCRE_NO_AUTO_CAPTURE Disable numbered capturing paren- theses (named ones available) PCRE_NO_UTF16_CHECK Do not check the pattern for UTF-16 validity (only relevant if PCRE_UTF16 is set) PCRE_NO_UTF32_CHECK Do not check the pattern for UTF-32 validity (only relevant if PCRE_UTF32 is set) PCRE_NO_UTF8_CHECK Do not check the pattern for UTF-8 validity (only relevant if PCRE_UTF8 is set) PCRE_UCP Use Unicode properties for backslash-d, backslash-w, etc. PCRE_UNGREEDY Invert greediness of quantifiers PCRE_UTF16 Run in pcre16_compile() UTF-16 mode PCRE_UTF32 Run in pcre32_compile() UTF-32 mode PCRE_UTF8 Run in pcre_compile() UTF-8 mode

new N_PCRE object with given parameters

function npcre_match_capture

int npcre_match_capture(
    char * str,
    N_PCRE * pcre
)

thread-safe pcre match with captures stored in pcre->match_list

Parameters:

  • str String to test against the regexp
  • pcre The N_PCRE regexp holder

Return: TRUE or FALSE

Return TRUE if str matches regexp, and make captures.

thread-safe pcre match with captures stored in pcre->match_list

Thread-safe: uses a rwlock to serialize access to match_data, match_list, captured. The caller must read pcre->match_list/captured before the next call to npcre_match_capture() or npcre_clean_match() on the same N_PCRE, as those will overwrite/free the previous results.

function npcre_match

int npcre_match(
    char * str,
    N_PCRE * pcre
)

thread-safe pcre match, only returns TRUE/FALSE, no captures stored

Parameters:

  • str String to test against the regexp
  • pcre The N_PCRE regexp holder (read-only access to regexp field)

Return: TRUE or FALSE

Thread-safe match: returns TRUE/FALSE without modifying the N_PCRE struct.

thread-safe pcre match, only returns TRUE/FALSE, no captures stored

Uses a local pcre2_match_data so multiple threads can safely share the same compiled N_PCRE pattern. No captures are stored.

function npcre_delete

int npcre_delete(
    N_PCRE ** pcre
)

pcre helper, delete/free a compiled regexp

Parameters:

  • pcre The N_PCRE regexp holder

Return: TRUE or FALSE

Free a N_PCRE pointer.

pcre helper, delete/free a compiled regexp

function npcre_clean_match

int npcre_clean_match(
    N_PCRE * pcre
)

clean match results from a previous npcre_match_capture

Parameters:

  • pcre The N_PCRE regexp holder

Return: TRUE or FALSE

clean the match list of the last capture, if any

clean match results from a previous npcre_match_capture

function _npcre_print_error

int _npcre_print_error(
    int LOG_LEVEL,
    const char * file,
    const char * func,
    int line,
    int rc
)

print error helper

Parameters:

  • LOG_LEVEL The log level to use when printing errors. Note: non error will always be printed in LOG_DEBUG
  • file name of the file which called the function
  • func name of the calling function
  • line line in the file
  • rc the code to test

Return: TRUE if everything was OK and no errors, FALSE if an error was decoded, or if it couldn't get the exact error and printed default

print error associated to pcre error code

print error helper