zrex(3)

zrex(3)

CZMQ Manual - CZMQ/3.0.1

Name

zrex - 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 *
    zrex_new (const char *expression);

//  Destructor
CZMQ_EXPORT void
    zrex_destroy (zrex_t **self_p);

//  Return true if the expression was valid and compiled without errors.
CZMQ_EXPORT bool
    zrex_valid (zrex_t *self);

//  Return the error message generated during compilation of the expression.
CZMQ_EXPORT const char *
    zrex_strerror (zrex_t *self);

//  Returns true if the text matches the previously compiled expression.
//  Use this method to compare one expression against many strings.
CZMQ_EXPORT bool
    zrex_matches (zrex_t *self, const char *text);

//  Returns true if the text matches the supplied expression. Use this
//  method to compare one string against several expressions.
CZMQ_EXPORT bool
    zrex_eq (zrex_t *self, const char *text, const char *expression);

//  Returns number of hits from last zrex_matches or zrex_eq. If the text
//  matched, returns 1 plus the number of capture groups. If the text did
//  not match, returns zero. To retrieve individual capture groups, call
//  zrex_hit ().
CZMQ_EXPORT int
    zrex_hits (zrex_t *self);

//  Returns the Nth capture group from the last expression match, where
//  N is 0 to the value returned by zrex_hits(). Capture group 0 is the
//  whole matching string. Sequence 1 is the first capture group, if any,
//  and so on.
CZMQ_EXPORT const char *
    zrex_hit (zrex_t *self, uint index);

//  Fetches hits into string variables provided by caller; this makes for
//  nicer code than accessing hits by index. Caller should not modify nor
//  free the returned values. Returns number of strings returned. This
//  method starts at hit 1, i.e. first capture group, as hit 0 is always
//  the original matched string.
CZMQ_EXPORT int
    zrex_fetch (zrex_t *self, const char **string_p, ...);

//  Self test of this class
CZMQ_EXPORT void
    zrex_test (bool verbose);

Description

Wraps 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.

Example

From 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);

Authors

The czmq manual was written by the authors in the AUTHORS file.

Resources

Main web site:

Report bugs to the email <gro.qmorez.stsil|ved-qmorez#gro.qmorez.stsil|ved-qmorez>

Copyright

Copyright (c) 1991-2012 iMatix Corporation — http://www.imatix.com Copyright other contributors as noted in the AUTHORS file. This file is part of CZMQ, the high-level C binding for ØMQ: 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.