1ZREX(3) CZMQ Manual ZREX(3)
2
3
4
6 zrex - Class for work with regular expressions
7
9 // Constructor. Optionally, sets an expression against which we can match
10 // text and capture hits. If there is an error in the expression, reports
11 // zrex_valid() as false and provides the error in zrex_strerror(). If you
12 // set a pattern, you can call zrex_matches() to test it against text.
13 CZMQ_EXPORT zrex_t *
14 zrex_new (const char *expression);
15
16 // Destructor
17 CZMQ_EXPORT void
18 zrex_destroy (zrex_t **self_p);
19
20 // Return true if the expression was valid and compiled without errors.
21 CZMQ_EXPORT bool
22 zrex_valid (zrex_t *self);
23
24 // Return the error message generated during compilation of the expression.
25 CZMQ_EXPORT const char *
26 zrex_strerror (zrex_t *self);
27
28 // Returns true if the text matches the previously compiled expression.
29 // Use this method to compare one expression against many strings.
30 CZMQ_EXPORT bool
31 zrex_matches (zrex_t *self, const char *text);
32
33 // Returns true if the text matches the supplied expression. Use this
34 // method to compare one string against several expressions.
35 CZMQ_EXPORT bool
36 zrex_eq (zrex_t *self, const char *text, const char *expression);
37
38 // Returns number of hits from last zrex_matches or zrex_eq. If the text
39 // matched, returns 1 plus the number of capture groups. If the text did
40 // not match, returns zero. To retrieve individual capture groups, call
41 // zrex_hit ().
42 CZMQ_EXPORT int
43 zrex_hits (zrex_t *self);
44
45 // Returns the Nth capture group from the last expression match, where
46 // N is 0 to the value returned by zrex_hits(). Capture group 0 is the
47 // whole matching string. Sequence 1 is the first capture group, if any,
48 // and so on.
49 CZMQ_EXPORT const char *
50 zrex_hit (zrex_t *self, uint index);
51
52 // Fetches hits into string variables provided by caller; this makes for
53 // nicer code than accessing hits by index. Caller should not modify nor
54 // free the returned values. Returns number of strings returned. This
55 // method starts at hit 1, i.e. first capture group, as hit 0 is always
56 // the original matched string.
57 CZMQ_EXPORT int
58 zrex_fetch (zrex_t *self, const char **string_p, ...);
59
60 // Self test of this class
61 CZMQ_EXPORT void
62 zrex_test (bool verbose);
63 Please add '@interface' section in './../src/zrex.c'.
64
66 Wraps a very simple regular expression library (SLRE) as a CZMQ class.
67 Supports this syntax:
68
69 ^ Match beginning of a buffer
70 $ Match end of a buffer
71 () Grouping and substring capturing
72 [...] Match any character from set
73 [^...] Match any character but ones from set
74 . Match any character
75 \s Match whitespace
76 \S Match non-whitespace
77 \d Match decimal digit
78 \D Match non decimal digit
79 \a Match alphabetic character
80 \A Match non-alphabetic character
81 \w Match alphanumeric character
82 \W Match non-alphanumeric character
83 \r Match carriage return
84 \n Match newline
85 + Match one or more times (greedy)
86 +? Match one or more times (non-greedy)
87 * Match zero or more times (greedy)
88 *? Match zero or more times (non-greedy)
89 ? Match zero or once
90 \xDD Match byte with hex value 0xDD
91 \meta Match one of the meta character: ^$().[*+?\
92
93 Please add @discuss section in ./../src/zrex.c.
94
96 From zrex_test method.
97
98 // This shows the pattern of matching many lines to a single pattern
99 zrex_t *rex = zrex_new ("\\d+-\\d+-\\d+");
100 assert (rex);
101 assert (zrex_valid (rex));
102 bool matches = zrex_matches (rex, "123-456-789");
103 assert (matches);
104 assert (zrex_hits (rex) == 1);
105 assert (streq (zrex_hit (rex, 0), "123-456-789"));
106 assert (zrex_hit (rex, 1) == NULL);
107 zrex_destroy (&rex);
108
109 // Here we pick out hits using capture groups
110 rex = zrex_new ("(\\d+)-(\\d+)-(\\d+)");
111 assert (rex);
112 assert (zrex_valid (rex));
113 matches = zrex_matches (rex, "123-456-ABC");
114 assert (!matches);
115 matches = zrex_matches (rex, "123-456-789");
116 assert (matches);
117 assert (zrex_hits (rex) == 4);
118 assert (streq (zrex_hit (rex, 0), "123-456-789"));
119 assert (streq (zrex_hit (rex, 1), "123"));
120 assert (streq (zrex_hit (rex, 2), "456"));
121 assert (streq (zrex_hit (rex, 3), "789"));
122 zrex_destroy (&rex);
123
124 // This shows the pattern of matching one line against many
125 // patterns and then handling the case when it hits
126 rex = zrex_new (NULL); // No initial pattern
127 assert (rex);
128 char *input = "Mechanism: CURVE";
129 matches = zrex_eq (rex, input, "Version: (.+)");
130 assert (!matches);
131 assert (zrex_hits (rex) == 0);
132 matches = zrex_eq (rex, input, "Mechanism: (.+)");
133 assert (matches);
134 assert (zrex_hits (rex) == 2);
135 const char *mechanism;
136 zrex_fetch (rex, &mechanism, NULL);
137 assert (streq (zrex_hit (rex, 1), "CURVE"));
138 assert (streq (mechanism, "CURVE"));
139 zrex_destroy (&rex);
140
141 #if defined (__WINDOWS__)
142 zsys_shutdown();
143 #endif
144
145
147 The czmq manual was written by the authors in the AUTHORS file.
148
150 Main web site:
151
152 Report bugs to the email <zeromq-dev@lists.zeromq.org[1]>
153
155 Copyright (c) the Contributors as noted in the AUTHORS file. This file
156 is part of CZMQ, the high-level C binding for 0MQ:
157 http://czmq.zeromq.org. This Source Code Form is subject to the terms
158 of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
159 distributed with this file, You can obtain one at
160 http://mozilla.org/MPL/2.0/. LICENSE included with the czmq
161 distribution.
162
164 1. zeromq-dev@lists.zeromq.org
165 mailto:zeromq-dev@lists.zeromq.org
166
167
168
169CZMQ 4.1.1 01/31/2019 ZREX(3)