File: regular_expression.html

package info (click to toggle)
groonga 11.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 121,576 kB
  • sloc: ansic: 801,209; javascript: 62,121; ruby: 46,206; cpp: 33,790; xml: 24,951; yacc: 13,333; sh: 7,776; python: 3,266; makefile: 2,315; perl: 133
file content (963 lines) | stat: -rw-r--r-- 40,861 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963


<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>7.14. Regular expression &#8212; Groonga v10.1.1-31-g1e46ba6 documentation</title>
    <link rel="stylesheet" href="../_static/groonga.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../_static/language_data.js"></script>
    
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="7.15. Function" href="function.html" />
    <link rel="prev" title="7.13.2. Script syntax" href="grn_expr/script_syntax.html" /> 
  </head><body>
<div class="header">
  <h1 class="title">
    <a id="top-link" href="../index.html">
      <span class="project">groonga</span>
      <span class="separator">-</span>
      <span class="description">An open-source fulltext search engine and column store.</span>
    </a>
  </h1>

  <div class="other-language-links">
    <ul>
      <li><a href="../../../ja/html/reference/regular_expression.html">日本語</a></li>
    </ul>
  </div>
</div>
  

    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="function.html" title="7.15. Function"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="grn_expr/script_syntax.html" title="7.13.2. Script syntax"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Groonga v10.1.1-31-g1e46ba6 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../reference.html" accesskey="U"><span class="section-number">7. </span>Reference manual</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href=""><span class="section-number">7.14. </span>Regular expression</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="regular-expression">
