int zrex_test (bool verbose) { printf (" * zrex: "); zrex_t *rex = zrex_new (""); assert (rex); assert (!zrex_valid (rex)); if (verbose) puts (zrex_strerror (rex)); zrex_destroy (&rex); // This shows the pattern of matching many lines to a single pattern rex = zrex_new ("[0-9]+\\-[0-9]+\\-[0-9]+"); assert (rex); int hits = zrex_hits (rex, "123-456-789"); assert (hits == 1); assert (streq (zrex_hit (rex, 0), "123-456-789")); assert (zrex_hit (rex, 1) == NULL); zrex_destroy (&rex); // Here we pick out hits using capture groups rex = zrex_new ("([0-9]+)\\-([0-9]+)\\-([0-9]+)"); assert (rex); assert (zrex_valid (rex)); hits = zrex_hits (rex, "123-456-ABC"); assert (hits == 0); hits = zrex_hits (rex, "123-456-789"); assert (hits == 4); assert (streq (zrex_hit (rex, 0), "123-456-789")); assert (streq (zrex_hit (rex, 1), "123")); assert (streq (zrex_hit (rex, 2), "456")); assert (streq (zrex_hit (rex, 3), "789")); zrex_destroy (&rex); // This shows the pattern of matching one line against many // patterns and then handling the case when it hits rex = zrex_new (NULL); // No initial pattern char *input = "Mechanism: CURVE"; if (zrex_eq (rex, input, "Version: (.+)")) assert (false); else if (zrex_eq (rex, input, "Mechanism: (.+)")) assert (streq (zrex_hit (rex, 1), "CURVE")); zrex_destroy (&rex); printf ("OK\n"); return 0; }
int main (int argc, char **argv) { if (argc < 2) { zsys_warning ("Need argument"); zsys_info ("usage: %s <regex>", argv [0]); return EXIT_FAILURE; } zrex_t *regex = zrex_new (argv [1]); if (!regex) { zsys_error ("zrex_new () failed."); return EXIT_FAILURE; } if (!zrex_valid (regex)) { zsys_error ("regex not valid: %s", zrex_strerror (regex)); return EXIT_FAILURE; } zsys_info ("zrex valid"); return EXIT_SUCCESS; }