Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_pcre.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include "nilorea/n_log.h"
28#include "nilorea/n_pcre.h"
29
30int main(int argc, char** argv) {
33
34 n_log(LOG_INFO, "Starting !");
35
36 if (argc < 3) {
37 n_log(LOG_ERR, "Not enough args\nUsage:\n %s \"regexp\" \"string\"", argv[0]);
38 exit(1);
39 }
40 N_PCRE* pcre = npcre_new(argv[1], 0);
41 if (npcre_match_capture(argv[2], pcre) == TRUE) {
42 n_log(LOG_INFO, "MATCHED !");
43 if (pcre->captured > 1) {
44 n_log(LOG_INFO, "CAPTURED !");
45 int it = 0;
46 while (pcre->match_list[it]) {
47 n_log(LOG_INFO, "Match[%d]:%s", it, pcre->match_list[it]);
48 it++;
49 }
50 }
51 } else {
52 n_log(LOG_INFO, "NO MATCH !");
53 }
54
55 /* test npcre_clean_match to clear previous match data */
57 n_log(LOG_INFO, "npcre_clean_match done, captured: %d", pcre->captured);
58
59 /* reuse the same pattern with a second match */
60 if (npcre_match_capture("another test string", pcre) == TRUE) {
61 n_log(LOG_INFO, "Second match succeeded");
62 } else {
63 n_log(LOG_INFO, "Second match: no match");
64 }
65
66 npcre_delete(&pcre);
67
68 exit(0);
69}
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.