<h1><span class="section-number">7.14. </span>Regular expression<a class="headerlink" href="#regular-expression" title="Permalink to this headline">¶</a></h1>
<div class="section" id="summary">
<h2><span class="section-number">7.14.1. </span>Summary<a class="headerlink" href="#summary" title="Permalink to this headline">¶</a></h2>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Regular expression support is an experimental feature.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">New in version 5.0.1.</span></p>
</div>
<p>Groonga supports pattern match by regular expression. Regular
expression is widely used format to describe a pattern. Regular
expression is useful to represent complex pattern.</p>
<p>In most cases, pattern match by regular expression is evaluated as
sequential search. It’ll be slow for many records and many texts.</p>
<p>In some cases, pattern match by regular expression can be evaluated
by index. It’s very fast rather than sequential search. Patterns
that can be evaluated by index are described later.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 5.0.7: </span>Groonga normalizes match target text by <a class="reference internal" href="normalizers/normalizer_auto.html#normalizer-auto"><span class="std std-ref">NormalizerAuto</span></a>
normalizer when Groonga doesn’t use index for regular expression
search. It means that regular expression that has upper case such
as <code class="docutils literal notranslate"><span class="pre">Groonga</span></code> never match. Because <a class="reference internal" href="normalizers/normalizer_auto.html#normalizer-auto"><span class="std std-ref">NormalizerAuto</span></a>
normalizer normalizes all alphabets to lower case. <code class="docutils literal notranslate"><span class="pre">groonga</span></code>
matches to both <code class="docutils literal notranslate"><span class="pre">Groonga</span></code> and <code class="docutils literal notranslate"><span class="pre">groonga</span></code>.</p>
<p>Why is match target text normalizered? It’s for increasing index
search-able patterns. If Groonga doesn’t normalize match target
text, you need to write complex regular expression such as
<code class="docutils literal notranslate"><span class="pre">[Dd][Ii][Ss][Kk]</span></code> and <code class="docutils literal notranslate"><span class="pre">(?i)disk</span></code> for case-insensitive match.
Groonga can’t use index against complex regular expression.</p>
<p>If you write <code class="docutils literal notranslate"><span class="pre">disk</span></code> regular expression for case-insensitive
match, Groonga can search the pattern with index. It’s fast.</p>
<p>By full text search normally, Groonga normalize search keywords
using the normalizer specified in a lexicon.
By using regular expression search, Groonga doesn’t normalize search
keywords.
Because the regular expression has specified meaning in uppercase and
lowercase.</p>
<p>So, if you regular expression search that doesn’t use the index,
we suggest that use <a class="reference internal" href="commands/normalize.html"><span class="doc">normalize</span></a> command to
normalize search keywords before a search.
By using <a class="reference internal" href="commands/normalize.html"><span class="doc">normalize</span></a> command, you don’t have
to need to think about how to normalize search keywords.</p>
<p>You may feel the behavior is strange. But fast search based on this
behavior will help you.</p>
</div>
<p>There are many regular expression syntaxes. Groonga uses the same
syntax in Ruby. Because Groonga uses the same regular expression
engine as Ruby. The regular expression engine is <a class="reference external" href="https://github.com/k-takata/Onigmo/">Onigmo</a>. Characteristic difference
with other regular expression syntax is <code class="docutils literal notranslate"><span class="pre">^</span></code> and <code class="docutils literal notranslate"><span class="pre">$</span></code>. The regular
expression syntax in Ruby, <code class="docutils literal notranslate"><span class="pre">^</span></code> means the beginning of line and <code class="docutils literal notranslate"><span class="pre">$</span></code>
means the end of line. <code class="docutils literal notranslate"><span class="pre">^</span></code> means the beginning of text and <code class="docutils literal notranslate"><span class="pre">$</span></code>
means the end of text in other most regular expression syntaxes. The regular
expression syntax in Ruby uses <code class="docutils literal notranslate"><span class="pre">\A</span></code> for the beginning of text and
<code class="docutils literal notranslate"><span class="pre">\z</span></code> for the end of text.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 5.0.6: </span>Groonga uses multiline mode since 5.0.6. It means that <code class="docutils literal notranslate"><span class="pre">.</span></code>
matches on <code class="docutils literal notranslate"><span class="pre">\n</span></code>.</p>
<p>But it’s meaningless. Because <code class="docutils literal notranslate"><span class="pre">\n</span></code> is removed by
<a class="reference internal" href="normalizers/normalizer_auto.html#normalizer-auto"><span class="std std-ref">NormalizerAuto</span></a> normalizer.</p>
</div>
<p>You can use regular expression in <a class="reference internal" href="commands/select.html#select-query"><span class="std std-ref">query</span></a> and
<a class="reference internal" href="commands/select.html#select-filter"><span class="std std-ref">filter</span></a> options of <a class="reference internal" href="commands/select.html"><span class="doc">select</span></a>
command.</p>
</div>
<div class="section" id="usage">
<h2><span class="section-number">7.14.2. </span>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2>
<p>Here are a schema definition and sample data to show usage. There is
only one table, <code class="docutils literal notranslate"><span class="pre">Logs</span></code>. <code class="docutils literal notranslate"><span class="pre">Logs</span></code> table has only <code class="docutils literal notranslate"><span class="pre">message</span></code>
column. Log messages are stored into the <code class="docutils literal notranslate"><span class="pre">message</span></code> column.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>table_create Logs TABLE_NO_KEY
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Logs message COLUMN_SCALAR Text
# [[0, 1337566253.89858, 0.000355720520019531], true]
load --table Logs
[
{&quot;message&quot;: &quot;host1:[error]: No memory&quot;},
{&quot;message&quot;: &quot;host1:[warning]: Remained disk space is less than 30%&quot;},
{&quot;message&quot;: &quot;host1:[error]: Disk full&quot;},
{&quot;message&quot;: &quot;host2:[error]: No memory&quot;},
{&quot;message&quot;: &quot;host2:[info]: Shutdown&quot;}
]
# [[0, 1337566253.89858, 0.000355720520019531], 5]
</pre></div>
</div>
<p>Here is an example that uses regular expression in
<a class="reference internal" href="commands/select.html#select-query"><span class="std std-ref">query</span></a>. You need to use
<code class="docutils literal notranslate"><span class="pre">${COLUMN}:~${REGULAR_EXPRESSION}</span></code> syntax.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --query &#39;message:~&quot;disk (space|full)&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         2
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         2,
#         &quot;host1:[warning]: Remained disk space is less than 30%&quot;
#       ],
#       [
#         3,
#         &quot;host1:[error]: Disk full&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>Here is an example that uses regular expression in
<a class="reference internal" href="commands/select.html#select-filter"><span class="std std-ref">filter</span></a>. You need to use
<code class="docutils literal notranslate"><span class="pre">${COLUMN}</span> <span class="pre">&#64;~</span> <span class="pre">${REGULAR_EXPRESSION}</span></code> syntax.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --filter &#39;message @~ &quot;disk (space|full)&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         2
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         2,
#         &quot;host1:[warning]: Remained disk space is less than 30%&quot;
#       ],
#       [
#         3,
#         &quot;host1:[error]: Disk full&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
</div>
<div class="section" id="index">
<span id="regular-expression-index"></span><h2><span class="section-number">7.14.3. </span>Index<a class="headerlink" href="#index" title="Permalink to this headline">¶</a></h2>
<p>Groonga can search records by regular expression with index. It’s very
fast rather than sequential search.</p>
<p>But it doesn’t support all regular expression patterns. It supports
only the following regular expression patterns. The patterns will be
increased in the future.</p>
<blockquote>
<div><ul class="simple">
<li><p>Literal only pattern such as <code class="docutils literal notranslate"><span class="pre">disk</span></code></p></li>
<li><p>The begging of text and literal only pattern such as <code class="docutils literal notranslate"><span class="pre">\Adisk</span></code></p></li>
<li><p>The end of text and literal only pattern such as <code class="docutils literal notranslate"><span class="pre">disk\z</span></code></p></li>
</ul>
</div></blockquote>
<p>You need to create an index for fast regular expression search. Here
are requirements of index:</p>
<blockquote>
<div><ul class="simple">
<li><p>Lexicon must be <a class="reference internal" href="tables.html#table-pat-key"><span class="std std-ref">TABLE_PAT_KEY</span></a> table.</p></li>
<li><p>Lexicon must use <a class="reference internal" href="tokenizers/token_regexp.html#token-regexp"><span class="std std-ref">TokenRegexp</span></a> tokenizer.</p></li>
<li><p>Index column must has <code class="docutils literal notranslate"><span class="pre">WITH_POSITION</span></code> flag.</p></li>
</ul>
</div></blockquote>
<p>Other configurations such as lexicon’s normalizer are optional. You
can choose what you like. If you want to use case-insensitive search,
use <a class="reference internal" href="normalizers/normalizer_auto.html#normalizer-auto"><span class="std std-ref">NormalizerAuto</span></a> normalizer.</p>
<p>Here are recommended index definitions. In general, it’s reasonable
index definitions.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>table_create RegexpLexicon TABLE_PAT_KEY ShortText \
  --default_tokenizer TokenRegexp \
  --normalizer NormalizerAuto
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create RegexpLexicon logs_message_index \
  COLUMN_INDEX|WITH_POSITION Logs message
