|
NAMEzrex - Class for work with regular expressions SYNOPSIS// Constructor. Optionally, sets an expression against which we can match // text and capture hits. If there is an error in the expression, reports // zrex_valid() as false and provides the error in zrex_strerror(). If you // set a pattern, you can call zrex_matches() to test it against text. CZMQ_EXPORT zrex_t * DESCRIPTIONWraps a very simple regular expression library (SLRE) as a CZMQ class. Supports this syntax: ^ Match beginning of a buffer $ Match end of a buffer () Grouping and substring capturing [...] Match any character from set [^...] Match any character but ones from set . Match any character \s Match whitespace \S Match non-whitespace \d Match decimal digit \D Match non decimal digit \a Match alphabetic character \A Match non-alphabetic character \w Match alphanumeric character \W Match non-alphanumeric character \r Match carriage return \n Match newline + Match one or more times (greedy) +? Match one or more times (non-greedy) * Match zero or more times (greedy) *? Match zero or more times (non-greedy) ? Match zero or once \xDD Match byte with hex value 0xDD \meta Match one of the meta character: ^$().[*+?\ Please add @discuss section in ./../src/zrex.c. EXAMPLEFrom zrex_test method. // This shows the pattern of matching many lines to a single pattern
zrex_t *rex = zrex_new ("\\d+-\\d+-\\d+");
assert (rex);
assert (zrex_valid (rex));
bool matches = zrex_matches (rex, "123-456-789");
assert (matches);
assert (zrex_hits (rex) == 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 ("(\\d+)-(\\d+)-(\\d+)");
assert (rex);
assert (zrex_valid (rex));
matches = zrex_matches (rex, "123-456-ABC");
assert (!matches);
matches = zrex_matches (rex, "123-456-789");
assert (matches);
assert (zrex_hits (rex) == 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
assert (rex);
char *input = "Mechanism: CURVE";
matches = zrex_eq (rex, input, "Version: (.+)");
assert (!matches);
assert (zrex_hits (rex) == 0);
matches = zrex_eq (rex, input, "Mechanism: (.+)");
assert (matches);
assert (zrex_hits (rex) == 2);
const char *mechanism;
zrex_fetch (rex, &mechanism, NULL);
assert (streq (zrex_hit (rex, 1), "CURVE"));
assert (streq (mechanism, "CURVE"));
zrex_destroy (&rex);
#if defined (__WINDOWS__)
zsys_shutdown();
#endif
AUTHORSThe czmq manual was written by the authors in the AUTHORS file. RESOURCESMain web site: Report bugs to the email <zeromq-dev@lists.zeromq.org[1]> COPYRIGHTCopyright (c) the Contributors as noted in the AUTHORS file. This file is part of CZMQ, the high-level C binding for 0MQ: http://czmq.zeromq.org. This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. LICENSE included with the czmq distribution. NOTES
mailto:zeromq-dev@lists.zeromq.org
|