Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_pcre.c

Nilorea Library pcre api test.

Nilorea Library pcre api test

Author
Castagnier Mickael
Version
1.0
Date
04/12/2019
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "nilorea/n_log.h"
#include "nilorea/n_pcre.h"
int main(int argc, char** argv) {
n_log(LOG_INFO, "Starting !");
if (argc < 3) {
n_log(LOG_ERR, "Not enough args\nUsage:\n %s \"regexp\" \"string\"", argv[0]);
exit(1);
}
N_PCRE* pcre = npcre_new(argv[1], 0);
if (npcre_match_capture(argv[2], pcre) == TRUE) {
n_log(LOG_INFO, "MATCHED !");
if (pcre->captured > 1) {
n_log(LOG_INFO, "CAPTURED !");
int it = 0;
while (pcre->match_list[it]) {
n_log(LOG_INFO, "Match[%d]:%s", it, pcre->match_list[it]);
it++;
}
}
} else {
n_log(LOG_INFO, "NO MATCH !");
}
/* test npcre_clean_match to clear previous match data */
n_log(LOG_INFO, "npcre_clean_match done, captured: %d", pcre->captured);
/* reuse the same pattern with a second match */
if (npcre_match_capture("another test string", pcre) == TRUE) {
n_log(LOG_INFO, "Second match succeeded");
} else {
n_log(LOG_INFO, "Second match: no match");
}
npcre_delete(&pcre);
exit(0);
}
int main(void)
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
#define LOG_STDERR
internal, default LOG_TYPE
Definition n_log.h:49
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_INFO
informational
Definition n_log.h:81
PCRE2_UCHAR8 ** match_list
populated match list (NULL-terminated) after npcre_match_capture() Allocated by PCRE2 via pcre2_subst...
Definition n_pcre.h:104
int captured
flag for match_list cleaning
Definition n_pcre.h:106
N_PCRE * npcre_new(char *str, int flags)
From pcre doc, the flag bits are: PCRE_ANCHORED Force pattern anchoring PCRE_AUTO_CALLOUT Compile aut...
Definition n_pcre.c:80
int npcre_match_capture(char *str, N_PCRE *pcre)
Return TRUE if str matches regexp, and make captures.
Definition n_pcre.c:261
int npcre_clean_match(N_PCRE *pcre)
clean the match list of the last capture, if any
Definition n_pcre.c:164
int npcre_delete(N_PCRE **pcre)
Free a N_PCRE pointer.
Definition n_pcre.c:134
N_PCRE structure.
Definition n_pcre.h:93
Generic log system.
PCRE helpers for regex matching.