# [[0, 1337566253.89858, 0.000355720520019531], true]
</pre></div>
</div>
<p>Now, you can use index for regular expression search. The following
regular expression can be evaluated by index because it uses only “the
beginning of text” and “literal”.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --query message:~\\\\Ahost1
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         3
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         1,
#         &quot;host1:[error]: No memory&quot;
#       ],
#       [
#         2,
#         &quot;host1:[warning]: Remained disk space is less than 30%&quot;
#       ],
#       [
#         3,
#         &quot;host1:[error]: Disk full&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>Here is an example that uses <a class="reference internal" href="commands/select.html#select-filter"><span class="std std-ref">filter</span></a> instead of
<a class="reference internal" href="commands/select.html#select-query"><span class="std std-ref">query</span></a>. It uses the same regular expression as the
previous example.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --filter &#39;message @~ &quot;\\\\Ahost1:&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         3
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         1,
#         &quot;host1:[error]: No memory&quot;
#       ],
#       [
#         2,
#         &quot;host1:[warning]: Remained disk space is less than 30%&quot;
#       ],
#       [
#         3,
#         &quot;host1:[error]: Disk full&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">\</span></code> escape will confuse you because there are some steps that
require escape between you and Groonga. Here are steps that require
<code class="docutils literal notranslate"><span class="pre">\</span></code> escape:</p>
<blockquote>
<div><ul>
<li><p>Shell only when you pass Groonga command from command line the
following:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>% groonga /tmp/db select Logs --filter &#39;&quot;message @~ \&quot;\\\\Ahost1:&quot;\&quot;&#39;
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">--filter</span> <span class="pre">'&quot;message</span> <span class="pre">&#64;~</span> <span class="pre">\&quot;\\\\Ahost1:\&quot;&quot;'</span></code> is evaluated as the
following two arguments by shell:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">--filter</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&quot;message</span> <span class="pre">&#64;~</span> <span class="pre">\&quot;\\\\Ahost1:\&quot;&quot;</span></code></p></li>
</ul>
</div></blockquote>
</li>
<li><p>Groonga command parser only when you pass Groonga command by
command line style (<code class="docutils literal notranslate"><span class="pre">COMMAND</span> <span class="pre">ARG1_VALUE</span> <span class="pre">ARG2_VALUE</span> <span class="pre">...</span></code>) not
HTTP path style
(<code class="docutils literal notranslate"><span class="pre">/d/COMMAND?ARG1_NAME=ARG1_VALUE&amp;ARG2_NAME=ARG3_VALUE</span></code>).</p>
<p><code class="docutils literal notranslate"><span class="pre">&quot;message</span> <span class="pre">&#64;~</span> <span class="pre">\&quot;\\\\Ahost1:\&quot;&quot;</span></code> is evaluated as the following
value by Groonga command parser:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">message</span> <span class="pre">&#64;~</span> <span class="pre">&quot;\\Ahost1:&quot;</span></code></p></li>
</ul>
</div></blockquote>
</li>
<li><p><a class="reference internal" href="grn_expr.html"><span class="doc">grn_expr</span></a> parser. <code class="docutils literal notranslate"><span class="pre">\</span></code> escape is required in both
<a class="reference internal" href="grn_expr/query_syntax.html"><span class="doc">Query syntax</span></a> and
<a class="reference internal" href="grn_expr/script_syntax.html"><span class="doc">Script syntax</span></a>.</p>
<p><code class="docutils literal notranslate"><span class="pre">&quot;\\Ahost1:&quot;</span></code> string literal in script syntax is evaluated as
the following value:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">\Ahost1</span></code></p></li>
</ul>
</div></blockquote>
<p>The value is evaluated as regular expression.</p>
</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="syntax">
<span id="regular-expression-syntax"></span><h2><span class="section-number">7.14.4. </span>Syntax<a class="headerlink" href="#syntax" title="Permalink to this headline">¶</a></h2>
<p>This section describes about only commonly used syntaxes. See <a class="reference external" href="https://github.com/k-takata/Onigmo/blob/master/doc/RE">Onigmo
syntax documentation</a> for other
syntaxes and details.</p>
<div class="section" id="escape">
<span id="regular-expression-escape"></span><h3><span class="section-number">7.14.4.1. </span>Escape<a class="headerlink" href="#escape" title="Permalink to this headline">¶</a></h3>
<p>In regular expression, there are the following special characters:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">\</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">|</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">(</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">[</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">]</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">.</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">*</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">+</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">?</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">{</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">}</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">^</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">$</span></code></p></li>
</ul>
</div></blockquote>
<p>If you want to write pattern that matches these special character as
is, you need to escape them.</p>
<p>You can escape them by putting <code class="docutils literal notranslate"><span class="pre">\</span></code> before special character. Here
are regular expressions that match special character itself:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">\\</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\|</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\(</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\[</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\]</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\.</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\*</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\+</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\?</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\{</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\}</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\^</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\$</span></code></p></li>
</ul>
</div></blockquote>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --filter &#39;message @~ &quot;warning|info&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         2
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         2,
#         &quot;host1:[warning]: Remained disk space is less than 30%&quot;
#       ],
#       [
#         5,
#         &quot;host2:[info]: Shutdown&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>If your regular expression doesn’t work as you expected, confirm that
some special characters are used without escaping.</p>
</div>
<div class="section" id="choice">
<span id="regular-expression-choice"></span><h3><span class="section-number">7.14.4.2. </span>Choice<a class="headerlink" href="#choice" title="Permalink to this headline">¶</a></h3>
<p>Choice syntax is <code class="docutils literal notranslate"><span class="pre">A|B</span></code>. The regular expression matches when either
<code class="docutils literal notranslate"><span class="pre">A</span></code> pattern or <code class="docutils literal notranslate"><span class="pre">B</span></code> pattern is matched.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --filter &#39;message @~ &quot;warning|info&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         2
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         2,
#         &quot;host1:[warning]: Remained disk space is less than 30%&quot;
#       ],
#       [
#         5,
#         &quot;host2:[info]: Shutdown&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<div class="admonition caution">
<p class="admonition-title">Caution</p>
<p>Regular expression that uses this syntax can’t be evaluated by
index.</p>
</div>
</div>
<div class="section" id="group">
<span id="regular-expression-group"></span><h3><span class="section-number">7.14.4.3. </span>Group<a class="headerlink" href="#group" title="Permalink to this headline">¶</a></h3>
<p>Group syntax is <code class="docutils literal notranslate"><span class="pre">(...)</span></code>. Group provides the following features:</p>
<blockquote>
<div><ul class="simple">
<li><p>Back reference</p></li>
<li><p>Scope reducing</p></li>
</ul>
</div></blockquote>
<p>You can refer matched groups by <code class="docutils literal notranslate"><span class="pre">\n</span></code> (<code class="docutils literal notranslate"><span class="pre">n</span></code> is the group number)
syntax. For example, <code class="docutils literal notranslate"><span class="pre">e(r)\1o\1</span></code> matches <code class="docutils literal notranslate"><span class="pre">error</span></code>. Because <code class="docutils literal notranslate"><span class="pre">\1</span></code>
is replaced with match result (<code class="docutils literal notranslate"><span class="pre">r</span></code>) of the first group <code class="docutils literal notranslate"><span class="pre">(r)</span></code>.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --filter &#39;message @~ &quot;e(r)\\\\1o\\\\1&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         3
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         1,
#         &quot;host1:[error]: No memory&quot;
#       ],
#       [
#         3,
#         &quot;host1:[error]: Disk full&quot;
#       ],
#       [
#         4,
#         &quot;host2:[error]: No memory&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>You can also use more powerful back reference features. See <a class="reference external" href="https://github.com/k-takata/Onigmo/blob/master/doc/RE#L302">“8. Back
reference” section in Onigmo documentation</a> for
details.</p>
<p>Group syntax reduces scope. For example, <code class="docutils literal notranslate"><span class="pre">\[(warning|info)\]</span></code>
reduces choice syntax scope. The regular expression matches
<code class="docutils literal notranslate"><span class="pre">[warning]</span></code> and <code class="docutils literal notranslate"><span class="pre">[info]</span></code>.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --filter &#39;message @~ &quot;\\\\[(warning|info)\\\\]&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         2
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         2,
#         &quot;host1:[warning]: Remained disk space is less than 30%&quot;
#       ],
#       [
#         5,
#         &quot;host2:[info]: Shutdown&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>You can also use more powerful group related features. See
<a class="reference external" href="https://github.com/k-takata/Onigmo/blob/master/doc/RE#L225">“7. Extended groups” section in Onigmo documentation</a> for
details.</p>
<div class="admonition caution">
<p class="admonition-title">Caution</p>
<p>Regular expression that uses this syntax can’t be evaluated by
index.</p>
</div>
</div>
<div class="section" id="character-class">
<span id="regular-expression-character-class"></span><h3><span class="section-number">7.14.4.4. </span>Character class<a class="headerlink" href="#character-class" title="Permalink to this headline">¶</a></h3>
<p>Character class syntax is <code class="docutils literal notranslate"><span class="pre">[...]</span></code>. Character class is useful to
specify multiple characters to be matched.</p>
<p>For example, <code class="docutils literal notranslate"><span class="pre">[12]</span></code> matches <code class="docutils literal notranslate"><span class="pre">1</span></code> or <code class="docutils literal notranslate"><span class="pre">2</span></code>.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --filter &#39;message @~ &quot;host[12]&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         5
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         1,
#         &quot;host1:[error]: No memory&quot;
#       ],
#       [
#         2,
#         &quot;host1:[warning]: Remained disk space is less than 30%&quot;
#       ],
#       [
#         3,
#         &quot;host1:[error]: Disk full&quot;
#       ],
#       [
#         4,
#         &quot;host2:[error]: No memory&quot;
#       ],
#       [
#         5,
#         &quot;host2:[info]: Shutdown&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>You can specify characters by range. For example, <code class="docutils literal notranslate"><span class="pre">[0-9]</span></code> matches
one digit.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --filter &#39;message @~ &quot;[0-9][0-9]%&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         1
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         2,
#         &quot;host1:[warning]: Remained disk space is less than 30%&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>You can also use more powerful character class related features. See
<a class="reference external" href="https://github.com/k-takata/Onigmo/blob/master/doc/RE#L164">“6. Character class” section in Onigmo documentation</a> for
details.</p>
<div class="admonition caution">
<p class="admonition-title">Caution</p>
<p>Regular expression that uses this syntax can’t be evaluated by
index.</p>
</div>
</div>
<div class="section" id="anchor">
<span id="regular-expression-anchor"></span><h3><span class="section-number">7.14.4.5. </span>Anchor<a class="headerlink" href="#anchor" title="Permalink to this headline">¶</a></h3>
<p>There are the following commonly used anchor syntaxes. Some anchors
can be evaluated by index.</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 33%" />
<col style="width: 33%" />
<col style="width: 33%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Anchor</p></th>
<th class="head"><p>Description</p></th>
<th class="head"><p>Index ready</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">^</span></code></p></td>
<td><p>The beginning of line</p></td>
<td><p>o</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">$</span></code></p></td>
<td><p>The end of line</p></td>
<td><p>x</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\A</span></code></p></td>
<td><p>The beginning of text</p></td>
<td><p>o</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\z</span></code></p></td>
<td><p>The end of text</p></td>
<td><p>x</p></td>
</tr>
</tbody>
</table>
<p>Here is an example that uses <code class="docutils literal notranslate"><span class="pre">\z</span></code>.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --filter &#39;message @~ &quot;%\\\\z&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         1
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         2,
#         &quot;host1:[warning]: Remained disk space is less than 30%&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>You can also use more anchors. See <a class="reference external" href="https://github.com/k-takata/Onigmo/blob/master/doc/RE#L152">“5. Anchors” section in Onigmo
documentation</a> for
details.</p>
<div class="admonition caution">
<p class="admonition-title">Caution</p>
<p>Regular expression that uses this syntax except <code class="docutils literal notranslate"><span class="pre">\A</span></code> and <code class="docutils literal notranslate"><span class="pre">\z</span></code>
can’t be evaluated by index.</p>
</div>
</div>
<div class="section" id="quantifier">
<span id="regular-expression-quantifier"></span><h3><span class="section-number">7.14.4.6. </span>Quantifier<a class="headerlink" href="#quantifier" title="Permalink to this headline">¶</a></h3>
<p>There are the following commonly used quantifier syntaxes.</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Quantifier</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">?</span></code></p></td>
<td><p>0 or 1 time</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">*</span></code></p></td>
<td><p>0 or more times</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">+</span></code></p></td>
<td><p>1 or more times</p></td>
</tr>
</tbody>
</table>
<p>For example, <code class="docutils literal notranslate"><span class="pre">er+or</span></code> matches <code class="docutils literal notranslate"><span class="pre">error</span></code>, <code class="docutils literal notranslate"><span class="pre">errror</span></code> and so on.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Logs --filter &#39;message @~ &quot;er+or&quot;&#39;
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         3
#       ],
#       [
#         [
#           &quot;_id&quot;,
#           &quot;UInt32&quot;
#         ],
#         [
#           &quot;message&quot;,
#           &quot;Text&quot;
#         ]
#       ],
#       [
#         1,
#         &quot;host1:[error]: No memory&quot;
#       ],
#       [
#         3,
#         &quot;host1:[error]: Disk full&quot;
#       ],
#       [
#         4,
#         &quot;host2:[error]: No memory&quot;
#       ]
#     ]
#   ]
# ]
</pre></div>
</div>
<p>You can also use more quantifiers. See <a class="reference external" href="https://github.com/k-takata/Onigmo/blob/master/doc/RE#L119">“4. Quantifier” section in Onigmo
documentation</a> for
details.</p>
<div class="admonition caution">
<p class="admonition-title">Caution</p>
<p>Regular expression that uses this syntax can’t be evaluated by
index.</p>
</div>
</div>
<div class="section" id="others">
<h3><span class="section-number">7.14.4.7. </span>Others<a class="headerlink" href="#others" title="Permalink to this headline">¶</a></h3>
<p>There are more syntaxes. If you’re interested in them, see <a class="reference external" href="https://github.com/k-takata/Onigmo/blob/master/doc/RE">Onigmo
documentation</a> for
details. You may be interested in “character type” and “character”
syntaxes.</p>
</div>
</div>
</div>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">7.14. Regular expression</a><ul>
<li><a class="reference internal" href="#summary">7.14.1. Summary</a></li>
<li><a class="reference internal" href="#usage">7.14.2. Usage</a></li>
<li><a class="reference internal" href="#index">7.14.3. Index</a></li>
<li><a class="reference internal" href="#syntax">7.14.4. Syntax</a><ul>
<li><a class="reference internal" href="#escape">7.14.4.1. Escape</a></li>
<li><a class="reference internal" href="#choice">7.14.4.2. Choice</a></li>
<li><a class="reference internal" href="#group">7.14.4.3. Group</a></li>
<li><a class="reference internal" href="#character-class">7.14.4.4. Character class</a></li>
<li><a class="reference internal" href="#anchor">7.14.4.5. Anchor</a></li>
<li><a class="reference internal" href="#quantifier">7.14.4.6. Quantifier</a></li>
<li><a class="reference internal" href="#others">7.14.4.7. Others</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="grn_expr/script_syntax.html"
                        title="previous chapter"><span class="section-number">7.13.2. </span>Script syntax</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="function.html"
                        title="next chapter"><span class="section-number">7.15. </span>Function</a></p>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="function.html" title="7.15. Function"
             >next</a> |</li>
        <li class="right" >
          <a href="grn_expr/script_syntax.html" title="7.13.2. Script syntax"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Groonga v10.1.1-31-g1e46ba6 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../reference.html" ><span class="section-number">7. </span>Reference manual</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href=""><span class="section-number">7.14. </span>Regular expression</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2009-2021, Brazil, Inc.
    </div>
  </body>
</html>