Debian Codegraph @

doc/source/reference/regular_expression.rst

raw
1 .. -*- rst -*-
2
3 .. groonga-command
4 .. database: regular_expression
5
6 Regular expression
7 ==================
8
9 Summary
10 -------
11
12 .. note::
13
14 Regular expression support is an experimental feature.
15
16 .. versionadded:: 5.0.1
17
18 Groonga supports pattern match by regular expression. Regular
19 expression is widely used format to describe a pattern. Regular
20 expression is useful to represent complex pattern.
21
22 In most cases, pattern match by regular expression is evaluated as
23 sequential search. It'll be slow for many records and many texts.
24
25 In some cases, pattern match by regular expression can be evaluated
26 by index. It's very fast rather than sequential search. Patterns
27 that can be evaluated by index are described later.
28
29 .. versionadded:: 5.0.7
30
31 Groonga normalizes match target text by :ref:`normalizer-auto`
32 normalizer when Groonga doesn't use index for regular expression
33 search. It means that regular expression that has upper case such
34 as ``Groonga`` never match. Because :ref:`normalizer-auto`
35 normalizer normalizes all alphabets to lower case. ``groonga``
36 matches to both ``Groonga`` and ``groonga``.
37
38 Why is match target text normalized? It's for increasing index
39 search-able patterns. If Groonga doesn't normalize match target
40 text, you need to write complex regular expression such as
41 ``[Dd][Ii][Ss][Kk]`` and ``(?i)disk`` for case-insensitive match.
42 Groonga can't use index against complex regular expression.
43
44 If you write ``disk`` regular expression for case-insensitive
45 match, Groonga can search the pattern with index. It's fast.
46
47 By full text search normally, Groonga normalize search keywords
48 using the normalizer specified in a lexicon.
49 By using regular expression search, Groonga doesn't normalize search
50 keywords.
51 Because the regular expression has specified meaning in uppercase and
52 lowercase.
53
54 So, if you regular expression search that doesn't use the index,
55 we suggest that use :doc:`/reference/commands/normalize` command to
56 normalize search keywords before a search.
57 By using :doc:`/reference/commands/normalize` command, you don't have
58 to need to think about how to normalize search keywords.
59
60 You may feel the behavior is strange. But fast search based on this
61 behavior will help you.
62
63 There are many regular expression syntaxes. Groonga uses the same
64 syntax in Ruby. Because Groonga uses the same regular expression
65 engine as Ruby. The regular expression engine is `Onigmo
66 <https://github.com/k-takata/Onigmo/>`_. Characteristic difference
67 with other regular expression syntax is ``^`` and ``$``. The regular
68 expression syntax in Ruby, ``^`` means the beginning of line and ``$``
69 means the end of line. ``^`` means the beginning of text and ``$``
70 means the end of text in other most regular expression syntaxes. The regular
71 expression syntax in Ruby uses ``\A`` for the beginning of text and
72 ``\z`` for the end of text.
73
74 .. versionadded:: 5.0.6
75
76 Groonga uses multiline mode since 5.0.6. It means that ``.``
77 matches on ``\n``.
78
79 But it's meaningless. Because ``\n`` is removed by
80 :ref:`normalizer-auto` normalizer.
81
82 You can use regular expression in :ref:`select-query` and
83 :ref:`select-filter` options of :doc:`/reference/commands/select`
84 command.
85
86 Usage
87 -----
88
89 Here are a schema definition and sample data to show usage. There is
90 only one table, ``Logs``. ``Logs`` table has only ``message``
91 column. Log messages are stored into the ``message`` column.
92
93 .. groonga-command
94 .. include:: ../example/reference/regular_expression/usage_setup.log
95 .. table_create Logs TABLE_NO_KEY
96 .. column_create Logs message COLUMN_SCALAR Text
97 ..
98 .. load --table Logs
99 .. [
100 .. {"message": "host1:[error]: No memory"},
101 .. {"message": "host1:[warning]: Remained disk space is less than 30%"},
102 .. {"message": "host1:[error]: Disk full"},
103 .. {"message": "host2:[error]: No memory"},
104 .. {"message": "host2:[info]: Shutdown"}
105 .. ]
106
107 Here is an example that uses regular expression in
108 :ref:`select-query`. You need to use
109 ``${COLUMN}:~${REGULAR_EXPRESSION}`` syntax.
110
111 .. groonga-command
112 .. include:: ../example/reference/regular_expression/usage_query.log
113 .. select Logs --query 'message:~"disk (space|full)"'
114
115 Here is an example that uses regular expression in
116 :ref:`select-filter`. You need to use
117 ``${COLUMN} @~ ${REGULAR_EXPRESSION}`` syntax.
118
119 .. groonga-command
120 .. include:: ../example/reference/regular_expression/usage_filter.log
121 .. select Logs --filter 'message @~ "disk (space|full)"'
122
123 .. _regular-expression-index:
124
125 Index
126 -----
127
128 Groonga can search records by regular expression with index. It's very
129 fast rather than sequential search.
130
131 But it doesn't support all regular expression patterns. It supports
132 only the following regular expression patterns. The patterns will be
133 increased in the future.
134
135 * Literal only pattern such as ``disk``
136 * The begging of text and literal only pattern such as ``\Adisk``
137 * The end of text and literal only pattern such as ``disk\z``
138
139 You need to create an index for fast regular expression search. Here
140 are requirements of index:
141
142 * Lexicon must be :ref:`table-pat-key` table.
143 * Lexicon must use :ref:`token-regexp` tokenizer.
144 * Index column must has ``WITH_POSITION`` flag.
145
146 Other configurations such as lexicon's normalizer are optional. You
147 can choose what you like. If you want to use case-insensitive search,
148 use :ref:`normalizer-auto` normalizer.
149
150 Here are recommended index definitions. In general, it's reasonable
151 index definitions.
152
153 .. groonga-command
154 .. include:: ../example/reference/regular_expression/index_definitions.log
155 .. table_create RegexpLexicon TABLE_PAT_KEY ShortText \
156 .. --default_tokenizer TokenRegexp \
157 .. --normalizer NormalizerAuto
158 .. column_create RegexpLexicon logs_message_index \
159 .. COLUMN_INDEX|WITH_POSITION Logs message
160
161 Now, you can use index for regular expression search. The following
162 regular expression can be evaluated by index because it uses only "the
163 beginning of text" and "literal".
164
165 .. groonga-command
166 .. include:: ../example/reference/regular_expression/search_by_index_query.log
167 .. select Logs --query message:~\\\\Ahost1
168
169 Here is an example that uses :ref:`select-filter` instead of
170 :ref:`select-query`. It uses the same regular expression as the
171 previous example.
172
173 .. groonga-command
174 .. include:: ../example/reference/regular_expression/search_by_index_filter.log
175 .. select Logs --filter 'message @~ "\\\\Ahost1:"'
176
177 ``\`` escape will confuse you because there are some steps that
178 require escape between you and Groonga. Here are steps that require
179 ``\`` escape:
180
181 * Shell only when you pass Groonga command from command line the
182 following::
183
184 % groonga /tmp/db select Logs --filter '"message @~ \"\\\\Ahost1:"\"'
185
186 ``--filter '"message @~ \"\\\\Ahost1:\""'`` is evaluated as the
187 following two arguments by shell:
188
189 * ``--filter``
190 * ``"message @~ \"\\\\Ahost1:\""``
191
192 * Groonga command parser only when you pass Groonga command by
193 command line style (``COMMAND ARG1_VALUE ARG2_VALUE ...``) not
194 HTTP path style
195 (``/d/COMMAND?ARG1_NAME=ARG1_VALUE&ARG2_NAME=ARG3_VALUE``).
196
197 ``"message @~ \"\\\\Ahost1:\""`` is evaluated as the following
198 value by Groonga command parser:
199
200 * ``message @~ "\\Ahost1:"``
201
202 * :doc:`/reference/grn_expr` parser. ``\`` escape is required in both
203 :doc:`/reference/grn_expr/query_syntax` and
204 :doc:`/reference/grn_expr/script_syntax`.
205
206 ``"\\Ahost1:"`` string literal in script syntax is evaluated as
207 the following value:
208
209 * ``\Ahost1``
210
211 The value is evaluated as regular expression.
212
213 .. _regular-expression-syntax:
214
215 Syntax
216 ------
217
218 This section describes about only commonly used syntaxes. See `Onigmo
219 syntax documentation
220 <https://github.com/k-takata/Onigmo/blob/master/doc/RE>`_ for other
221 syntaxes and details.
222
223 .. _regular-expression-escape:
224
225 Escape
226 ^^^^^^
227
228 In regular expression, there are the following special characters:
229
230 * ``\``
231 * ``|``
232 * ``(``
233 * ``)``
234 * ``[``
235 * ``]``
236 * ``.``
237 * ``*``
238 * ``+``
239 * ``?``
240 * ``{``
241 * ``}``
242 * ``^``
243 * ``$``
244
245 If you want to write pattern that matches these special character as
246 is, you need to escape them.
247
248 You can escape them by putting ``\`` before special character. Here
249 are regular expressions that match special character itself:
250
251 * ``\\``
252 * ``\|``
253 * ``\(``
254 * ``\)``
255 * ``\[``
256 * ``\]``
257 * ``\.``
258 * ``\*``
259 * ``\+``
260 * ``\?``
261 * ``\{``
262 * ``\}``
263 * ``\^``
264 * ``\$``
265
266 .. groonga-command
267 .. include:: ../example/reference/regular_expression/choice.log
268 .. select Logs --filter 'message @~ "\\\\[error\\\\]"'
269
270 If your regular expression doesn't work as you expected, confirm that
271 some special characters are used without escaping.
272
273 .. _regular-expression-choice:
274
275 Choice
276 ^^^^^^
277
278 Choice syntax is ``A|B``. The regular expression matches when either
279 ``A`` pattern or ``B`` pattern is matched.
280
281 .. groonga-command
282 .. include:: ../example/reference/regular_expression/choice.log
283 .. select Logs --filter 'message @~ "warning|info"'
284
285 .. caution::
286
287 Regular expression that uses this syntax can't be evaluated by
288 index.
289
290 .. _regular-expression-group:
291
292 Group
293 ^^^^^
294
295 Group syntax is ``(...)``. Group provides the following features:
296
297 * Back reference
298 * Scope reducing
299
300 You can refer matched groups by ``\n`` (``n`` is the group number)
301 syntax. For example, ``e(r)\1o\1`` matches ``error``. Because ``\1``
302 is replaced with match result (``r``) of the first group ``(r)``.
303
304 .. groonga-command
305 .. include:: ../example/reference/regular_expression/group_back_reference.log
306 .. select Logs --filter 'message @~ "e(r)\\\\1o\\\\1"'
307
308 You can also use more powerful back reference features. See `"8. Back
309 reference" section in Onigmo documentation
310 <https://github.com/k-takata/Onigmo/blob/master/doc/RE#L302>`_ for
311 details.
312
313 Group syntax reduces scope. For example, ``\[(warning|info)\]``
314 reduces choice syntax scope. The regular expression matches
315 ``[warning]`` and ``[info]``.
316
317 .. groonga-command
318 .. include:: ../example/reference/regular_expression/group_scope_reducing.log
319 .. select Logs --filter 'message @~ "\\\\[(warning|info)\\\\]"'
320
321 You can also use more powerful group related features. See
322 `"7. Extended groups" section in Onigmo documentation
323 <https://github.com/k-takata/Onigmo/blob/master/doc/RE#L225>`_ for
324 details.
325
326 .. caution::
327
328 Regular expression that uses this syntax can't be evaluated by
329 index.
330
331 .. _regular-expression-character-class:
332
333 Character class
334 ^^^^^^^^^^^^^^^
335
336 Character class syntax is ``[...]``. Character class is useful to
337 specify multiple characters to be matched.
338
339 For example, ``[12]`` matches ``1`` or ``2``.
340
341 .. groonga-command
342 .. include:: ../example/reference/regular_expression/character_class_characters.log
343 .. select Logs --filter 'message @~ "host[12]"'
344
345 You can specify characters by range. For example, ``[0-9]`` matches
346 one digit.
347
348 .. groonga-command
349 .. include:: ../example/reference/regular_expression/character_class_range.log
350 .. select Logs --filter 'message @~ "[0-9][0-9]%"'
351
352 You can also use more powerful character class related features. See
353 `"6. Character class" section in Onigmo documentation
354 <https://github.com/k-takata/Onigmo/blob/master/doc/RE#L164>`_ for
355 details.
356
357 .. caution::
358
359 Regular expression that uses this syntax can't be evaluated by
360 index.
361
362 .. _regular-expression-anchor:
363
364 Anchor
365 ^^^^^^
366
367 There are the following commonly used anchor syntaxes. Some anchors
368 can be evaluated by index.
369
370 .. list-table::
371 :header-rows: 1
372
373 * - Anchor
374 - Description
375 - Index ready
376 * - ``^``
377 - The beginning of line
378 - \-
379 * - ``$``
380 - The end of line
381 - \-
382 * - ``\A``
383 - The beginning of text
384 - ✓
385 * - ``\z``
386 - The end of text
387 - ✓
388
389 Here is an example that uses ``\z``.
390
391 .. groonga-command
392 .. include:: ../example/reference/regular_expression/anchor_z.log
393 .. select Logs --filter 'message @~ "%\\\\z"'
394
395 You can also use more anchors. See `"5. Anchors" section in Onigmo
396 documentation
397 <https://github.com/k-takata/Onigmo/blob/master/doc/RE#L152>`_ for
398 details.
399
400 .. caution::
401
402 Regular expression that uses this syntax except ``\A`` and ``\z``
403 can't be evaluated by index.
404
405 .. _regular-expression-quantifier:
406
407 Quantifier
408 ^^^^^^^^^^
409
410 There are the following commonly used quantifier syntaxes.
411
412 .. list-table::
413 :header-rows: 1
414
415 * - Quantifier
416 - Description
417 * - ``?``
418 - 0 or 1 time
419 * - ``*``
420 - 0 or more times
421 * - ``+``
422 - 1 or more times
423
424 For example, ``er+or`` matches ``error``, ``errror`` and so on.
425
426 .. groonga-command
427 .. include:: ../example/reference/regular_expression/quantifier_plus.log
428 .. select Logs --filter 'message @~ "er+or"'
429
430 You can also use more quantifiers. See `"4. Quantifier" section in Onigmo
431 documentation
432 <https://github.com/k-takata/Onigmo/blob/master/doc/RE#L119>`_ for
433 details.
434
435 .. caution::
436
437 Regular expression that uses this syntax can't be evaluated by
438 index.
439
440 Others
441 ^^^^^^
442
443 There are more syntaxes. If you're interested in them, see `Onigmo
444 documentation
445 <https://github.com/k-takata/Onigmo/blob/master/doc/RE>`_ for
446 details. You may be interested in "character type" and "character"
447 syntaxes.