~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/html/functions-matching.html

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
2
<HTML
 
3
><HEAD
 
4
><TITLE
 
5
>Pattern Matching</TITLE
 
6
><META
 
7
NAME="GENERATOR"
 
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
 
9
REV="MADE"
 
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
 
11
REL="HOME"
 
12
TITLE="PostgreSQL 9.1beta1 Documentation"
 
13
HREF="index.html"><LINK
 
14
REL="UP"
 
15
TITLE="Functions and Operators"
 
16
HREF="functions.html"><LINK
 
17
REL="PREVIOUS"
 
18
TITLE="Bit String Functions and Operators"
 
19
HREF="functions-bitstring.html"><LINK
 
20
REL="NEXT"
 
21
TITLE="Data Type Formatting Functions"
 
22
HREF="functions-formatting.html"><LINK
 
23
REL="STYLESHEET"
 
24
TYPE="text/css"
 
25
HREF="stylesheet.css"><META
 
26
HTTP-EQUIV="Content-Type"
 
27
CONTENT="text/html; charset=ISO-8859-1"><META
 
28
NAME="creation"
 
29
CONTENT="2011-04-27T21:20:33"></HEAD
 
30
><BODY
 
31
CLASS="SECT1"
 
32
><DIV
 
33
CLASS="NAVHEADER"
 
34
><TABLE
 
35
SUMMARY="Header navigation table"
 
36
WIDTH="100%"
 
37
BORDER="0"
 
38
CELLPADDING="0"
 
39
CELLSPACING="0"
 
40
><TR
 
41
><TH
 
42
COLSPAN="5"
 
43
ALIGN="center"
 
44
VALIGN="bottom"
 
45
><A
 
46
HREF="index.html"
 
47
>PostgreSQL 9.1beta1 Documentation</A
 
48
></TH
 
49
></TR
 
50
><TR
 
51
><TD
 
52
WIDTH="10%"
 
53
ALIGN="left"
 
54
VALIGN="top"
 
55
><A
 
56
TITLE="Bit String Functions and Operators"
 
57
HREF="functions-bitstring.html"
 
58
ACCESSKEY="P"
 
59
>Prev</A
 
60
></TD
 
61
><TD
 
62
WIDTH="10%"
 
63
ALIGN="left"
 
64
VALIGN="top"
 
65
><A
 
66
TITLE="Functions and Operators"
 
67
HREF="functions.html"
 
68
>Fast Backward</A
 
69
></TD
 
70
><TD
 
71
WIDTH="60%"
 
72
ALIGN="center"
 
73
VALIGN="bottom"
 
74
>Chapter 9. Functions and Operators</TD
 
75
><TD
 
76
WIDTH="10%"
 
77
ALIGN="right"
 
78
VALIGN="top"
 
79
><A
 
80
TITLE="Functions and Operators"
 
81
HREF="functions.html"
 
82
>Fast Forward</A
 
83
></TD
 
84
><TD
 
85
WIDTH="10%"
 
86
ALIGN="right"
 
87
VALIGN="top"
 
88
><A
 
89
TITLE="Data Type Formatting Functions"
 
90
HREF="functions-formatting.html"
 
91
ACCESSKEY="N"
 
92
>Next</A
 
93
></TD
 
94
></TR
 
95
></TABLE
 
96
><HR
 
97
ALIGN="LEFT"
 
98
WIDTH="100%"></DIV
 
99
><DIV
 
100
CLASS="SECT1"
 
101
><H1
 
102
CLASS="SECT1"
 
103
><A
 
104
NAME="FUNCTIONS-MATCHING"
 
105
>9.7. Pattern Matching</A
 
106
></H1
 
107
><P
 
108
>    There are three separate approaches to pattern matching provided
 
109
    by <SPAN
 
110
CLASS="PRODUCTNAME"
 
111
>PostgreSQL</SPAN
 
112
>: the traditional
 
113
    <ACRONYM
 
114
CLASS="ACRONYM"
 
115
>SQL</ACRONYM
 
116
> <CODE
 
117
CLASS="FUNCTION"
 
118
>LIKE</CODE
 
119
> operator, the
 
120
    more recent <CODE
 
121
CLASS="FUNCTION"
 
122
>SIMILAR TO</CODE
 
123
> operator (added in
 
124
    SQL:1999), and <ACRONYM
 
125
CLASS="ACRONYM"
 
126
>POSIX</ACRONYM
 
127
>-style regular
 
128
    expressions.  Aside from the basic <SPAN
 
129
CLASS="QUOTE"
 
130
>"does this string match
 
131
    this pattern?"</SPAN
 
132
> operators, functions are available to extract
 
133
    or replace matching substrings and to split a string at matching
 
134
    locations.
 
135
   </P
 
136
><DIV
 
137
CLASS="TIP"
 
138
><BLOCKQUOTE
 
139
CLASS="TIP"
 
140
><P
 
141
><B
 
142
>Tip: </B
 
143
>     If you have pattern matching needs that go beyond this,
 
144
     consider writing a user-defined function in Perl or Tcl.
 
145
    </P
 
146
></BLOCKQUOTE
 
147
></DIV
 
148
><DIV
 
149
CLASS="SECT2"
 
150
><H2
 
151
CLASS="SECT2"
 
152
><A
 
153
NAME="FUNCTIONS-LIKE"
 
154
>9.7.1. <CODE
 
155
CLASS="FUNCTION"
 
156
>LIKE</CODE
 
157
></A
 
158
></H2
 
159
><PRE
 
160
CLASS="SYNOPSIS"
 
161
><TT
 
162
CLASS="REPLACEABLE"
 
163
><I
 
164
>string</I
 
165
></TT
 
166
> LIKE <TT
 
167
CLASS="REPLACEABLE"
 
168
><I
 
169
>pattern</I
 
170
></TT
 
171
> [<SPAN
 
172
CLASS="OPTIONAL"
 
173
>ESCAPE <TT
 
174
CLASS="REPLACEABLE"
 
175
><I
 
176
>escape-character</I
 
177
></TT
 
178
></SPAN
 
179
>]
 
180
<TT
 
181
CLASS="REPLACEABLE"
 
182
><I
 
183
>string</I
 
184
></TT
 
185
> NOT LIKE <TT
 
186
CLASS="REPLACEABLE"
 
187
><I
 
188
>pattern</I
 
189
></TT
 
190
> [<SPAN
 
191
CLASS="OPTIONAL"
 
192
>ESCAPE <TT
 
193
CLASS="REPLACEABLE"
 
194
><I
 
195
>escape-character</I
 
196
></TT
 
197
></SPAN
 
198
>]</PRE
 
199
><P
 
200
>     The <CODE
 
201
CLASS="FUNCTION"
 
202
>LIKE</CODE
 
203
> expression returns true if the
 
204
     <TT
 
205
CLASS="REPLACEABLE"
 
206
><I
 
207
>string</I
 
208
></TT
 
209
> matches the supplied
 
210
     <TT
 
211
CLASS="REPLACEABLE"
 
212
><I
 
213
>pattern</I
 
214
></TT
 
215
>.  (As
 
216
     expected, the <CODE
 
217
CLASS="FUNCTION"
 
218
>NOT LIKE</CODE
 
219
> expression returns
 
220
     false if <CODE
 
221
CLASS="FUNCTION"
 
222
>LIKE</CODE
 
223
> returns true, and vice versa.
 
224
     An equivalent expression is
 
225
     <TT
 
226
CLASS="LITERAL"
 
227
>NOT (<TT
 
228
CLASS="REPLACEABLE"
 
229
><I
 
230
>string</I
 
231
></TT
 
232
> LIKE
 
233
      <TT
 
234
CLASS="REPLACEABLE"
 
235
><I
 
236
>pattern</I
 
237
></TT
 
238
>)</TT
 
239
>.)
 
240
    </P
 
241
><P
 
242
>     If <TT
 
243
CLASS="REPLACEABLE"
 
244
><I
 
245
>pattern</I
 
246
></TT
 
247
> does not contain percent
 
248
     signs or underscores, then the pattern only represents the string
 
249
     itself; in that case <CODE
 
250
CLASS="FUNCTION"
 
251
>LIKE</CODE
 
252
> acts like the
 
253
     equals operator.  An underscore (<TT
 
254
CLASS="LITERAL"
 
255
>_</TT
 
256
>) in
 
257
     <TT
 
258
CLASS="REPLACEABLE"
 
259
><I
 
260
>pattern</I
 
261
></TT
 
262
> stands for (matches) any single
 
263
     character; a percent sign (<TT
 
264
CLASS="LITERAL"
 
265
>%</TT
 
266
>) matches any sequence
 
267
     of zero or more characters.
 
268
    </P
 
269
><P
 
270
>    Some examples:
 
271
</P><PRE
 
272
CLASS="PROGRAMLISTING"
 
273
>'abc' LIKE 'abc'    <I
 
274
CLASS="LINEANNOTATION"
 
275
>true</I
 
276
>
 
277
'abc' LIKE 'a%'     <I
 
278
CLASS="LINEANNOTATION"
 
279
>true</I
 
280
>
 
281
'abc' LIKE '_b_'    <I
 
282
CLASS="LINEANNOTATION"
 
283
>true</I
 
284
>
 
285
'abc' LIKE 'c'      <I
 
286
CLASS="LINEANNOTATION"
 
287
>false</I
 
288
></PRE
 
289
><P>
 
290
   </P
 
291
><P
 
292
>    <CODE
 
293
CLASS="FUNCTION"
 
294
>LIKE</CODE
 
295
> pattern matching always covers the entire
 
296
    string.  Therefore, to match a sequence anywhere within a string, the
 
297
    pattern must start and end with a percent sign.
 
298
   </P
 
299
><P
 
300
>    To match a literal underscore or percent sign without matching
 
301
    other characters, the respective character in
 
302
    <TT
 
303
CLASS="REPLACEABLE"
 
304
><I
 
305
>pattern</I
 
306
></TT
 
307
> must be
 
308
    preceded by the escape character.  The default escape
 
309
    character is the backslash but a different one can be selected by
 
310
    using the <TT
 
311
CLASS="LITERAL"
 
312
>ESCAPE</TT
 
313
> clause.  To match the escape
 
314
    character itself, write two escape characters.
 
315
   </P
 
316
><P
 
317
>    Note that the backslash already has a special meaning in string literals,
 
318
    so to write a pattern constant that contains a backslash you must write two
 
319
    backslashes in an SQL statement (assuming escape string syntax is used, see
 
320
    <A
 
321
HREF="sql-syntax-lexical.html#SQL-SYNTAX-STRINGS"
 
322
>Section 4.1.2.1</A
 
323
>).  Thus, writing a pattern that
 
324
    actually matches a literal backslash means writing four backslashes in the
 
325
    statement.  You can avoid this by selecting a different escape character
 
326
    with <TT
 
327
CLASS="LITERAL"
 
328
>ESCAPE</TT
 
329
>; then a backslash is not special to
 
330
    <CODE
 
331
CLASS="FUNCTION"
 
332
>LIKE</CODE
 
333
> anymore. (But backslash is still special to the
 
334
    string literal parser, so you still need two of them to match a backslash.)
 
335
   </P
 
336
><P
 
337
>    It's also possible to select no escape character by writing
 
338
    <TT
 
339
CLASS="LITERAL"
 
340
>ESCAPE ''</TT
 
341
>.  This effectively disables the
 
342
    escape mechanism, which makes it impossible to turn off the
 
343
    special meaning of underscore and percent signs in the pattern.
 
344
   </P
 
345
><P
 
346
>    The key word <TT
 
347
CLASS="TOKEN"
 
348
>ILIKE</TT
 
349
> can be used instead of
 
350
    <TT
 
351
CLASS="TOKEN"
 
352
>LIKE</TT
 
353
> to make the match case-insensitive according
 
354
    to the active locale.  This is not in the <ACRONYM
 
355
CLASS="ACRONYM"
 
356
>SQL</ACRONYM
 
357
> standard but is a
 
358
    <SPAN
 
359
CLASS="PRODUCTNAME"
 
360
>PostgreSQL</SPAN
 
361
> extension.
 
362
   </P
 
363
><P
 
364
>    The operator <TT
 
365
CLASS="LITERAL"
 
366
>~~</TT
 
367
> is equivalent to
 
368
    <CODE
 
369
CLASS="FUNCTION"
 
370
>LIKE</CODE
 
371
>, and <TT
 
372
CLASS="LITERAL"
 
373
>~~*</TT
 
374
> corresponds to
 
375
    <CODE
 
376
CLASS="FUNCTION"
 
377
>ILIKE</CODE
 
378
>.  There are also
 
379
    <TT
 
380
CLASS="LITERAL"
 
381
>!~~</TT
 
382
> and <TT
 
383
CLASS="LITERAL"
 
384
>!~~*</TT
 
385
> operators that
 
386
    represent <CODE
 
387
CLASS="FUNCTION"
 
388
>NOT LIKE</CODE
 
389
> and <CODE
 
390
CLASS="FUNCTION"
 
391
>NOT
 
392
    ILIKE</CODE
 
393
>, respectively.  All of these operators are
 
394
    <SPAN
 
395
CLASS="PRODUCTNAME"
 
396
>PostgreSQL</SPAN
 
397
>-specific.
 
398
   </P
 
399
></DIV
 
400
><DIV
 
401
CLASS="SECT2"
 
402
><H2
 
403
CLASS="SECT2"
 
404
><A
 
405
NAME="FUNCTIONS-SIMILARTO-REGEXP"
 
406
>9.7.2. <CODE
 
407
CLASS="FUNCTION"
 
408
>SIMILAR TO</CODE
 
409
> Regular Expressions</A
 
410
></H2
 
411
><PRE
 
412
CLASS="SYNOPSIS"
 
413
><TT
 
414
CLASS="REPLACEABLE"
 
415
><I
 
416
>string</I
 
417
></TT
 
418
> SIMILAR TO <TT
 
419
CLASS="REPLACEABLE"
 
420
><I
 
421
>pattern</I
 
422
></TT
 
423
> [<SPAN
 
424
CLASS="OPTIONAL"
 
425
>ESCAPE <TT
 
426
CLASS="REPLACEABLE"
 
427
><I
 
428
>escape-character</I
 
429
></TT
 
430
></SPAN
 
431
>]
 
432
<TT
 
433
CLASS="REPLACEABLE"
 
434
><I
 
435
>string</I
 
436
></TT
 
437
> NOT SIMILAR TO <TT
 
438
CLASS="REPLACEABLE"
 
439
><I
 
440
>pattern</I
 
441
></TT
 
442
> [<SPAN
 
443
CLASS="OPTIONAL"
 
444
>ESCAPE <TT
 
445
CLASS="REPLACEABLE"
 
446
><I
 
447
>escape-character</I
 
448
></TT
 
449
></SPAN
 
450
>]</PRE
 
451
><P
 
452
>    The <CODE
 
453
CLASS="FUNCTION"
 
454
>SIMILAR TO</CODE
 
455
> operator returns true or
 
456
    false depending on whether its pattern matches the given string.
 
457
    It is similar to <CODE
 
458
CLASS="FUNCTION"
 
459
>LIKE</CODE
 
460
>, except that it
 
461
    interprets the pattern using the SQL standard's definition of a
 
462
    regular expression.  SQL regular expressions are a curious cross
 
463
    between <CODE
 
464
CLASS="FUNCTION"
 
465
>LIKE</CODE
 
466
> notation and common regular
 
467
    expression notation.
 
468
   </P
 
469
><P
 
470
>    Like <CODE
 
471
CLASS="FUNCTION"
 
472
>LIKE</CODE
 
473
>, the <CODE
 
474
CLASS="FUNCTION"
 
475
>SIMILAR TO</CODE
 
476
>
 
477
    operator succeeds only if its pattern matches the entire string;
 
478
    this is unlike common regular expression behavior where the pattern
 
479
    can match any part of the string.
 
480
    Also like
 
481
    <CODE
 
482
CLASS="FUNCTION"
 
483
>LIKE</CODE
 
484
>, <CODE
 
485
CLASS="FUNCTION"
 
486
>SIMILAR TO</CODE
 
487
> uses
 
488
    <TT
 
489
CLASS="LITERAL"
 
490
>_</TT
 
491
> and <TT
 
492
CLASS="LITERAL"
 
493
>%</TT
 
494
> as wildcard characters denoting
 
495
    any single character and any string, respectively (these are
 
496
    comparable to <TT
 
497
CLASS="LITERAL"
 
498
>.</TT
 
499
> and <TT
 
500
CLASS="LITERAL"
 
501
>.*</TT
 
502
> in POSIX regular
 
503
    expressions).
 
504
   </P
 
505
><P
 
506
>    In addition to these facilities borrowed from <CODE
 
507
CLASS="FUNCTION"
 
508
>LIKE</CODE
 
509
>,
 
510
    <CODE
 
511
CLASS="FUNCTION"
 
512
>SIMILAR TO</CODE
 
513
> supports these pattern-matching
 
514
    metacharacters borrowed from POSIX regular expressions:
 
515
 
 
516
   <P
 
517
></P
 
518
></P><UL
 
519
><LI
 
520
><P
 
521
>      <TT
 
522
CLASS="LITERAL"
 
523
>|</TT
 
524
> denotes alternation (either of two alternatives).
 
525
     </P
 
526
></LI
 
527
><LI
 
528
><P
 
529
>      <TT
 
530
CLASS="LITERAL"
 
531
>*</TT
 
532
> denotes repetition of the previous item zero
 
533
      or more times.
 
534
     </P
 
535
></LI
 
536
><LI
 
537
><P
 
538
>      <TT
 
539
CLASS="LITERAL"
 
540
>+</TT
 
541
> denotes repetition of the previous item one
 
542
      or more times.
 
543
     </P
 
544
></LI
 
545
><LI
 
546
><P
 
547
>      <TT
 
548
CLASS="LITERAL"
 
549
>?</TT
 
550
> denotes repetition of the previous item zero
 
551
      or one time.
 
552
     </P
 
553
></LI
 
554
><LI
 
555
><P
 
556
>      <TT
 
557
CLASS="LITERAL"
 
558
>{</TT
 
559
><TT
 
560
CLASS="REPLACEABLE"
 
561
><I
 
562
>m</I
 
563
></TT
 
564
><TT
 
565
CLASS="LITERAL"
 
566
>}</TT
 
567
> denotes repetition
 
568
      of the previous item exactly <TT
 
569
CLASS="REPLACEABLE"
 
570
><I
 
571
>m</I
 
572
></TT
 
573
> times.
 
574
     </P
 
575
></LI
 
576
><LI
 
577
><P
 
578
>      <TT
 
579
CLASS="LITERAL"
 
580
>{</TT
 
581
><TT
 
582
CLASS="REPLACEABLE"
 
583
><I
 
584
>m</I
 
585
></TT
 
586
><TT
 
587
CLASS="LITERAL"
 
588
>,}</TT
 
589
> denotes repetition
 
590
      of the previous item <TT
 
591
CLASS="REPLACEABLE"
 
592
><I
 
593
>m</I
 
594
></TT
 
595
> or more times.
 
596
     </P
 
597
></LI
 
598
><LI
 
599
><P
 
600
>      <TT
 
601
CLASS="LITERAL"
 
602
>{</TT
 
603
><TT
 
604
CLASS="REPLACEABLE"
 
605
><I
 
606
>m</I
 
607
></TT
 
608
><TT
 
609
CLASS="LITERAL"
 
610
>,</TT
 
611
><TT
 
612
CLASS="REPLACEABLE"
 
613
><I
 
614
>n</I
 
615
></TT
 
616
><TT
 
617
CLASS="LITERAL"
 
618
>}</TT
 
619
>
 
620
      denotes repetition of the previous item at least <TT
 
621
CLASS="REPLACEABLE"
 
622
><I
 
623
>m</I
 
624
></TT
 
625
> and
 
626
      not more than <TT
 
627
CLASS="REPLACEABLE"
 
628
><I
 
629
>n</I
 
630
></TT
 
631
> times.
 
632
     </P
 
633
></LI
 
634
><LI
 
635
><P
 
636
>      Parentheses <TT
 
637
CLASS="LITERAL"
 
638
>()</TT
 
639
> can be used to group items into
 
640
      a single logical item.
 
641
     </P
 
642
></LI
 
643
><LI
 
644
><P
 
645
>      A bracket expression <TT
 
646
CLASS="LITERAL"
 
647
>[...]</TT
 
648
> specifies a character
 
649
      class, just as in POSIX regular expressions.
 
650
     </P
 
651
></LI
 
652
></UL
 
653
><P>
 
654
 
 
655
    Notice that the period (<TT
 
656
CLASS="LITERAL"
 
657
>.</TT
 
658
>) is not a metacharacter
 
659
    for <CODE
 
660
CLASS="FUNCTION"
 
661
>SIMILAR TO</CODE
 
662
>.
 
663
   </P
 
664
><P
 
665
>    As with <CODE
 
666
CLASS="FUNCTION"
 
667
>LIKE</CODE
 
668
>, a backslash disables the special meaning
 
669
    of any of these metacharacters; or a different escape character can
 
670
    be specified with <TT
 
671
CLASS="LITERAL"
 
672
>ESCAPE</TT
 
673
>.
 
674
   </P
 
675
><P
 
676
>    Some examples:
 
677
</P><PRE
 
678
CLASS="PROGRAMLISTING"
 
679
>'abc' SIMILAR TO 'abc'      <I
 
680
CLASS="LINEANNOTATION"
 
681
>true</I
 
682
>
 
683
'abc' SIMILAR TO 'a'        <I
 
684
CLASS="LINEANNOTATION"
 
685
>false</I
 
686
>
 
687
'abc' SIMILAR TO '%(b|d)%'  <I
 
688
CLASS="LINEANNOTATION"
 
689
>true</I
 
690
>
 
691
'abc' SIMILAR TO '(b|c)%'   <I
 
692
CLASS="LINEANNOTATION"
 
693
>false</I
 
694
></PRE
 
695
><P>
 
696
   </P
 
697
><P
 
698
>    The <CODE
 
699
CLASS="FUNCTION"
 
700
>substring</CODE
 
701
> function with three parameters,
 
702
    <CODE
 
703
CLASS="FUNCTION"
 
704
>substring(<TT
 
705
CLASS="REPLACEABLE"
 
706
><I
 
707
>string</I
 
708
></TT
 
709
> from
 
710
    <TT
 
711
CLASS="REPLACEABLE"
 
712
><I
 
713
>pattern</I
 
714
></TT
 
715
> for
 
716
    <TT
 
717
CLASS="REPLACEABLE"
 
718
><I
 
719
>escape-character</I
 
720
></TT
 
721
>)</CODE
 
722
>, provides
 
723
    extraction of a substring that matches an SQL
 
724
    regular expression pattern.  As with <TT
 
725
CLASS="LITERAL"
 
726
>SIMILAR TO</TT
 
727
>, the
 
728
    specified pattern must match the entire data string, or else the
 
729
    function fails and returns null.  To indicate the part of the
 
730
    pattern that should be returned on success, the pattern must contain
 
731
    two occurrences of the escape character followed by a double quote
 
732
    (<TT
 
733
CLASS="LITERAL"
 
734
>"</TT
 
735
>). 
 
736
    The text matching the portion of the pattern
 
737
    between these markers is returned.
 
738
   </P
 
739
><P
 
740
>    Some examples, with <TT
 
741
CLASS="LITERAL"
 
742
>#&quot;</TT
 
743
> delimiting the return string:
 
744
</P><PRE
 
745
CLASS="PROGRAMLISTING"
 
746
>substring('foobar' from '%#"o_b#"%' for '#')   <I
 
747
CLASS="LINEANNOTATION"
 
748
>oob</I
 
749
>
 
750
substring('foobar' from '#"o_b#"%' for '#')    <I
 
751
CLASS="LINEANNOTATION"
 
752
>NULL</I
 
753
></PRE
 
754
><P>
 
755
   </P
 
756
></DIV
 
757
><DIV
 
758
CLASS="SECT2"
 
759
><H2
 
760
CLASS="SECT2"
 
761
><A
 
762
NAME="FUNCTIONS-POSIX-REGEXP"
 
763
>9.7.3. <ACRONYM
 
764
CLASS="ACRONYM"
 
765
>POSIX</ACRONYM
 
766
> Regular Expressions</A
 
767
></H2
 
768
><P
 
769
>    <A
 
770
HREF="functions-matching.html#FUNCTIONS-POSIX-TABLE"
 
771
>Table 9-11</A
 
772
> lists the available
 
773
    operators for pattern matching using POSIX regular expressions.
 
774
   </P
 
775
><DIV
 
776
CLASS="TABLE"
 
777
><A
 
778
NAME="FUNCTIONS-POSIX-TABLE"
 
779
></A
 
780
><P
 
781
><B
 
782
>Table 9-11. Regular Expression Match Operators</B
 
783
></P
 
784
><TABLE
 
785
BORDER="1"
 
786
CLASS="CALSTABLE"
 
787
><COL><COL><COL><THEAD
 
788
><TR
 
789
><TH
 
790
>Operator</TH
 
791
><TH
 
792
>Description</TH
 
793
><TH
 
794
>Example</TH
 
795
></TR
 
796
></THEAD
 
797
><TBODY
 
798
><TR
 
799
><TD
 
800
> <TT
 
801
CLASS="LITERAL"
 
802
>~</TT
 
803
> </TD
 
804
><TD
 
805
>Matches regular expression, case sensitive</TD
 
806
><TD
 
807
><TT
 
808
CLASS="LITERAL"
 
809
>'thomas' ~ '.*thomas.*'</TT
 
810
></TD
 
811
></TR
 
812
><TR
 
813
><TD
 
814
> <TT
 
815
CLASS="LITERAL"
 
816
>~*</TT
 
817
> </TD
 
818
><TD
 
819
>Matches regular expression, case insensitive</TD
 
820
><TD
 
821
><TT
 
822
CLASS="LITERAL"
 
823
>'thomas' ~* '.*Thomas.*'</TT
 
824
></TD
 
825
></TR
 
826
><TR
 
827
><TD
 
828
> <TT
 
829
CLASS="LITERAL"
 
830
>!~</TT
 
831
> </TD
 
832
><TD
 
833
>Does not match regular expression, case sensitive</TD
 
834
><TD
 
835
><TT
 
836
CLASS="LITERAL"
 
837
>'thomas' !~ '.*Thomas.*'</TT
 
838
></TD
 
839
></TR
 
840
><TR
 
841
><TD
 
842
> <TT
 
843
CLASS="LITERAL"
 
844
>!~*</TT
 
845
> </TD
 
846
><TD
 
847
>Does not match regular expression, case insensitive</TD
 
848
><TD
 
849
><TT
 
850
CLASS="LITERAL"
 
851
>'thomas' !~* '.*vadim.*'</TT
 
852
></TD
 
853
></TR
 
854
></TBODY
 
855
></TABLE
 
856
></DIV
 
857
><P
 
858
>     <ACRONYM
 
859
CLASS="ACRONYM"
 
860
>POSIX</ACRONYM
 
861
> regular expressions provide a more
 
862
     powerful means for pattern matching than the <CODE
 
863
CLASS="FUNCTION"
 
864
>LIKE</CODE
 
865
> and
 
866
     <CODE
 
867
CLASS="FUNCTION"
 
868
>SIMILAR TO</CODE
 
869
> operators.
 
870
     Many Unix tools such as <TT
 
871
CLASS="COMMAND"
 
872
>egrep</TT
 
873
>,
 
874
     <TT
 
875
CLASS="COMMAND"
 
876
>sed</TT
 
877
>, or <TT
 
878
CLASS="COMMAND"
 
879
>awk</TT
 
880
> use a pattern
 
881
     matching language that is similar to the one described here.
 
882
    </P
 
883
><P
 
884
>     A regular expression is a character sequence that is an
 
885
     abbreviated definition of a set of strings (a <I
 
886
CLASS="FIRSTTERM"
 
887
>regular
 
888
     set</I
 
889
>).  A string is said to match a regular expression
 
890
     if it is a member of the regular set described by the regular
 
891
     expression.  As with <CODE
 
892
CLASS="FUNCTION"
 
893
>LIKE</CODE
 
894
>, pattern characters
 
895
     match string characters exactly unless they are special characters
 
896
     in the regular expression language &mdash; but regular expressions use
 
897
     different special characters than <CODE
 
898
CLASS="FUNCTION"
 
899
>LIKE</CODE
 
900
> does.
 
901
     Unlike <CODE
 
902
CLASS="FUNCTION"
 
903
>LIKE</CODE
 
904
> patterns, a
 
905
     regular expression is allowed to match anywhere within a string, unless
 
906
     the regular expression is explicitly anchored to the beginning or
 
907
     end of the string.
 
908
    </P
 
909
><P
 
910
>     Some examples:
 
911
</P><PRE
 
912
CLASS="PROGRAMLISTING"
 
913
>'abc' ~ 'abc'    <I
 
914
CLASS="LINEANNOTATION"
 
915
>true</I
 
916
>
 
917
'abc' ~ '^a'     <I
 
918
CLASS="LINEANNOTATION"
 
919
>true</I
 
920
>
 
921
'abc' ~ '(b|d)'  <I
 
922
CLASS="LINEANNOTATION"
 
923
>true</I
 
924
>
 
925
'abc' ~ '^(b|c)' <I
 
926
CLASS="LINEANNOTATION"
 
927
>false</I
 
928
></PRE
 
929
><P>
 
930
    </P
 
931
><P
 
932
>     The <ACRONYM
 
933
CLASS="ACRONYM"
 
934
>POSIX</ACRONYM
 
935
> pattern language is described in much
 
936
     greater detail below.
 
937
    </P
 
938
><P
 
939
>     The <CODE
 
940
CLASS="FUNCTION"
 
941
>substring</CODE
 
942
> function with two parameters,
 
943
     <CODE
 
944
CLASS="FUNCTION"
 
945
>substring(<TT
 
946
CLASS="REPLACEABLE"
 
947
><I
 
948
>string</I
 
949
></TT
 
950
> from
 
951
     <TT
 
952
CLASS="REPLACEABLE"
 
953
><I
 
954
>pattern</I
 
955
></TT
 
956
>)</CODE
 
957
>, provides extraction of a
 
958
     substring
 
959
     that matches a POSIX regular expression pattern.  It returns null if
 
960
     there is no match, otherwise the portion of the text that matched the
 
961
     pattern.  But if the pattern contains any parentheses, the portion
 
962
     of the text that matched the first parenthesized subexpression (the
 
963
     one whose left parenthesis comes first) is
 
964
     returned.  You can put parentheses around the whole expression
 
965
     if you want to use parentheses within it without triggering this
 
966
     exception.  If you need parentheses in the pattern before the
 
967
     subexpression you want to extract, see the non-capturing parentheses
 
968
     described below.
 
969
    </P
 
970
><P
 
971
>    Some examples:
 
972
</P><PRE
 
973
CLASS="PROGRAMLISTING"
 
974
>substring('foobar' from 'o.b')     <I
 
975
CLASS="LINEANNOTATION"
 
976
>oob</I
 
977
>
 
978
substring('foobar' from 'o(.)b')   <I
 
979
CLASS="LINEANNOTATION"
 
980
>o</I
 
981
></PRE
 
982
><P>
 
983
   </P
 
984
><P
 
985
>     The <CODE
 
986
CLASS="FUNCTION"
 
987
>regexp_replace</CODE
 
988
> function provides substitution of
 
989
     new text for substrings that match POSIX regular expression patterns.
 
990
     It has the syntax
 
991
     <CODE
 
992
CLASS="FUNCTION"
 
993
>regexp_replace</CODE
 
994
>(<TT
 
995
CLASS="REPLACEABLE"
 
996
><I
 
997
>source</I
 
998
></TT
 
999
>,
 
1000
     <TT
 
1001
CLASS="REPLACEABLE"
 
1002
><I
 
1003
>pattern</I
 
1004
></TT
 
1005
>, <TT
 
1006
CLASS="REPLACEABLE"
 
1007
><I
 
1008
>replacement</I
 
1009
></TT
 
1010
>
 
1011
     [<SPAN
 
1012
CLASS="OPTIONAL"
 
1013
>, <TT
 
1014
CLASS="REPLACEABLE"
 
1015
><I
 
1016
>flags</I
 
1017
></TT
 
1018
> </SPAN
 
1019
>]).
 
1020
     The <TT
 
1021
CLASS="REPLACEABLE"
 
1022
><I
 
1023
>source</I
 
1024
></TT
 
1025
> string is returned unchanged if
 
1026
     there is no match to the <TT
 
1027
CLASS="REPLACEABLE"
 
1028
><I
 
1029
>pattern</I
 
1030
></TT
 
1031
>.  If there is a
 
1032
     match, the <TT
 
1033
CLASS="REPLACEABLE"
 
1034
><I
 
1035
>source</I
 
1036
></TT
 
1037
> string is returned with the
 
1038
     <TT
 
1039
CLASS="REPLACEABLE"
 
1040
><I
 
1041
>replacement</I
 
1042
></TT
 
1043
> string substituted for the matching
 
1044
     substring.  The <TT
 
1045
CLASS="REPLACEABLE"
 
1046
><I
 
1047
>replacement</I
 
1048
></TT
 
1049
> string can contain
 
1050
     <TT
 
1051
CLASS="LITERAL"
 
1052
>\</TT
 
1053
><TT
 
1054
CLASS="REPLACEABLE"
 
1055
><I
 
1056
>n</I
 
1057
></TT
 
1058
>, where <TT
 
1059
CLASS="REPLACEABLE"
 
1060
><I
 
1061
>n</I
 
1062
></TT
 
1063
> is 1
 
1064
     through 9, to indicate that the source substring matching the
 
1065
     <TT
 
1066
CLASS="REPLACEABLE"
 
1067
><I
 
1068
>n</I
 
1069
></TT
 
1070
>'th parenthesized subexpression of the pattern should be
 
1071
     inserted, and it can contain <TT
 
1072
CLASS="LITERAL"
 
1073
>\&amp;</TT
 
1074
> to indicate that the
 
1075
     substring matching the entire pattern should be inserted.  Write
 
1076
     <TT
 
1077
CLASS="LITERAL"
 
1078
>\\</TT
 
1079
> if you need to put a literal backslash in the replacement
 
1080
     text.  (As always, remember to double backslashes written in literal
 
1081
     constant strings, assuming escape string syntax is used.)
 
1082
     The <TT
 
1083
CLASS="REPLACEABLE"
 
1084
><I
 
1085
>flags</I
 
1086
></TT
 
1087
> parameter is an optional text
 
1088
     string containing zero or more single-letter flags that change the
 
1089
     function's behavior.  Flag <TT
 
1090
CLASS="LITERAL"
 
1091
>i</TT
 
1092
> specifies case-insensitive
 
1093
     matching, while flag <TT
 
1094
CLASS="LITERAL"
 
1095
>g</TT
 
1096
> specifies replacement of each matching
 
1097
     substring rather than only the first one.  Other supported flags are
 
1098
     described in <A
 
1099
HREF="functions-matching.html#POSIX-EMBEDDED-OPTIONS-TABLE"
 
1100
>Table 9-19</A
 
1101
>.
 
1102
    </P
 
1103
><P
 
1104
>    Some examples:
 
1105
</P><PRE
 
1106
CLASS="PROGRAMLISTING"
 
1107
>regexp_replace('foobarbaz', 'b..', 'X')
 
1108
                                   <I
 
1109
CLASS="LINEANNOTATION"
 
1110
>fooXbaz</I
 
1111
>
 
1112
regexp_replace('foobarbaz', 'b..', 'X', 'g')
 
1113
                                   <I
 
1114
CLASS="LINEANNOTATION"
 
1115
>fooXX</I
 
1116
>
 
1117
regexp_replace('foobarbaz', 'b(..)', E'X\\1Y', 'g')
 
1118
                                   <I
 
1119
CLASS="LINEANNOTATION"
 
1120
>fooXarYXazY</I
 
1121
></PRE
 
1122
><P>
 
1123
   </P
 
1124
><P
 
1125
>     The <CODE
 
1126
CLASS="FUNCTION"
 
1127
>regexp_matches</CODE
 
1128
> function returns a text array of
 
1129
     all of the captured substrings resulting from matching a POSIX
 
1130
     regular expression pattern.  It has the syntax
 
1131
     <CODE
 
1132
CLASS="FUNCTION"
 
1133
>regexp_matches</CODE
 
1134
>(<TT
 
1135
CLASS="REPLACEABLE"
 
1136
><I
 
1137
>string</I
 
1138
></TT
 
1139
>, <TT
 
1140
CLASS="REPLACEABLE"
 
1141
><I
 
1142
>pattern</I
 
1143
></TT
 
1144
>
 
1145
     [<SPAN
 
1146
CLASS="OPTIONAL"
 
1147
>, <TT
 
1148
CLASS="REPLACEABLE"
 
1149
><I
 
1150
>flags</I
 
1151
></TT
 
1152
> </SPAN
 
1153
>]).
 
1154
     The function can return no rows, one row, or multiple rows (see
 
1155
     the <TT
 
1156
CLASS="LITERAL"
 
1157
>g</TT
 
1158
> flag below).  If the <TT
 
1159
CLASS="REPLACEABLE"
 
1160
><I
 
1161
>pattern</I
 
1162
></TT
 
1163
>
 
1164
     does not match, the function returns no rows.  If the pattern
 
1165
     contains no parenthesized subexpressions, then each row
 
1166
     returned is a single-element text array containing the substring
 
1167
     matching the whole pattern.  If the pattern contains parenthesized
 
1168
     subexpressions, the function returns a text array whose
 
1169
     <TT
 
1170
CLASS="REPLACEABLE"
 
1171
><I
 
1172
>n</I
 
1173
></TT
 
1174
>'th element is the substring matching the
 
1175
     <TT
 
1176
CLASS="REPLACEABLE"
 
1177
><I
 
1178
>n</I
 
1179
></TT
 
1180
>'th parenthesized subexpression of the pattern
 
1181
     (not counting <SPAN
 
1182
CLASS="QUOTE"
 
1183
>"non-capturing"</SPAN
 
1184
> parentheses; see below for
 
1185
     details).
 
1186
     The <TT
 
1187
CLASS="REPLACEABLE"
 
1188
><I
 
1189
>flags</I
 
1190
></TT
 
1191
> parameter is an optional text
 
1192
     string containing zero or more single-letter flags that change the
 
1193
     function's behavior.  Flag <TT
 
1194
CLASS="LITERAL"
 
1195
>g</TT
 
1196
> causes the function to find
 
1197
     each match in the string, not only the first one, and return a row for
 
1198
     each such match.  Other supported
 
1199
     flags are described in <A
 
1200
HREF="functions-matching.html#POSIX-EMBEDDED-OPTIONS-TABLE"
 
1201
>Table 9-19</A
 
1202
>.
 
1203
    </P
 
1204
><P
 
1205
>    Some examples:
 
1206
</P><PRE
 
1207
CLASS="PROGRAMLISTING"
 
1208
>SELECT regexp_matches('foobarbequebaz', '(bar)(beque)');
 
1209
 regexp_matches 
 
1210
----------------
 
1211
 {bar,beque}
 
1212
(1 row)
 
1213
 
 
1214
SELECT regexp_matches('foobarbequebazilbarfbonk', '(b[^b]+)(b[^b]+)', 'g');
 
1215
 regexp_matches 
 
1216
----------------
 
1217
 {bar,beque}
 
1218
 {bazil,barf}
 
1219
(2 rows)
 
1220
 
 
1221
SELECT regexp_matches('foobarbequebaz', 'barbeque');
 
1222
 regexp_matches 
 
1223
----------------
 
1224
 {barbeque}
 
1225
(1 row)</PRE
 
1226
><P>
 
1227
   </P
 
1228
><P
 
1229
>    It is possible to force <CODE
 
1230
CLASS="FUNCTION"
 
1231
>regexp_matches()</CODE
 
1232
> to always
 
1233
    return one row by using a sub-select;  this is particularly useful
 
1234
    in a <TT
 
1235
CLASS="LITERAL"
 
1236
>SELECT</TT
 
1237
> target list when you want all rows
 
1238
    returned, even non-matching ones:
 
1239
</P><PRE
 
1240
CLASS="PROGRAMLISTING"
 
1241
>SELECT col1, (SELECT regexp_matches(col2, '(bar)(beque)')) FROM tab;</PRE
 
1242
><P>
 
1243
   </P
 
1244
><P
 
1245
>     The <CODE
 
1246
CLASS="FUNCTION"
 
1247
>regexp_split_to_table</CODE
 
1248
> function splits a string using a POSIX
 
1249
     regular expression pattern as a delimiter.  It has the syntax
 
1250
     <CODE
 
1251
CLASS="FUNCTION"
 
1252
>regexp_split_to_table</CODE
 
1253
>(<TT
 
1254
CLASS="REPLACEABLE"
 
1255
><I
 
1256
>string</I
 
1257
></TT
 
1258
>, <TT
 
1259
CLASS="REPLACEABLE"
 
1260
><I
 
1261
>pattern</I
 
1262
></TT
 
1263
>
 
1264
     [<SPAN
 
1265
CLASS="OPTIONAL"
 
1266
>, <TT
 
1267
CLASS="REPLACEABLE"
 
1268
><I
 
1269
>flags</I
 
1270
></TT
 
1271
> </SPAN
 
1272
>]).
 
1273
     If there is no match to the <TT
 
1274
CLASS="REPLACEABLE"
 
1275
><I
 
1276
>pattern</I
 
1277
></TT
 
1278
>, the function returns the
 
1279
     <TT
 
1280
CLASS="REPLACEABLE"
 
1281
><I
 
1282
>string</I
 
1283
></TT
 
1284
>.  If there is at least one match, for each match it returns
 
1285
     the text from the end of the last match (or the beginning of the string)
 
1286
     to the beginning of the match.  When there are no more matches, it
 
1287
     returns the text from the end of the last match to the end of the string.
 
1288
     The <TT
 
1289
CLASS="REPLACEABLE"
 
1290
><I
 
1291
>flags</I
 
1292
></TT
 
1293
> parameter is an optional text string containing
 
1294
     zero or more single-letter flags that change the function's behavior.
 
1295
     <CODE
 
1296
CLASS="FUNCTION"
 
1297
>regexp_split_to_table</CODE
 
1298
> supports the flags described in
 
1299
     <A
 
1300
HREF="functions-matching.html#POSIX-EMBEDDED-OPTIONS-TABLE"
 
1301
>Table 9-19</A
 
1302
>.
 
1303
    </P
 
1304
><P
 
1305
>     The <CODE
 
1306
CLASS="FUNCTION"
 
1307
>regexp_split_to_array</CODE
 
1308
> function behaves the same as
 
1309
     <CODE
 
1310
CLASS="FUNCTION"
 
1311
>regexp_split_to_table</CODE
 
1312
>, except that <CODE
 
1313
CLASS="FUNCTION"
 
1314
>regexp_split_to_array</CODE
 
1315
>
 
1316
     returns its result as an array of <TT
 
1317
CLASS="TYPE"
 
1318
>text</TT
 
1319
>.  It has the syntax
 
1320
     <CODE
 
1321
CLASS="FUNCTION"
 
1322
>regexp_split_to_array</CODE
 
1323
>(<TT
 
1324
CLASS="REPLACEABLE"
 
1325
><I
 
1326
>string</I
 
1327
></TT
 
1328
>, <TT
 
1329
CLASS="REPLACEABLE"
 
1330
><I
 
1331
>pattern</I
 
1332
></TT
 
1333
>
 
1334
     [<SPAN
 
1335
CLASS="OPTIONAL"
 
1336
>, <TT
 
1337
CLASS="REPLACEABLE"
 
1338
><I
 
1339
>flags</I
 
1340
></TT
 
1341
> </SPAN
 
1342
>]).
 
1343
     The parameters are the same as for <CODE
 
1344
CLASS="FUNCTION"
 
1345
>regexp_split_to_table</CODE
 
1346
>.
 
1347
    </P
 
1348
><P
 
1349
>    Some examples:
 
1350
</P><PRE
 
1351
CLASS="PROGRAMLISTING"
 
1352
>&#13;SELECT foo FROM regexp_split_to_table('the quick brown fox jumped over the lazy dog', E'\\s+') AS foo;
 
1353
  foo   
 
1354
--------
 
1355
 the    
 
1356
 quick  
 
1357
 brown  
 
1358
 fox    
 
1359
 jumped 
 
1360
 over   
 
1361
 the    
 
1362
 lazy   
 
1363
 dog    
 
1364
(9 rows)
 
1365
 
 
1366
SELECT regexp_split_to_array('the quick brown fox jumped over the lazy dog', E'\\s+');
 
1367
              regexp_split_to_array             
 
1368
------------------------------------------------
 
1369
 {the,quick,brown,fox,jumped,over,the,lazy,dog}
 
1370
(1 row)
 
1371
 
 
1372
SELECT foo FROM regexp_split_to_table('the quick brown fox', E'\\s*') AS foo;
 
1373
 foo 
 
1374
-----
 
1375
 t         
 
1376
 h         
 
1377
 e         
 
1378
 q         
 
1379
 u         
 
1380
 i         
 
1381
 c         
 
1382
 k         
 
1383
 b         
 
1384
 r         
 
1385
 o         
 
1386
 w         
 
1387
 n         
 
1388
 f         
 
1389
 o         
 
1390
 x         
 
1391
(16 rows)</PRE
 
1392
><P>
 
1393
   </P
 
1394
><P
 
1395
>    As the last example demonstrates, the regexp split functions ignore
 
1396
    zero-length matches that occur at the start or end of the string
 
1397
    or immediately after a previous match.  This is contrary to the strict
 
1398
    definition of regexp matching that is implemented by
 
1399
    <CODE
 
1400
CLASS="FUNCTION"
 
1401
>regexp_matches</CODE
 
1402
>, but is usually the most convenient behavior
 
1403
    in practice.  Other software systems such as Perl use similar definitions.
 
1404
   </P
 
1405
><DIV
 
1406
CLASS="SECT3"
 
1407
><H3
 
1408
CLASS="SECT3"
 
1409
><A
 
1410
NAME="POSIX-SYNTAX-DETAILS"
 
1411
>9.7.3.1. Regular Expression Details</A
 
1412
></H3
 
1413
><P
 
1414
>    <SPAN
 
1415
CLASS="PRODUCTNAME"
 
1416
>PostgreSQL</SPAN
 
1417
>'s regular expressions are implemented
 
1418
    using a software package written by Henry Spencer.  Much of
 
1419
    the description of regular expressions below is copied verbatim from his
 
1420
    manual.
 
1421
   </P
 
1422
><P
 
1423
>    Regular expressions (<ACRONYM
 
1424
CLASS="ACRONYM"
 
1425
>RE</ACRONYM
 
1426
>s), as defined in
 
1427
    <ACRONYM
 
1428
CLASS="ACRONYM"
 
1429
>POSIX</ACRONYM
 
1430
> 1003.2, come in two forms:
 
1431
    <I
 
1432
CLASS="FIRSTTERM"
 
1433
>extended</I
 
1434
> <ACRONYM
 
1435
CLASS="ACRONYM"
 
1436
>RE</ACRONYM
 
1437
>s or <ACRONYM
 
1438
CLASS="ACRONYM"
 
1439
>ERE</ACRONYM
 
1440
>s
 
1441
    (roughly those of <TT
 
1442
CLASS="COMMAND"
 
1443
>egrep</TT
 
1444
>), and
 
1445
    <I
 
1446
CLASS="FIRSTTERM"
 
1447
>basic</I
 
1448
> <ACRONYM
 
1449
CLASS="ACRONYM"
 
1450
>RE</ACRONYM
 
1451
>s or <ACRONYM
 
1452
CLASS="ACRONYM"
 
1453
>BRE</ACRONYM
 
1454
>s
 
1455
    (roughly those of <TT
 
1456
CLASS="COMMAND"
 
1457
>ed</TT
 
1458
>).
 
1459
    <SPAN
 
1460
CLASS="PRODUCTNAME"
 
1461
>PostgreSQL</SPAN
 
1462
> supports both forms, and
 
1463
    also implements some extensions
 
1464
    that are not in the POSIX standard, but have become widely used
 
1465
    due to their availability in programming languages such as Perl and Tcl.
 
1466
    <ACRONYM
 
1467
CLASS="ACRONYM"
 
1468
>RE</ACRONYM
 
1469
>s using these non-POSIX extensions are called
 
1470
    <I
 
1471
CLASS="FIRSTTERM"
 
1472
>advanced</I
 
1473
> <ACRONYM
 
1474
CLASS="ACRONYM"
 
1475
>RE</ACRONYM
 
1476
>s or <ACRONYM
 
1477
CLASS="ACRONYM"
 
1478
>ARE</ACRONYM
 
1479
>s
 
1480
    in this documentation.  AREs are almost an exact superset of EREs,
 
1481
    but BREs have several notational incompatibilities (as well as being
 
1482
    much more limited).
 
1483
    We first describe the ARE and ERE forms, noting features that apply
 
1484
    only to AREs, and then describe how BREs differ.
 
1485
   </P
 
1486
><DIV
 
1487
CLASS="NOTE"
 
1488
><BLOCKQUOTE
 
1489
CLASS="NOTE"
 
1490
><P
 
1491
><B
 
1492
>Note: </B
 
1493
>     <SPAN
 
1494
CLASS="PRODUCTNAME"
 
1495
>PostgreSQL</SPAN
 
1496
> always initially presumes that a regular
 
1497
     expression follows the ARE rules.  However, the more limited ERE or
 
1498
     BRE rules can be chosen by prepending an <I
 
1499
CLASS="FIRSTTERM"
 
1500
>embedded option</I
 
1501
>
 
1502
     to the RE pattern, as described in <A
 
1503
HREF="functions-matching.html#POSIX-METASYNTAX"
 
1504
>Section 9.7.3.4</A
 
1505
>.
 
1506
     This can be useful for compatibility with applications that expect
 
1507
     exactly the <ACRONYM
 
1508
CLASS="ACRONYM"
 
1509
>POSIX</ACRONYM
 
1510
> 1003.2 rules.
 
1511
    </P
 
1512
></BLOCKQUOTE
 
1513
></DIV
 
1514
><P
 
1515
>    A regular expression is defined as one or more
 
1516
    <I
 
1517
CLASS="FIRSTTERM"
 
1518
>branches</I
 
1519
>, separated by
 
1520
    <TT
 
1521
CLASS="LITERAL"
 
1522
>|</TT
 
1523
>.  It matches anything that matches one of the
 
1524
    branches.
 
1525
   </P
 
1526
><P
 
1527
>    A branch is zero or more <I
 
1528
CLASS="FIRSTTERM"
 
1529
>quantified atoms</I
 
1530
> or
 
1531
    <I
 
1532
CLASS="FIRSTTERM"
 
1533
>constraints</I
 
1534
>, concatenated.
 
1535
    It matches a match for the first, followed by a match for the second, etc;
 
1536
    an empty branch matches the empty string.
 
1537
   </P
 
1538
><P
 
1539
>    A quantified atom is an <I
 
1540
CLASS="FIRSTTERM"
 
1541
>atom</I
 
1542
> possibly followed
 
1543
    by a single <I
 
1544
CLASS="FIRSTTERM"
 
1545
>quantifier</I
 
1546
>.
 
1547
    Without a quantifier, it matches a match for the atom.
 
1548
    With a quantifier, it can match some number of matches of the atom.
 
1549
    An <I
 
1550
CLASS="FIRSTTERM"
 
1551
>atom</I
 
1552
> can be any of the possibilities
 
1553
    shown in <A
 
1554
HREF="functions-matching.html#POSIX-ATOMS-TABLE"
 
1555
>Table 9-12</A
 
1556
>.
 
1557
    The possible quantifiers and their meanings are shown in
 
1558
    <A
 
1559
HREF="functions-matching.html#POSIX-QUANTIFIERS-TABLE"
 
1560
>Table 9-13</A
 
1561
>.
 
1562
   </P
 
1563
><P
 
1564
>    A <I
 
1565
CLASS="FIRSTTERM"
 
1566
>constraint</I
 
1567
> matches an empty string, but matches only when
 
1568
    specific conditions are met.  A constraint can be used where an atom
 
1569
    could be used, except it cannot be followed by a quantifier.
 
1570
    The simple constraints are shown in
 
1571
    <A
 
1572
HREF="functions-matching.html#POSIX-CONSTRAINTS-TABLE"
 
1573
>Table 9-14</A
 
1574
>;
 
1575
    some more constraints are described later.
 
1576
   </P
 
1577
><DIV
 
1578
CLASS="TABLE"
 
1579
><A
 
1580
NAME="POSIX-ATOMS-TABLE"
 
1581
></A
 
1582
><P
 
1583
><B
 
1584
>Table 9-12. Regular Expression Atoms</B
 
1585
></P
 
1586
><TABLE
 
1587
BORDER="1"
 
1588
CLASS="CALSTABLE"
 
1589
><COL><COL><THEAD
 
1590
><TR
 
1591
><TH
 
1592
>Atom</TH
 
1593
><TH
 
1594
>Description</TH
 
1595
></TR
 
1596
></THEAD
 
1597
><TBODY
 
1598
><TR
 
1599
><TD
 
1600
> <TT
 
1601
CLASS="LITERAL"
 
1602
>(</TT
 
1603
><TT
 
1604
CLASS="REPLACEABLE"
 
1605
><I
 
1606
>re</I
 
1607
></TT
 
1608
><TT
 
1609
CLASS="LITERAL"
 
1610
>)</TT
 
1611
> </TD
 
1612
><TD
 
1613
> (where <TT
 
1614
CLASS="REPLACEABLE"
 
1615
><I
 
1616
>re</I
 
1617
></TT
 
1618
> is any regular expression)
 
1619
       matches a match for
 
1620
       <TT
 
1621
CLASS="REPLACEABLE"
 
1622
><I
 
1623
>re</I
 
1624
></TT
 
1625
>, with the match noted for possible reporting </TD
 
1626
></TR
 
1627
><TR
 
1628
><TD
 
1629
> <TT
 
1630
CLASS="LITERAL"
 
1631
>(?:</TT
 
1632
><TT
 
1633
CLASS="REPLACEABLE"
 
1634
><I
 
1635
>re</I
 
1636
></TT
 
1637
><TT
 
1638
CLASS="LITERAL"
 
1639
>)</TT
 
1640
> </TD
 
1641
><TD
 
1642
> as above, but the match is not noted for reporting
 
1643
       (a <SPAN
 
1644
CLASS="QUOTE"
 
1645
>"non-capturing"</SPAN
 
1646
> set of parentheses)
 
1647
       (AREs only) </TD
 
1648
></TR
 
1649
><TR
 
1650
><TD
 
1651
> <TT
 
1652
CLASS="LITERAL"
 
1653
>.</TT
 
1654
> </TD
 
1655
><TD
 
1656
> matches any single character </TD
 
1657
></TR
 
1658
><TR
 
1659
><TD
 
1660
> <TT
 
1661
CLASS="LITERAL"
 
1662
>[</TT
 
1663
><TT
 
1664
CLASS="REPLACEABLE"
 
1665
><I
 
1666
>chars</I
 
1667
></TT
 
1668
><TT
 
1669
CLASS="LITERAL"
 
1670
>]</TT
 
1671
> </TD
 
1672
><TD
 
1673
> a <I
 
1674
CLASS="FIRSTTERM"
 
1675
>bracket expression</I
 
1676
>,
 
1677
       matching any one of the <TT
 
1678
CLASS="REPLACEABLE"
 
1679
><I
 
1680
>chars</I
 
1681
></TT
 
1682
> (see
 
1683
       <A
 
1684
HREF="functions-matching.html#POSIX-BRACKET-EXPRESSIONS"
 
1685
>Section 9.7.3.2</A
 
1686
> for more detail) </TD
 
1687
></TR
 
1688
><TR
 
1689
><TD
 
1690
> <TT
 
1691
CLASS="LITERAL"
 
1692
>\</TT
 
1693
><TT
 
1694
CLASS="REPLACEABLE"
 
1695
><I
 
1696
>k</I
 
1697
></TT
 
1698
> </TD
 
1699
><TD
 
1700
> (where <TT
 
1701
CLASS="REPLACEABLE"
 
1702
><I
 
1703
>k</I
 
1704
></TT
 
1705
> is a non-alphanumeric character)
 
1706
       matches that character taken as an ordinary character,
 
1707
       e.g., <TT
 
1708
CLASS="LITERAL"
 
1709
>\\</TT
 
1710
> matches a backslash character </TD
 
1711
></TR
 
1712
><TR
 
1713
><TD
 
1714
> <TT
 
1715
CLASS="LITERAL"
 
1716
>\</TT
 
1717
><TT
 
1718
CLASS="REPLACEABLE"
 
1719
><I
 
1720
>c</I
 
1721
></TT
 
1722
> </TD
 
1723
><TD
 
1724
> where <TT
 
1725
CLASS="REPLACEABLE"
 
1726
><I
 
1727
>c</I
 
1728
></TT
 
1729
> is alphanumeric
 
1730
       (possibly followed by other characters)
 
1731
       is an <I
 
1732
CLASS="FIRSTTERM"
 
1733
>escape</I
 
1734
>, see <A
 
1735
HREF="functions-matching.html#POSIX-ESCAPE-SEQUENCES"
 
1736
>Section 9.7.3.3</A
 
1737
>
 
1738
       (AREs only; in EREs and BREs, this matches <TT
 
1739
CLASS="REPLACEABLE"
 
1740
><I
 
1741
>c</I
 
1742
></TT
 
1743
>) </TD
 
1744
></TR
 
1745
><TR
 
1746
><TD
 
1747
> <TT
 
1748
CLASS="LITERAL"
 
1749
>{</TT
 
1750
> </TD
 
1751
><TD
 
1752
> when followed by a character other than a digit,
 
1753
       matches the left-brace character <TT
 
1754
CLASS="LITERAL"
 
1755
>{</TT
 
1756
>;
 
1757
       when followed by a digit, it is the beginning of a
 
1758
       <TT
 
1759
CLASS="REPLACEABLE"
 
1760
><I
 
1761
>bound</I
 
1762
></TT
 
1763
> (see below) </TD
 
1764
></TR
 
1765
><TR
 
1766
><TD
 
1767
> <TT
 
1768
CLASS="REPLACEABLE"
 
1769
><I
 
1770
>x</I
 
1771
></TT
 
1772
> </TD
 
1773
><TD
 
1774
> where <TT
 
1775
CLASS="REPLACEABLE"
 
1776
><I
 
1777
>x</I
 
1778
></TT
 
1779
> is a single character with no other
 
1780
       significance, matches that character </TD
 
1781
></TR
 
1782
></TBODY
 
1783
></TABLE
 
1784
></DIV
 
1785
><P
 
1786
>    An RE cannot end with <TT
 
1787
CLASS="LITERAL"
 
1788
>\</TT
 
1789
>.
 
1790
   </P
 
1791
><DIV
 
1792
CLASS="NOTE"
 
1793
><BLOCKQUOTE
 
1794
CLASS="NOTE"
 
1795
><P
 
1796
><B
 
1797
>Note: </B
 
1798
>     Remember that the backslash (<TT
 
1799
CLASS="LITERAL"
 
1800
>\</TT
 
1801
>) already has a special
 
1802
     meaning in <SPAN
 
1803
CLASS="PRODUCTNAME"
 
1804
>PostgreSQL</SPAN
 
1805
> string literals.
 
1806
     To write a pattern constant that contains a backslash,
 
1807
     you must write two backslashes in the statement, assuming escape
 
1808
     string syntax is used (see <A
 
1809
HREF="sql-syntax-lexical.html#SQL-SYNTAX-STRINGS"
 
1810
>Section 4.1.2.1</A
 
1811
>).
 
1812
    </P
 
1813
></BLOCKQUOTE
 
1814
></DIV
 
1815
><DIV
 
1816
CLASS="TABLE"
 
1817
><A
 
1818
NAME="POSIX-QUANTIFIERS-TABLE"
 
1819
></A
 
1820
><P
 
1821
><B
 
1822
>Table 9-13. Regular Expression Quantifiers</B
 
1823
></P
 
1824
><TABLE
 
1825
BORDER="1"
 
1826
CLASS="CALSTABLE"
 
1827
><COL><COL><THEAD
 
1828
><TR
 
1829
><TH
 
1830
>Quantifier</TH
 
1831
><TH
 
1832
>Matches</TH
 
1833
></TR
 
1834
></THEAD
 
1835
><TBODY
 
1836
><TR
 
1837
><TD
 
1838
> <TT
 
1839
CLASS="LITERAL"
 
1840
>*</TT
 
1841
> </TD
 
1842
><TD
 
1843
> a sequence of 0 or more matches of the atom </TD
 
1844
></TR
 
1845
><TR
 
1846
><TD
 
1847
> <TT
 
1848
CLASS="LITERAL"
 
1849
>+</TT
 
1850
> </TD
 
1851
><TD
 
1852
> a sequence of 1 or more matches of the atom </TD
 
1853
></TR
 
1854
><TR
 
1855
><TD
 
1856
> <TT
 
1857
CLASS="LITERAL"
 
1858
>?</TT
 
1859
> </TD
 
1860
><TD
 
1861
> a sequence of 0 or 1 matches of the atom </TD
 
1862
></TR
 
1863
><TR
 
1864
><TD
 
1865
> <TT
 
1866
CLASS="LITERAL"
 
1867
>{</TT
 
1868
><TT
 
1869
CLASS="REPLACEABLE"
 
1870
><I
 
1871
>m</I
 
1872
></TT
 
1873
><TT
 
1874
CLASS="LITERAL"
 
1875
>}</TT
 
1876
> </TD
 
1877
><TD
 
1878
> a sequence of exactly <TT
 
1879
CLASS="REPLACEABLE"
 
1880
><I
 
1881
>m</I
 
1882
></TT
 
1883
> matches of the atom </TD
 
1884
></TR
 
1885
><TR
 
1886
><TD
 
1887
> <TT
 
1888
CLASS="LITERAL"
 
1889
>{</TT
 
1890
><TT
 
1891
CLASS="REPLACEABLE"
 
1892
><I
 
1893
>m</I
 
1894
></TT
 
1895
><TT
 
1896
CLASS="LITERAL"
 
1897
>,}</TT
 
1898
> </TD
 
1899
><TD
 
1900
> a sequence of <TT
 
1901
CLASS="REPLACEABLE"
 
1902
><I
 
1903
>m</I
 
1904
></TT
 
1905
> or more matches of the atom </TD
 
1906
></TR
 
1907
><TR
 
1908
><TD
 
1909
>       <TT
 
1910
CLASS="LITERAL"
 
1911
>{</TT
 
1912
><TT
 
1913
CLASS="REPLACEABLE"
 
1914
><I
 
1915
>m</I
 
1916
></TT
 
1917
><TT
 
1918
CLASS="LITERAL"
 
1919
>,</TT
 
1920
><TT
 
1921
CLASS="REPLACEABLE"
 
1922
><I
 
1923
>n</I
 
1924
></TT
 
1925
><TT
 
1926
CLASS="LITERAL"
 
1927
>}</TT
 
1928
> </TD
 
1929
><TD
 
1930
> a sequence of <TT
 
1931
CLASS="REPLACEABLE"
 
1932
><I
 
1933
>m</I
 
1934
></TT
 
1935
> through <TT
 
1936
CLASS="REPLACEABLE"
 
1937
><I
 
1938
>n</I
 
1939
></TT
 
1940
>
 
1941
       (inclusive) matches of the atom; <TT
 
1942
CLASS="REPLACEABLE"
 
1943
><I
 
1944
>m</I
 
1945
></TT
 
1946
> cannot exceed
 
1947
       <TT
 
1948
CLASS="REPLACEABLE"
 
1949
><I
 
1950
>n</I
 
1951
></TT
 
1952
> </TD
 
1953
></TR
 
1954
><TR
 
1955
><TD
 
1956
> <TT
 
1957
CLASS="LITERAL"
 
1958
>*?</TT
 
1959
> </TD
 
1960
><TD
 
1961
> non-greedy version of <TT
 
1962
CLASS="LITERAL"
 
1963
>*</TT
 
1964
> </TD
 
1965
></TR
 
1966
><TR
 
1967
><TD
 
1968
> <TT
 
1969
CLASS="LITERAL"
 
1970
>+?</TT
 
1971
> </TD
 
1972
><TD
 
1973
> non-greedy version of <TT
 
1974
CLASS="LITERAL"
 
1975
>+</TT
 
1976
> </TD
 
1977
></TR
 
1978
><TR
 
1979
><TD
 
1980
> <TT
 
1981
CLASS="LITERAL"
 
1982
>??</TT
 
1983
> </TD
 
1984
><TD
 
1985
> non-greedy version of <TT
 
1986
CLASS="LITERAL"
 
1987
>?</TT
 
1988
> </TD
 
1989
></TR
 
1990
><TR
 
1991
><TD
 
1992
> <TT
 
1993
CLASS="LITERAL"
 
1994
>{</TT
 
1995
><TT
 
1996
CLASS="REPLACEABLE"
 
1997
><I
 
1998
>m</I
 
1999
></TT
 
2000
><TT
 
2001
CLASS="LITERAL"
 
2002
>}?</TT
 
2003
> </TD
 
2004
><TD
 
2005
> non-greedy version of <TT
 
2006
CLASS="LITERAL"
 
2007
>{</TT
 
2008
><TT
 
2009
CLASS="REPLACEABLE"
 
2010
><I
 
2011
>m</I
 
2012
></TT
 
2013
><TT
 
2014
CLASS="LITERAL"
 
2015
>}</TT
 
2016
> </TD
 
2017
></TR
 
2018
><TR
 
2019
><TD
 
2020
> <TT
 
2021
CLASS="LITERAL"
 
2022
>{</TT
 
2023
><TT
 
2024
CLASS="REPLACEABLE"
 
2025
><I
 
2026
>m</I
 
2027
></TT
 
2028
><TT
 
2029
CLASS="LITERAL"
 
2030
>,}?</TT
 
2031
> </TD
 
2032
><TD
 
2033
> non-greedy version of <TT
 
2034
CLASS="LITERAL"
 
2035
>{</TT
 
2036
><TT
 
2037
CLASS="REPLACEABLE"
 
2038
><I
 
2039
>m</I
 
2040
></TT
 
2041
><TT
 
2042
CLASS="LITERAL"
 
2043
>,}</TT
 
2044
> </TD
 
2045
></TR
 
2046
><TR
 
2047
><TD
 
2048
>       <TT
 
2049
CLASS="LITERAL"
 
2050
>{</TT
 
2051
><TT
 
2052
CLASS="REPLACEABLE"
 
2053
><I
 
2054
>m</I
 
2055
></TT
 
2056
><TT
 
2057
CLASS="LITERAL"
 
2058
>,</TT
 
2059
><TT
 
2060
CLASS="REPLACEABLE"
 
2061
><I
 
2062
>n</I
 
2063
></TT
 
2064
><TT
 
2065
CLASS="LITERAL"
 
2066
>}?</TT
 
2067
> </TD
 
2068
><TD
 
2069
> non-greedy version of <TT
 
2070
CLASS="LITERAL"
 
2071
>{</TT
 
2072
><TT
 
2073
CLASS="REPLACEABLE"
 
2074
><I
 
2075
>m</I
 
2076
></TT
 
2077
><TT
 
2078
CLASS="LITERAL"
 
2079
>,</TT
 
2080
><TT
 
2081
CLASS="REPLACEABLE"
 
2082
><I
 
2083
>n</I
 
2084
></TT
 
2085
><TT
 
2086
CLASS="LITERAL"
 
2087
>}</TT
 
2088
> </TD
 
2089
></TR
 
2090
></TBODY
 
2091
></TABLE
 
2092
></DIV
 
2093
><P
 
2094
>    The forms using <TT
 
2095
CLASS="LITERAL"
 
2096
>{</TT
 
2097
><TT
 
2098
CLASS="REPLACEABLE"
 
2099
><I
 
2100
>...</I
 
2101
></TT
 
2102
><TT
 
2103
CLASS="LITERAL"
 
2104
>}</TT
 
2105
>
 
2106
    are known as <I
 
2107
CLASS="FIRSTTERM"
 
2108
>bounds</I
 
2109
>.
 
2110
    The numbers <TT
 
2111
CLASS="REPLACEABLE"
 
2112
><I
 
2113
>m</I
 
2114
></TT
 
2115
> and <TT
 
2116
CLASS="REPLACEABLE"
 
2117
><I
 
2118
>n</I
 
2119
></TT
 
2120
> within a bound are
 
2121
    unsigned decimal integers with permissible values from 0 to 255 inclusive.
 
2122
   </P
 
2123
><P
 
2124
>     <I
 
2125
CLASS="FIRSTTERM"
 
2126
>Non-greedy</I
 
2127
> quantifiers (available in AREs only) match the
 
2128
     same possibilities as their corresponding normal (<I
 
2129
CLASS="FIRSTTERM"
 
2130
>greedy</I
 
2131
>)
 
2132
     counterparts, but prefer the smallest number rather than the largest
 
2133
     number of matches.
 
2134
     See <A
 
2135
HREF="functions-matching.html#POSIX-MATCHING-RULES"
 
2136
>Section 9.7.3.5</A
 
2137
> for more detail.
 
2138
   </P
 
2139
><DIV
 
2140
CLASS="NOTE"
 
2141
><BLOCKQUOTE
 
2142
CLASS="NOTE"
 
2143
><P
 
2144
><B
 
2145
>Note: </B
 
2146
>     A quantifier cannot immediately follow another quantifier, e.g.,
 
2147
     <TT
 
2148
CLASS="LITERAL"
 
2149
>**</TT
 
2150
> is invalid.
 
2151
     A quantifier cannot
 
2152
     begin an expression or subexpression or follow
 
2153
     <TT
 
2154
CLASS="LITERAL"
 
2155
>^</TT
 
2156
> or <TT
 
2157
CLASS="LITERAL"
 
2158
>|</TT
 
2159
>.
 
2160
    </P
 
2161
></BLOCKQUOTE
 
2162
></DIV
 
2163
><DIV
 
2164
CLASS="TABLE"
 
2165
><A
 
2166
NAME="POSIX-CONSTRAINTS-TABLE"
 
2167
></A
 
2168
><P
 
2169
><B
 
2170
>Table 9-14. Regular Expression Constraints</B
 
2171
></P
 
2172
><TABLE
 
2173
BORDER="1"
 
2174
CLASS="CALSTABLE"
 
2175
><COL><COL><THEAD
 
2176
><TR
 
2177
><TH
 
2178
>Constraint</TH
 
2179
><TH
 
2180
>Description</TH
 
2181
></TR
 
2182
></THEAD
 
2183
><TBODY
 
2184
><TR
 
2185
><TD
 
2186
> <TT
 
2187
CLASS="LITERAL"
 
2188
>^</TT
 
2189
> </TD
 
2190
><TD
 
2191
> matches at the beginning of the string </TD
 
2192
></TR
 
2193
><TR
 
2194
><TD
 
2195
> <TT
 
2196
CLASS="LITERAL"
 
2197
>$</TT
 
2198
> </TD
 
2199
><TD
 
2200
> matches at the end of the string </TD
 
2201
></TR
 
2202
><TR
 
2203
><TD
 
2204
> <TT
 
2205
CLASS="LITERAL"
 
2206
>(?=</TT
 
2207
><TT
 
2208
CLASS="REPLACEABLE"
 
2209
><I
 
2210
>re</I
 
2211
></TT
 
2212
><TT
 
2213
CLASS="LITERAL"
 
2214
>)</TT
 
2215
> </TD
 
2216
><TD
 
2217
> <I
 
2218
CLASS="FIRSTTERM"
 
2219
>positive lookahead</I
 
2220
> matches at any point
 
2221
       where a substring matching <TT
 
2222
CLASS="REPLACEABLE"
 
2223
><I
 
2224
>re</I
 
2225
></TT
 
2226
> begins
 
2227
       (AREs only) </TD
 
2228
></TR
 
2229
><TR
 
2230
><TD
 
2231
> <TT
 
2232
CLASS="LITERAL"
 
2233
>(?!</TT
 
2234
><TT
 
2235
CLASS="REPLACEABLE"
 
2236
><I
 
2237
>re</I
 
2238
></TT
 
2239
><TT
 
2240
CLASS="LITERAL"
 
2241
>)</TT
 
2242
> </TD
 
2243
><TD
 
2244
> <I
 
2245
CLASS="FIRSTTERM"
 
2246
>negative lookahead</I
 
2247
> matches at any point
 
2248
       where no substring matching <TT
 
2249
CLASS="REPLACEABLE"
 
2250
><I
 
2251
>re</I
 
2252
></TT
 
2253
> begins
 
2254
       (AREs only) </TD
 
2255
></TR
 
2256
></TBODY
 
2257
></TABLE
 
2258
></DIV
 
2259
><P
 
2260
>    Lookahead constraints cannot contain <I
 
2261
CLASS="FIRSTTERM"
 
2262
>back references</I
 
2263
>
 
2264
    (see <A
 
2265
HREF="functions-matching.html#POSIX-ESCAPE-SEQUENCES"
 
2266
>Section 9.7.3.3</A
 
2267
>),
 
2268
    and all parentheses within them are considered non-capturing.
 
2269
   </P
 
2270
></DIV
 
2271
><DIV
 
2272
CLASS="SECT3"
 
2273
><H3
 
2274
CLASS="SECT3"
 
2275
><A
 
2276
NAME="POSIX-BRACKET-EXPRESSIONS"
 
2277
>9.7.3.2. Bracket Expressions</A
 
2278
></H3
 
2279
><P
 
2280
>    A <I
 
2281
CLASS="FIRSTTERM"
 
2282
>bracket expression</I
 
2283
> is a list of
 
2284
    characters enclosed in <TT
 
2285
CLASS="LITERAL"
 
2286
>[]</TT
 
2287
>.  It normally matches
 
2288
    any single character from the list (but see below).  If the list
 
2289
    begins with <TT
 
2290
CLASS="LITERAL"
 
2291
>^</TT
 
2292
>, it matches any single character
 
2293
    <SPAN
 
2294
CLASS="emphasis"
 
2295
><I
 
2296
CLASS="EMPHASIS"
 
2297
>not</I
 
2298
></SPAN
 
2299
> from the rest of the list.
 
2300
    If two characters
 
2301
    in the list are separated by <TT
 
2302
CLASS="LITERAL"
 
2303
>-</TT
 
2304
>, this is
 
2305
    shorthand for the full range of characters between those two
 
2306
    (inclusive) in the collating sequence,
 
2307
    e.g., <TT
 
2308
CLASS="LITERAL"
 
2309
>[0-9]</TT
 
2310
> in <ACRONYM
 
2311
CLASS="ACRONYM"
 
2312
>ASCII</ACRONYM
 
2313
> matches
 
2314
    any decimal digit.  It is illegal for two ranges to share an
 
2315
    endpoint, e.g.,  <TT
 
2316
CLASS="LITERAL"
 
2317
>a-c-e</TT
 
2318
>.  Ranges are very
 
2319
    collating-sequence-dependent, so portable programs should avoid
 
2320
    relying on them.
 
2321
   </P
 
2322
><P
 
2323
>    To include a literal <TT
 
2324
CLASS="LITERAL"
 
2325
>]</TT
 
2326
> in the list, make it the
 
2327
    first character (after <TT
 
2328
CLASS="LITERAL"
 
2329
>^</TT
 
2330
>, if that is used).  To
 
2331
    include a literal <TT
 
2332
CLASS="LITERAL"
 
2333
>-</TT
 
2334
>, make it the first or last
 
2335
    character, or the second endpoint of a range.  To use a literal
 
2336
    <TT
 
2337
CLASS="LITERAL"
 
2338
>-</TT
 
2339
> as the first endpoint of a range, enclose it
 
2340
    in <TT
 
2341
CLASS="LITERAL"
 
2342
>[.</TT
 
2343
> and <TT
 
2344
CLASS="LITERAL"
 
2345
>.]</TT
 
2346
> to make it a
 
2347
    collating element (see below).  With the exception of these characters,
 
2348
    some combinations using <TT
 
2349
CLASS="LITERAL"
 
2350
>[</TT
 
2351
>
 
2352
    (see next paragraphs), and escapes (AREs only), all other special
 
2353
    characters lose their special significance within a bracket expression.
 
2354
    In particular, <TT
 
2355
CLASS="LITERAL"
 
2356
>\</TT
 
2357
> is not special when following
 
2358
    ERE or BRE rules, though it is special (as introducing an escape)
 
2359
    in AREs.
 
2360
   </P
 
2361
><P
 
2362
>    Within a bracket expression, a collating element (a character, a
 
2363
    multiple-character sequence that collates as if it were a single
 
2364
    character, or a collating-sequence name for either) enclosed in
 
2365
    <TT
 
2366
CLASS="LITERAL"
 
2367
>[.</TT
 
2368
> and <TT
 
2369
CLASS="LITERAL"
 
2370
>.]</TT
 
2371
> stands for the
 
2372
    sequence of characters of that collating element.  The sequence is
 
2373
    treated as a single element of the bracket expression's list.  This
 
2374
    allows a bracket
 
2375
    expression containing a multiple-character collating element to
 
2376
    match more than one character, e.g., if the collating sequence
 
2377
    includes a <TT
 
2378
CLASS="LITERAL"
 
2379
>ch</TT
 
2380
> collating element, then the RE
 
2381
    <TT
 
2382
CLASS="LITERAL"
 
2383
>[[.ch.]]*c</TT
 
2384
> matches the first five characters of
 
2385
    <TT
 
2386
CLASS="LITERAL"
 
2387
>chchcc</TT
 
2388
>.
 
2389
   </P
 
2390
><DIV
 
2391
CLASS="NOTE"
 
2392
><BLOCKQUOTE
 
2393
CLASS="NOTE"
 
2394
><P
 
2395
><B
 
2396
>Note: </B
 
2397
>     <SPAN
 
2398
CLASS="PRODUCTNAME"
 
2399
>PostgreSQL</SPAN
 
2400
> currently does not support multi-character collating
 
2401
     elements. This information describes possible future behavior.
 
2402
    </P
 
2403
></BLOCKQUOTE
 
2404
></DIV
 
2405
><P
 
2406
>    Within a bracket expression, a collating element enclosed in
 
2407
    <TT
 
2408
CLASS="LITERAL"
 
2409
>[=</TT
 
2410
> and <TT
 
2411
CLASS="LITERAL"
 
2412
>=]</TT
 
2413
> is an <I
 
2414
CLASS="FIRSTTERM"
 
2415
>equivalence
 
2416
    class</I
 
2417
>, standing for the sequences of characters of all collating
 
2418
    elements equivalent to that one, including itself.  (If there are
 
2419
    no other equivalent collating elements, the treatment is as if the
 
2420
    enclosing delimiters were <TT
 
2421
CLASS="LITERAL"
 
2422
>[.</TT
 
2423
> and
 
2424
    <TT
 
2425
CLASS="LITERAL"
 
2426
>.]</TT
 
2427
>.)  For example, if <TT
 
2428
CLASS="LITERAL"
 
2429
>o</TT
 
2430
> and
 
2431
    <TT
 
2432
CLASS="LITERAL"
 
2433
>^</TT
 
2434
> are the members of an equivalence class, then
 
2435
    <TT
 
2436
CLASS="LITERAL"
 
2437
>[[=o=]]</TT
 
2438
>, <TT
 
2439
CLASS="LITERAL"
 
2440
>[[=^=]]</TT
 
2441
>, and
 
2442
    <TT
 
2443
CLASS="LITERAL"
 
2444
>[o^]</TT
 
2445
> are all synonymous.  An equivalence class
 
2446
    cannot be an endpoint of a range.
 
2447
   </P
 
2448
><P
 
2449
>    Within a bracket expression, the name of a character class
 
2450
    enclosed in <TT
 
2451
CLASS="LITERAL"
 
2452
>[:</TT
 
2453
> and <TT
 
2454
CLASS="LITERAL"
 
2455
>:]</TT
 
2456
> stands
 
2457
    for the list of all characters belonging to that class.  Standard
 
2458
    character class names are: <TT
 
2459
CLASS="LITERAL"
 
2460
>alnum</TT
 
2461
>,
 
2462
    <TT
 
2463
CLASS="LITERAL"
 
2464
>alpha</TT
 
2465
>, <TT
 
2466
CLASS="LITERAL"
 
2467
>blank</TT
 
2468
>,
 
2469
    <TT
 
2470
CLASS="LITERAL"
 
2471
>cntrl</TT
 
2472
>, <TT
 
2473
CLASS="LITERAL"
 
2474
>digit</TT
 
2475
>,
 
2476
    <TT
 
2477
CLASS="LITERAL"
 
2478
>graph</TT
 
2479
>, <TT
 
2480
CLASS="LITERAL"
 
2481
>lower</TT
 
2482
>,
 
2483
    <TT
 
2484
CLASS="LITERAL"
 
2485
>print</TT
 
2486
>, <TT
 
2487
CLASS="LITERAL"
 
2488
>punct</TT
 
2489
>,
 
2490
    <TT
 
2491
CLASS="LITERAL"
 
2492
>space</TT
 
2493
>, <TT
 
2494
CLASS="LITERAL"
 
2495
>upper</TT
 
2496
>,
 
2497
    <TT
 
2498
CLASS="LITERAL"
 
2499
>xdigit</TT
 
2500
>.  These stand for the character classes
 
2501
    defined in
 
2502
    <SPAN
 
2503
CLASS="CITEREFENTRY"
 
2504
><SPAN
 
2505
CLASS="REFENTRYTITLE"
 
2506
>ctype</SPAN
 
2507
></SPAN
 
2508
>.
 
2509
    A locale can provide others.  A character class cannot be used as
 
2510
    an endpoint of a range.
 
2511
   </P
 
2512
><P
 
2513
>    There are two special cases of bracket expressions:  the bracket
 
2514
    expressions <TT
 
2515
CLASS="LITERAL"
 
2516
>[[:&lt;:]]</TT
 
2517
> and
 
2518
    <TT
 
2519
CLASS="LITERAL"
 
2520
>[[:&gt;:]]</TT
 
2521
> are constraints,
 
2522
    matching empty strings at the beginning
 
2523
    and end of a word respectively.  A word is defined as a sequence
 
2524
    of word characters that is neither preceded nor followed by word
 
2525
    characters.  A word character is an <TT
 
2526
CLASS="LITERAL"
 
2527
>alnum</TT
 
2528
> character (as
 
2529
    defined by
 
2530
    <SPAN
 
2531
CLASS="CITEREFENTRY"
 
2532
><SPAN
 
2533
CLASS="REFENTRYTITLE"
 
2534
>ctype</SPAN
 
2535
></SPAN
 
2536
>)
 
2537
    or an underscore.  This is an extension, compatible with but not
 
2538
    specified by <ACRONYM
 
2539
CLASS="ACRONYM"
 
2540
>POSIX</ACRONYM
 
2541
> 1003.2, and should be used with
 
2542
    caution in software intended to be portable to other systems.
 
2543
    The constraint escapes described below are usually preferable; they
 
2544
    are no more standard, but are easier to type.
 
2545
   </P
 
2546
></DIV
 
2547
><DIV
 
2548
CLASS="SECT3"
 
2549
><H3
 
2550
CLASS="SECT3"
 
2551
><A
 
2552
NAME="POSIX-ESCAPE-SEQUENCES"
 
2553
>9.7.3.3. Regular Expression Escapes</A
 
2554
></H3
 
2555
><P
 
2556
>    <I
 
2557
CLASS="FIRSTTERM"
 
2558
>Escapes</I
 
2559
> are special sequences beginning with <TT
 
2560
CLASS="LITERAL"
 
2561
>\</TT
 
2562
>
 
2563
    followed by an alphanumeric character. Escapes come in several varieties:
 
2564
    character entry, class shorthands, constraint escapes, and back references.
 
2565
    A <TT
 
2566
CLASS="LITERAL"
 
2567
>\</TT
 
2568
> followed by an alphanumeric character but not constituting
 
2569
    a valid escape is illegal in AREs.
 
2570
    In EREs, there are no escapes: outside a bracket expression,
 
2571
    a <TT
 
2572
CLASS="LITERAL"
 
2573
>\</TT
 
2574
> followed by an alphanumeric character merely stands for
 
2575
    that character as an ordinary character, and inside a bracket expression,
 
2576
    <TT
 
2577
CLASS="LITERAL"
 
2578
>\</TT
 
2579
> is an ordinary character.
 
2580
    (The latter is the one actual incompatibility between EREs and AREs.)
 
2581
   </P
 
2582
><P
 
2583
>    <I
 
2584
CLASS="FIRSTTERM"
 
2585
>Character-entry escapes</I
 
2586
> exist to make it easier to specify
 
2587
    non-printing and other inconvenient characters in REs.  They are
 
2588
    shown in <A
 
2589
HREF="functions-matching.html#POSIX-CHARACTER-ENTRY-ESCAPES-TABLE"
 
2590
>Table 9-15</A
 
2591
>.
 
2592
   </P
 
2593
><P
 
2594
>    <I
 
2595
CLASS="FIRSTTERM"
 
2596
>Class-shorthand escapes</I
 
2597
> provide shorthands for certain
 
2598
    commonly-used character classes.  They are
 
2599
    shown in <A
 
2600
HREF="functions-matching.html#POSIX-CLASS-SHORTHAND-ESCAPES-TABLE"
 
2601
>Table 9-16</A
 
2602
>.
 
2603
   </P
 
2604
><P
 
2605
>    A <I
 
2606
CLASS="FIRSTTERM"
 
2607
>constraint escape</I
 
2608
> is a constraint,
 
2609
    matching the empty string if specific conditions are met,
 
2610
    written as an escape.  They are
 
2611
    shown in <A
 
2612
HREF="functions-matching.html#POSIX-CONSTRAINT-ESCAPES-TABLE"
 
2613
>Table 9-17</A
 
2614
>.
 
2615
   </P
 
2616
><P
 
2617
>    A <I
 
2618
CLASS="FIRSTTERM"
 
2619
>back reference</I
 
2620
> (<TT
 
2621
CLASS="LITERAL"
 
2622
>\</TT
 
2623
><TT
 
2624
CLASS="REPLACEABLE"
 
2625
><I
 
2626
>n</I
 
2627
></TT
 
2628
>) matches the
 
2629
    same string matched by the previous parenthesized subexpression specified
 
2630
    by the number <TT
 
2631
CLASS="REPLACEABLE"
 
2632
><I
 
2633
>n</I
 
2634
></TT
 
2635
>
 
2636
    (see <A
 
2637
HREF="functions-matching.html#POSIX-CONSTRAINT-BACKREF-TABLE"
 
2638
>Table 9-18</A
 
2639
>).  For example,
 
2640
    <TT
 
2641
CLASS="LITERAL"
 
2642
>([bc])\1</TT
 
2643
> matches <TT
 
2644
CLASS="LITERAL"
 
2645
>bb</TT
 
2646
> or <TT
 
2647
CLASS="LITERAL"
 
2648
>cc</TT
 
2649
>
 
2650
    but not <TT
 
2651
CLASS="LITERAL"
 
2652
>bc</TT
 
2653
> or <TT
 
2654
CLASS="LITERAL"
 
2655
>cb</TT
 
2656
>.
 
2657
    The subexpression must entirely precede the back reference in the RE.
 
2658
    Subexpressions are numbered in the order of their leading parentheses.
 
2659
    Non-capturing parentheses do not define subexpressions.
 
2660
   </P
 
2661
><DIV
 
2662
CLASS="NOTE"
 
2663
><BLOCKQUOTE
 
2664
CLASS="NOTE"
 
2665
><P
 
2666
><B
 
2667
>Note: </B
 
2668
>     Keep in mind that an escape's leading <TT
 
2669
CLASS="LITERAL"
 
2670
>\</TT
 
2671
> will need to be
 
2672
     doubled when entering the pattern as an SQL string constant.  For example:
 
2673
</P><PRE
 
2674
CLASS="PROGRAMLISTING"
 
2675
>'123' ~ E'^\\d{3}' <I
 
2676
CLASS="LINEANNOTATION"
 
2677
>true</I
 
2678
></PRE
 
2679
><P>
 
2680
    </P
 
2681
></BLOCKQUOTE
 
2682
></DIV
 
2683
><DIV
 
2684
CLASS="TABLE"
 
2685
><A
 
2686
NAME="POSIX-CHARACTER-ENTRY-ESCAPES-TABLE"
 
2687
></A
 
2688
><P
 
2689
><B
 
2690
>Table 9-15. Regular Expression Character-entry Escapes</B
 
2691
></P
 
2692
><TABLE
 
2693
BORDER="1"
 
2694
CLASS="CALSTABLE"
 
2695
><COL><COL><THEAD
 
2696
><TR
 
2697
><TH
 
2698
>Escape</TH
 
2699
><TH
 
2700
>Description</TH
 
2701
></TR
 
2702
></THEAD
 
2703
><TBODY
 
2704
><TR
 
2705
><TD
 
2706
> <TT
 
2707
CLASS="LITERAL"
 
2708
>\a</TT
 
2709
> </TD
 
2710
><TD
 
2711
> alert (bell) character, as in C </TD
 
2712
></TR
 
2713
><TR
 
2714
><TD
 
2715
> <TT
 
2716
CLASS="LITERAL"
 
2717
>\b</TT
 
2718
> </TD
 
2719
><TD
 
2720
> backspace, as in C </TD
 
2721
></TR
 
2722
><TR
 
2723
><TD
 
2724
> <TT
 
2725
CLASS="LITERAL"
 
2726
>\B</TT
 
2727
> </TD
 
2728
><TD
 
2729
> synonym for backslash (<TT
 
2730
CLASS="LITERAL"
 
2731
>\</TT
 
2732
>) to help reduce the need for backslash
 
2733
       doubling </TD
 
2734
></TR
 
2735
><TR
 
2736
><TD
 
2737
> <TT
 
2738
CLASS="LITERAL"
 
2739
>\c</TT
 
2740
><TT
 
2741
CLASS="REPLACEABLE"
 
2742
><I
 
2743
>X</I
 
2744
></TT
 
2745
> </TD
 
2746
><TD
 
2747
> (where <TT
 
2748
CLASS="REPLACEABLE"
 
2749
><I
 
2750
>X</I
 
2751
></TT
 
2752
> is any character) the character whose
 
2753
       low-order 5 bits are the same as those of
 
2754
       <TT
 
2755
CLASS="REPLACEABLE"
 
2756
><I
 
2757
>X</I
 
2758
></TT
 
2759
>, and whose other bits are all zero </TD
 
2760
></TR
 
2761
><TR
 
2762
><TD
 
2763
> <TT
 
2764
CLASS="LITERAL"
 
2765
>\e</TT
 
2766
> </TD
 
2767
><TD
 
2768
> the character whose collating-sequence name
 
2769
       is <TT
 
2770
CLASS="LITERAL"
 
2771
>ESC</TT
 
2772
>,
 
2773
       or failing that, the character with octal value 033 </TD
 
2774
></TR
 
2775
><TR
 
2776
><TD
 
2777
> <TT
 
2778
CLASS="LITERAL"
 
2779
>\f</TT
 
2780
> </TD
 
2781
><TD
 
2782
> form feed, as in C </TD
 
2783
></TR
 
2784
><TR
 
2785
><TD
 
2786
> <TT
 
2787
CLASS="LITERAL"
 
2788
>\n</TT
 
2789
> </TD
 
2790
><TD
 
2791
> newline, as in C </TD
 
2792
></TR
 
2793
><TR
 
2794
><TD
 
2795
> <TT
 
2796
CLASS="LITERAL"
 
2797
>\r</TT
 
2798
> </TD
 
2799
><TD
 
2800
> carriage return, as in C </TD
 
2801
></TR
 
2802
><TR
 
2803
><TD
 
2804
> <TT
 
2805
CLASS="LITERAL"
 
2806
>\t</TT
 
2807
> </TD
 
2808
><TD
 
2809
> horizontal tab, as in C </TD
 
2810
></TR
 
2811
><TR
 
2812
><TD
 
2813
> <TT
 
2814
CLASS="LITERAL"
 
2815
>\u</TT
 
2816
><TT
 
2817
CLASS="REPLACEABLE"
 
2818
><I
 
2819
>wxyz</I
 
2820
></TT
 
2821
> </TD
 
2822
><TD
 
2823
> (where <TT
 
2824
CLASS="REPLACEABLE"
 
2825
><I
 
2826
>wxyz</I
 
2827
></TT
 
2828
> is exactly four hexadecimal digits)
 
2829
       the UTF16 (Unicode, 16-bit) character <TT
 
2830
CLASS="LITERAL"
 
2831
>U+</TT
 
2832
><TT
 
2833
CLASS="REPLACEABLE"
 
2834
><I
 
2835
>wxyz</I
 
2836
></TT
 
2837
>
 
2838
       in the local byte ordering </TD
 
2839
></TR
 
2840
><TR
 
2841
><TD
 
2842
> <TT
 
2843
CLASS="LITERAL"
 
2844
>\U</TT
 
2845
><TT
 
2846
CLASS="REPLACEABLE"
 
2847
><I
 
2848
>stuvwxyz</I
 
2849
></TT
 
2850
> </TD
 
2851
><TD
 
2852
> (where <TT
 
2853
CLASS="REPLACEABLE"
 
2854
><I
 
2855
>stuvwxyz</I
 
2856
></TT
 
2857
> is exactly eight hexadecimal
 
2858
       digits)
 
2859
       reserved for a hypothetical Unicode extension to 32 bits
 
2860
       </TD
 
2861
></TR
 
2862
><TR
 
2863
><TD
 
2864
> <TT
 
2865
CLASS="LITERAL"
 
2866
>\v</TT
 
2867
> </TD
 
2868
><TD
 
2869
> vertical tab, as in C </TD
 
2870
></TR
 
2871
><TR
 
2872
><TD
 
2873
> <TT
 
2874
CLASS="LITERAL"
 
2875
>\x</TT
 
2876
><TT
 
2877
CLASS="REPLACEABLE"
 
2878
><I
 
2879
>hhh</I
 
2880
></TT
 
2881
> </TD
 
2882
><TD
 
2883
> (where <TT
 
2884
CLASS="REPLACEABLE"
 
2885
><I
 
2886
>hhh</I
 
2887
></TT
 
2888
> is any sequence of hexadecimal
 
2889
       digits)
 
2890
       the character whose hexadecimal value is
 
2891
       <TT
 
2892
CLASS="LITERAL"
 
2893
>0x</TT
 
2894
><TT
 
2895
CLASS="REPLACEABLE"
 
2896
><I
 
2897
>hhh</I
 
2898
></TT
 
2899
>
 
2900
       (a single character no matter how many hexadecimal digits are used)
 
2901
       </TD
 
2902
></TR
 
2903
><TR
 
2904
><TD
 
2905
> <TT
 
2906
CLASS="LITERAL"
 
2907
>\0</TT
 
2908
> </TD
 
2909
><TD
 
2910
> the character whose value is <TT
 
2911
CLASS="LITERAL"
 
2912
>0</TT
 
2913
> (the null byte)</TD
 
2914
></TR
 
2915
><TR
 
2916
><TD
 
2917
> <TT
 
2918
CLASS="LITERAL"
 
2919
>\</TT
 
2920
><TT
 
2921
CLASS="REPLACEABLE"
 
2922
><I
 
2923
>xy</I
 
2924
></TT
 
2925
> </TD
 
2926
><TD
 
2927
> (where <TT
 
2928
CLASS="REPLACEABLE"
 
2929
><I
 
2930
>xy</I
 
2931
></TT
 
2932
> is exactly two octal digits,
 
2933
       and is not a <I
 
2934
CLASS="FIRSTTERM"
 
2935
>back reference</I
 
2936
>)
 
2937
       the character whose octal value is
 
2938
       <TT
 
2939
CLASS="LITERAL"
 
2940
>0</TT
 
2941
><TT
 
2942
CLASS="REPLACEABLE"
 
2943
><I
 
2944
>xy</I
 
2945
></TT
 
2946
> </TD
 
2947
></TR
 
2948
><TR
 
2949
><TD
 
2950
> <TT
 
2951
CLASS="LITERAL"
 
2952
>\</TT
 
2953
><TT
 
2954
CLASS="REPLACEABLE"
 
2955
><I
 
2956
>xyz</I
 
2957
></TT
 
2958
> </TD
 
2959
><TD
 
2960
> (where <TT
 
2961
CLASS="REPLACEABLE"
 
2962
><I
 
2963
>xyz</I
 
2964
></TT
 
2965
> is exactly three octal digits,
 
2966
       and is not a <I
 
2967
CLASS="FIRSTTERM"
 
2968
>back reference</I
 
2969
>)
 
2970
       the character whose octal value is
 
2971
       <TT
 
2972
CLASS="LITERAL"
 
2973
>0</TT
 
2974
><TT
 
2975
CLASS="REPLACEABLE"
 
2976
><I
 
2977
>xyz</I
 
2978
></TT
 
2979
> </TD
 
2980
></TR
 
2981
></TBODY
 
2982
></TABLE
 
2983
></DIV
 
2984
><P
 
2985
>    Hexadecimal digits are <TT
 
2986
CLASS="LITERAL"
 
2987
>0</TT
 
2988
>-<TT
 
2989
CLASS="LITERAL"
 
2990
>9</TT
 
2991
>,
 
2992
    <TT
 
2993
CLASS="LITERAL"
 
2994
>a</TT
 
2995
>-<TT
 
2996
CLASS="LITERAL"
 
2997
>f</TT
 
2998
>, and <TT
 
2999
CLASS="LITERAL"
 
3000
>A</TT
 
3001
>-<TT
 
3002
CLASS="LITERAL"
 
3003
>F</TT
 
3004
>.
 
3005
    Octal digits are <TT
 
3006
CLASS="LITERAL"
 
3007
>0</TT
 
3008
>-<TT
 
3009
CLASS="LITERAL"
 
3010
>7</TT
 
3011
>.
 
3012
   </P
 
3013
><P
 
3014
>    The character-entry escapes are always taken as ordinary characters.
 
3015
    For example, <TT
 
3016
CLASS="LITERAL"
 
3017
>\135</TT
 
3018
> is <TT
 
3019
CLASS="LITERAL"
 
3020
>]</TT
 
3021
> in ASCII, but
 
3022
    <TT
 
3023
CLASS="LITERAL"
 
3024
>\135</TT
 
3025
> does not terminate a bracket expression.
 
3026
   </P
 
3027
><DIV
 
3028
CLASS="TABLE"
 
3029
><A
 
3030
NAME="POSIX-CLASS-SHORTHAND-ESCAPES-TABLE"
 
3031
></A
 
3032
><P
 
3033
><B
 
3034
>Table 9-16. Regular Expression Class-shorthand Escapes</B
 
3035
></P
 
3036
><TABLE
 
3037
BORDER="1"
 
3038
CLASS="CALSTABLE"
 
3039
><COL><COL><THEAD
 
3040
><TR
 
3041
><TH
 
3042
>Escape</TH
 
3043
><TH
 
3044
>Description</TH
 
3045
></TR
 
3046
></THEAD
 
3047
><TBODY
 
3048
><TR
 
3049
><TD
 
3050
> <TT
 
3051
CLASS="LITERAL"
 
3052
>\d</TT
 
3053
> </TD
 
3054
><TD
 
3055
> <TT
 
3056
CLASS="LITERAL"
 
3057
>[[:digit:]]</TT
 
3058
> </TD
 
3059
></TR
 
3060
><TR
 
3061
><TD
 
3062
> <TT
 
3063
CLASS="LITERAL"
 
3064
>\s</TT
 
3065
> </TD
 
3066
><TD
 
3067
> <TT
 
3068
CLASS="LITERAL"
 
3069
>[[:space:]]</TT
 
3070
> </TD
 
3071
></TR
 
3072
><TR
 
3073
><TD
 
3074
> <TT
 
3075
CLASS="LITERAL"
 
3076
>\w</TT
 
3077
> </TD
 
3078
><TD
 
3079
> <TT
 
3080
CLASS="LITERAL"
 
3081
>[[:alnum:]_]</TT
 
3082
>
 
3083
       (note underscore is included) </TD
 
3084
></TR
 
3085
><TR
 
3086
><TD
 
3087
> <TT
 
3088
CLASS="LITERAL"
 
3089
>\D</TT
 
3090
> </TD
 
3091
><TD
 
3092
> <TT
 
3093
CLASS="LITERAL"
 
3094
>[^[:digit:]]</TT
 
3095
> </TD
 
3096
></TR
 
3097
><TR
 
3098
><TD
 
3099
> <TT
 
3100
CLASS="LITERAL"
 
3101
>\S</TT
 
3102
> </TD
 
3103
><TD
 
3104
> <TT
 
3105
CLASS="LITERAL"
 
3106
>[^[:space:]]</TT
 
3107
> </TD
 
3108
></TR
 
3109
><TR
 
3110
><TD
 
3111
> <TT
 
3112
CLASS="LITERAL"
 
3113
>\W</TT
 
3114
> </TD
 
3115
><TD
 
3116
> <TT
 
3117
CLASS="LITERAL"
 
3118
>[^[:alnum:]_]</TT
 
3119
>
 
3120
       (note underscore is included) </TD
 
3121
></TR
 
3122
></TBODY
 
3123
></TABLE
 
3124
></DIV
 
3125
><P
 
3126
>    Within bracket expressions, <TT
 
3127
CLASS="LITERAL"
 
3128
>\d</TT
 
3129
>, <TT
 
3130
CLASS="LITERAL"
 
3131
>\s</TT
 
3132
>,
 
3133
    and <TT
 
3134
CLASS="LITERAL"
 
3135
>\w</TT
 
3136
> lose their outer brackets,
 
3137
    and <TT
 
3138
CLASS="LITERAL"
 
3139
>\D</TT
 
3140
>, <TT
 
3141
CLASS="LITERAL"
 
3142
>\S</TT
 
3143
>, and <TT
 
3144
CLASS="LITERAL"
 
3145
>\W</TT
 
3146
> are illegal.
 
3147
    (So, for example, <TT
 
3148
CLASS="LITERAL"
 
3149
>[a-c\d]</TT
 
3150
> is equivalent to
 
3151
    <TT
 
3152
CLASS="LITERAL"
 
3153
>[a-c[:digit:]]</TT
 
3154
>.
 
3155
    Also, <TT
 
3156
CLASS="LITERAL"
 
3157
>[a-c\D]</TT
 
3158
>, which is equivalent to
 
3159
    <TT
 
3160
CLASS="LITERAL"
 
3161
>[a-c^[:digit:]]</TT
 
3162
>, is illegal.)
 
3163
   </P
 
3164
><DIV
 
3165
CLASS="TABLE"
 
3166
><A
 
3167
NAME="POSIX-CONSTRAINT-ESCAPES-TABLE"
 
3168
></A
 
3169
><P
 
3170
><B
 
3171
>Table 9-17. Regular Expression Constraint Escapes</B
 
3172
></P
 
3173
><TABLE
 
3174
BORDER="1"
 
3175
CLASS="CALSTABLE"
 
3176
><COL><COL><THEAD
 
3177
><TR
 
3178
><TH
 
3179
>Escape</TH
 
3180
><TH
 
3181
>Description</TH
 
3182
></TR
 
3183
></THEAD
 
3184
><TBODY
 
3185
><TR
 
3186
><TD
 
3187
> <TT
 
3188
CLASS="LITERAL"
 
3189
>\A</TT
 
3190
> </TD
 
3191
><TD
 
3192
> matches only at the beginning of the string
 
3193
       (see <A
 
3194
HREF="functions-matching.html#POSIX-MATCHING-RULES"
 
3195
>Section 9.7.3.5</A
 
3196
> for how this differs from
 
3197
       <TT
 
3198
CLASS="LITERAL"
 
3199
>^</TT
 
3200
>) </TD
 
3201
></TR
 
3202
><TR
 
3203
><TD
 
3204
> <TT
 
3205
CLASS="LITERAL"
 
3206
>\m</TT
 
3207
> </TD
 
3208
><TD
 
3209
> matches only at the beginning of a word </TD
 
3210
></TR
 
3211
><TR
 
3212
><TD
 
3213
> <TT
 
3214
CLASS="LITERAL"
 
3215
>\M</TT
 
3216
> </TD
 
3217
><TD
 
3218
> matches only at the end of a word </TD
 
3219
></TR
 
3220
><TR
 
3221
><TD
 
3222
> <TT
 
3223
CLASS="LITERAL"
 
3224
>\y</TT
 
3225
> </TD
 
3226
><TD
 
3227
> matches only at the beginning or end of a word </TD
 
3228
></TR
 
3229
><TR
 
3230
><TD
 
3231
> <TT
 
3232
CLASS="LITERAL"
 
3233
>\Y</TT
 
3234
> </TD
 
3235
><TD
 
3236
> matches only at a point that is not the beginning or end of a
 
3237
       word </TD
 
3238
></TR
 
3239
><TR
 
3240
><TD
 
3241
> <TT
 
3242
CLASS="LITERAL"
 
3243
>\Z</TT
 
3244
> </TD
 
3245
><TD
 
3246
> matches only at the end of the string
 
3247
       (see <A
 
3248
HREF="functions-matching.html#POSIX-MATCHING-RULES"
 
3249
>Section 9.7.3.5</A
 
3250
> for how this differs from
 
3251
       <TT
 
3252
CLASS="LITERAL"
 
3253
>$</TT
 
3254
>) </TD
 
3255
></TR
 
3256
></TBODY
 
3257
></TABLE
 
3258
></DIV
 
3259
><P
 
3260
>    A word is defined as in the specification of
 
3261
    <TT
 
3262
CLASS="LITERAL"
 
3263
>[[:&lt;:]]</TT
 
3264
> and <TT
 
3265
CLASS="LITERAL"
 
3266
>[[:&gt;:]]</TT
 
3267
> above.
 
3268
    Constraint escapes are illegal within bracket expressions.
 
3269
   </P
 
3270
><DIV
 
3271
CLASS="TABLE"
 
3272
><A
 
3273
NAME="POSIX-CONSTRAINT-BACKREF-TABLE"
 
3274
></A
 
3275
><P
 
3276
><B
 
3277
>Table 9-18. Regular Expression Back References</B
 
3278
></P
 
3279
><TABLE
 
3280
BORDER="1"
 
3281
CLASS="CALSTABLE"
 
3282
><COL><COL><THEAD
 
3283
><TR
 
3284
><TH
 
3285
>Escape</TH
 
3286
><TH
 
3287
>Description</TH
 
3288
></TR
 
3289
></THEAD
 
3290
><TBODY
 
3291
><TR
 
3292
><TD
 
3293
> <TT
 
3294
CLASS="LITERAL"
 
3295
>\</TT
 
3296
><TT
 
3297
CLASS="REPLACEABLE"
 
3298
><I
 
3299
>m</I
 
3300
></TT
 
3301
> </TD
 
3302
><TD
 
3303
> (where <TT
 
3304
CLASS="REPLACEABLE"
 
3305
><I
 
3306
>m</I
 
3307
></TT
 
3308
> is a nonzero digit)
 
3309
       a back reference to the <TT
 
3310
CLASS="REPLACEABLE"
 
3311
><I
 
3312
>m</I
 
3313
></TT
 
3314
>'th subexpression </TD
 
3315
></TR
 
3316
><TR
 
3317
><TD
 
3318
> <TT
 
3319
CLASS="LITERAL"
 
3320
>\</TT
 
3321
><TT
 
3322
CLASS="REPLACEABLE"
 
3323
><I
 
3324
>mnn</I
 
3325
></TT
 
3326
> </TD
 
3327
><TD
 
3328
> (where <TT
 
3329
CLASS="REPLACEABLE"
 
3330
><I
 
3331
>m</I
 
3332
></TT
 
3333
> is a nonzero digit, and
 
3334
       <TT
 
3335
CLASS="REPLACEABLE"
 
3336
><I
 
3337
>nn</I
 
3338
></TT
 
3339
> is some more digits, and the decimal value
 
3340
       <TT
 
3341
CLASS="REPLACEABLE"
 
3342
><I
 
3343
>mnn</I
 
3344
></TT
 
3345
> is not greater than the number of closing capturing
 
3346
       parentheses seen so far)
 
3347
       a back reference to the <TT
 
3348
CLASS="REPLACEABLE"
 
3349
><I
 
3350
>mnn</I
 
3351
></TT
 
3352
>'th subexpression </TD
 
3353
></TR
 
3354
></TBODY
 
3355
></TABLE
 
3356
></DIV
 
3357
><DIV
 
3358
CLASS="NOTE"
 
3359
><BLOCKQUOTE
 
3360
CLASS="NOTE"
 
3361
><P
 
3362
><B
 
3363
>Note: </B
 
3364
>     There is an inherent ambiguity between octal character-entry
 
3365
     escapes and back references, which is resolved by the following heuristics,
 
3366
     as hinted at above.
 
3367
     A leading zero always indicates an octal escape.
 
3368
     A single non-zero digit, not followed by another digit,
 
3369
     is always taken as a back reference.
 
3370
     A multi-digit sequence not starting with a zero is taken as a back
 
3371
     reference if it comes after a suitable subexpression
 
3372
     (i.e., the number is in the legal range for a back reference),
 
3373
     and otherwise is taken as octal.
 
3374
    </P
 
3375
></BLOCKQUOTE
 
3376
></DIV
 
3377
></DIV
 
3378
><DIV
 
3379
CLASS="SECT3"
 
3380
><H3
 
3381
CLASS="SECT3"
 
3382
><A
 
3383
NAME="POSIX-METASYNTAX"
 
3384
>9.7.3.4. Regular Expression Metasyntax</A
 
3385
></H3
 
3386
><P
 
3387
>    In addition to the main syntax described above, there are some special
 
3388
    forms and miscellaneous syntactic facilities available.
 
3389
   </P
 
3390
><P
 
3391
>    An RE can begin with one of two special <I
 
3392
CLASS="FIRSTTERM"
 
3393
>director</I
 
3394
> prefixes.
 
3395
    If an RE begins with <TT
 
3396
CLASS="LITERAL"
 
3397
>***:</TT
 
3398
>,
 
3399
    the rest of the RE is taken as an ARE.  (This normally has no effect in
 
3400
    <SPAN
 
3401
CLASS="PRODUCTNAME"
 
3402
>PostgreSQL</SPAN
 
3403
>, since REs are assumed to be AREs;
 
3404
    but it does have an effect if ERE or BRE mode had been specified by
 
3405
    the <TT
 
3406
CLASS="REPLACEABLE"
 
3407
><I
 
3408
>flags</I
 
3409
></TT
 
3410
> parameter to a regex function.)
 
3411
    If an RE begins with <TT
 
3412
CLASS="LITERAL"
 
3413
>***=</TT
 
3414
>,
 
3415
    the rest of the RE is taken to be a literal string,
 
3416
    with all characters considered ordinary characters.
 
3417
   </P
 
3418
><P
 
3419
>    An ARE can begin with <I
 
3420
CLASS="FIRSTTERM"
 
3421
>embedded options</I
 
3422
>:
 
3423
    a sequence <TT
 
3424
CLASS="LITERAL"
 
3425
>(?</TT
 
3426
><TT
 
3427
CLASS="REPLACEABLE"
 
3428
><I
 
3429
>xyz</I
 
3430
></TT
 
3431
><TT
 
3432
CLASS="LITERAL"
 
3433
>)</TT
 
3434
>
 
3435
    (where <TT
 
3436
CLASS="REPLACEABLE"
 
3437
><I
 
3438
>xyz</I
 
3439
></TT
 
3440
> is one or more alphabetic characters)
 
3441
    specifies options affecting the rest of the RE.
 
3442
    These options override any previously determined options &mdash;
 
3443
    in particular, they can override the case-sensitivity behavior implied by
 
3444
    a regex operator, or the <TT
 
3445
CLASS="REPLACEABLE"
 
3446
><I
 
3447
>flags</I
 
3448
></TT
 
3449
> parameter to a regex
 
3450
    function.
 
3451
    The available option letters are
 
3452
    shown in <A
 
3453
HREF="functions-matching.html#POSIX-EMBEDDED-OPTIONS-TABLE"
 
3454
>Table 9-19</A
 
3455
>.
 
3456
    Note that these same option letters are used in the <TT
 
3457
CLASS="REPLACEABLE"
 
3458
><I
 
3459
>flags</I
 
3460
></TT
 
3461
>
 
3462
    parameters of regex functions.
 
3463
   </P
 
3464
><DIV
 
3465
CLASS="TABLE"
 
3466
><A
 
3467
NAME="POSIX-EMBEDDED-OPTIONS-TABLE"
 
3468
></A
 
3469
><P
 
3470
><B
 
3471
>Table 9-19. ARE Embedded-option Letters</B
 
3472
></P
 
3473
><TABLE
 
3474
BORDER="1"
 
3475
CLASS="CALSTABLE"
 
3476
><COL><COL><THEAD
 
3477
><TR
 
3478
><TH
 
3479
>Option</TH
 
3480
><TH
 
3481
>Description</TH
 
3482
></TR
 
3483
></THEAD
 
3484
><TBODY
 
3485
><TR
 
3486
><TD
 
3487
> <TT
 
3488
CLASS="LITERAL"
 
3489
>b</TT
 
3490
> </TD
 
3491
><TD
 
3492
> rest of RE is a BRE </TD
 
3493
></TR
 
3494
><TR
 
3495
><TD
 
3496
> <TT
 
3497
CLASS="LITERAL"
 
3498
>c</TT
 
3499
> </TD
 
3500
><TD
 
3501
> case-sensitive matching (overrides operator type) </TD
 
3502
></TR
 
3503
><TR
 
3504
><TD
 
3505
> <TT
 
3506
CLASS="LITERAL"
 
3507
>e</TT
 
3508
> </TD
 
3509
><TD
 
3510
> rest of RE is an ERE </TD
 
3511
></TR
 
3512
><TR
 
3513
><TD
 
3514
> <TT
 
3515
CLASS="LITERAL"
 
3516
>i</TT
 
3517
> </TD
 
3518
><TD
 
3519
> case-insensitive matching (see
 
3520
       <A
 
3521
HREF="functions-matching.html#POSIX-MATCHING-RULES"
 
3522
>Section 9.7.3.5</A
 
3523
>) (overrides operator type) </TD
 
3524
></TR
 
3525
><TR
 
3526
><TD
 
3527
> <TT
 
3528
CLASS="LITERAL"
 
3529
>m</TT
 
3530
> </TD
 
3531
><TD
 
3532
> historical synonym for <TT
 
3533
CLASS="LITERAL"
 
3534
>n</TT
 
3535
> </TD
 
3536
></TR
 
3537
><TR
 
3538
><TD
 
3539
> <TT
 
3540
CLASS="LITERAL"
 
3541
>n</TT
 
3542
> </TD
 
3543
><TD
 
3544
> newline-sensitive matching (see
 
3545
       <A
 
3546
HREF="functions-matching.html#POSIX-MATCHING-RULES"
 
3547
>Section 9.7.3.5</A
 
3548
>) </TD
 
3549
></TR
 
3550
><TR
 
3551
><TD
 
3552
> <TT
 
3553
CLASS="LITERAL"
 
3554
>p</TT
 
3555
> </TD
 
3556
><TD
 
3557
> partial newline-sensitive matching (see
 
3558
       <A
 
3559
HREF="functions-matching.html#POSIX-MATCHING-RULES"
 
3560
>Section 9.7.3.5</A
 
3561
>) </TD
 
3562
></TR
 
3563
><TR
 
3564
><TD
 
3565
> <TT
 
3566
CLASS="LITERAL"
 
3567
>q</TT
 
3568
> </TD
 
3569
><TD
 
3570
> rest of RE is a literal (<SPAN
 
3571
CLASS="QUOTE"
 
3572
>"quoted"</SPAN
 
3573
>) string, all ordinary
 
3574
       characters </TD
 
3575
></TR
 
3576
><TR
 
3577
><TD
 
3578
> <TT
 
3579
CLASS="LITERAL"
 
3580
>s</TT
 
3581
> </TD
 
3582
><TD
 
3583
> non-newline-sensitive matching (default) </TD
 
3584
></TR
 
3585
><TR
 
3586
><TD
 
3587
> <TT
 
3588
CLASS="LITERAL"
 
3589
>t</TT
 
3590
> </TD
 
3591
><TD
 
3592
> tight syntax (default; see below) </TD
 
3593
></TR
 
3594
><TR
 
3595
><TD
 
3596
> <TT
 
3597
CLASS="LITERAL"
 
3598
>w</TT
 
3599
> </TD
 
3600
><TD
 
3601
> inverse partial newline-sensitive (<SPAN
 
3602
CLASS="QUOTE"
 
3603
>"weird"</SPAN
 
3604
>) matching
 
3605
       (see <A
 
3606
HREF="functions-matching.html#POSIX-MATCHING-RULES"
 
3607
>Section 9.7.3.5</A
 
3608
>) </TD
 
3609
></TR
 
3610
><TR
 
3611
><TD
 
3612
> <TT
 
3613
CLASS="LITERAL"
 
3614
>x</TT
 
3615
> </TD
 
3616
><TD
 
3617
> expanded syntax (see below) </TD
 
3618
></TR
 
3619
></TBODY
 
3620
></TABLE
 
3621
></DIV
 
3622
><P
 
3623
>    Embedded options take effect at the <TT
 
3624
CLASS="LITERAL"
 
3625
>)</TT
 
3626
> terminating the sequence.
 
3627
    They can appear only at the start of an ARE (after the
 
3628
    <TT
 
3629
CLASS="LITERAL"
 
3630
>***:</TT
 
3631
> director if any).
 
3632
   </P
 
3633
><P
 
3634
>    In addition to the usual (<I
 
3635
CLASS="FIRSTTERM"
 
3636
>tight</I
 
3637
>) RE syntax, in which all
 
3638
    characters are significant, there is an <I
 
3639
CLASS="FIRSTTERM"
 
3640
>expanded</I
 
3641
> syntax,
 
3642
    available by specifying the embedded <TT
 
3643
CLASS="LITERAL"
 
3644
>x</TT
 
3645
> option.
 
3646
    In the expanded syntax,
 
3647
    white-space characters in the RE are ignored, as are
 
3648
    all characters between a <TT
 
3649
CLASS="LITERAL"
 
3650
>#</TT
 
3651
>
 
3652
    and the following newline (or the end of the RE).  This
 
3653
    permits paragraphing and commenting a complex RE.
 
3654
    There are three exceptions to that basic rule:
 
3655
 
 
3656
    <P
 
3657
></P
 
3658
></P><UL
 
3659
><LI
 
3660
><P
 
3661
>       a white-space character or <TT
 
3662
CLASS="LITERAL"
 
3663
>#</TT
 
3664
> preceded by <TT
 
3665
CLASS="LITERAL"
 
3666
>\</TT
 
3667
> is
 
3668
       retained
 
3669
      </P
 
3670
></LI
 
3671
><LI
 
3672
><P
 
3673
>       white space or <TT
 
3674
CLASS="LITERAL"
 
3675
>#</TT
 
3676
> within a bracket expression is retained
 
3677
      </P
 
3678
></LI
 
3679
><LI
 
3680
><P
 
3681
>       white space and comments cannot appear within multi-character symbols,
 
3682
       such as <TT
 
3683
CLASS="LITERAL"
 
3684
>(?:</TT
 
3685
>
 
3686
      </P
 
3687
></LI
 
3688
></UL
 
3689
><P>
 
3690
 
 
3691
    For this purpose, white-space characters are blank, tab, newline, and
 
3692
    any character that belongs to the <TT
 
3693
CLASS="REPLACEABLE"
 
3694
><I
 
3695
>space</I
 
3696
></TT
 
3697
> character class.
 
3698
   </P
 
3699
><P
 
3700
>    Finally, in an ARE, outside bracket expressions, the sequence
 
3701
    <TT
 
3702
CLASS="LITERAL"
 
3703
>(?#</TT
 
3704
><TT
 
3705
CLASS="REPLACEABLE"
 
3706
><I
 
3707
>ttt</I
 
3708
></TT
 
3709
><TT
 
3710
CLASS="LITERAL"
 
3711
>)</TT
 
3712
>
 
3713
    (where <TT
 
3714
CLASS="REPLACEABLE"
 
3715
><I
 
3716
>ttt</I
 
3717
></TT
 
3718
> is any text not containing a <TT
 
3719
CLASS="LITERAL"
 
3720
>)</TT
 
3721
>)
 
3722
    is a comment, completely ignored.
 
3723
    Again, this is not allowed between the characters of
 
3724
    multi-character symbols, like <TT
 
3725
CLASS="LITERAL"
 
3726
>(?:</TT
 
3727
>.
 
3728
    Such comments are more a historical artifact than a useful facility,
 
3729
    and their use is deprecated; use the expanded syntax instead.
 
3730
   </P
 
3731
><P
 
3732
>    <SPAN
 
3733
CLASS="emphasis"
 
3734
><I
 
3735
CLASS="EMPHASIS"
 
3736
>None</I
 
3737
></SPAN
 
3738
> of these metasyntax extensions is available if
 
3739
    an initial <TT
 
3740
CLASS="LITERAL"
 
3741
>***=</TT
 
3742
> director
 
3743
    has specified that the user's input be treated as a literal string
 
3744
    rather than as an RE.
 
3745
   </P
 
3746
></DIV
 
3747
><DIV
 
3748
CLASS="SECT3"
 
3749
><H3
 
3750
CLASS="SECT3"
 
3751
><A
 
3752
NAME="POSIX-MATCHING-RULES"
 
3753
>9.7.3.5. Regular Expression Matching Rules</A
 
3754
></H3
 
3755
><P
 
3756
>    In the event that an RE could match more than one substring of a given
 
3757
    string, the RE matches the one starting earliest in the string.
 
3758
    If the RE could match more than one substring starting at that point,
 
3759
    either the longest possible match or the shortest possible match will
 
3760
    be taken, depending on whether the RE is <I
 
3761
CLASS="FIRSTTERM"
 
3762
>greedy</I
 
3763
> or
 
3764
    <I
 
3765
CLASS="FIRSTTERM"
 
3766
>non-greedy</I
 
3767
>.
 
3768
   </P
 
3769
><P
 
3770
>    Whether an RE is greedy or not is determined by the following rules:
 
3771
    <P
 
3772
></P
 
3773
></P><UL
 
3774
><LI
 
3775
><P
 
3776
>       Most atoms, and all constraints, have no greediness attribute (because
 
3777
       they cannot match variable amounts of text anyway).
 
3778
      </P
 
3779
></LI
 
3780
><LI
 
3781
><P
 
3782
>       Adding parentheses around an RE does not change its greediness.
 
3783
      </P
 
3784
></LI
 
3785
><LI
 
3786
><P
 
3787
>       A quantified atom with a fixed-repetition quantifier
 
3788
       (<TT
 
3789
CLASS="LITERAL"
 
3790
>{</TT
 
3791
><TT
 
3792
CLASS="REPLACEABLE"
 
3793
><I
 
3794
>m</I
 
3795
></TT
 
3796
><TT
 
3797
CLASS="LITERAL"
 
3798
>}</TT
 
3799
>
 
3800
       or
 
3801
       <TT
 
3802
CLASS="LITERAL"
 
3803
>{</TT
 
3804
><TT
 
3805
CLASS="REPLACEABLE"
 
3806
><I
 
3807
>m</I
 
3808
></TT
 
3809
><TT
 
3810
CLASS="LITERAL"
 
3811
>}?</TT
 
3812
>)
 
3813
       has the same greediness (possibly none) as the atom itself.
 
3814
      </P
 
3815
></LI
 
3816
><LI
 
3817
><P
 
3818
>       A quantified atom with other normal quantifiers (including
 
3819
       <TT
 
3820
CLASS="LITERAL"
 
3821
>{</TT
 
3822
><TT
 
3823
CLASS="REPLACEABLE"
 
3824
><I
 
3825
>m</I
 
3826
></TT
 
3827
><TT
 
3828
CLASS="LITERAL"
 
3829
>,</TT
 
3830
><TT
 
3831
CLASS="REPLACEABLE"
 
3832
><I
 
3833
>n</I
 
3834
></TT
 
3835
><TT
 
3836
CLASS="LITERAL"
 
3837
>}</TT
 
3838
>
 
3839
       with <TT
 
3840
CLASS="REPLACEABLE"
 
3841
><I
 
3842
>m</I
 
3843
></TT
 
3844
> equal to <TT
 
3845
CLASS="REPLACEABLE"
 
3846
><I
 
3847
>n</I
 
3848
></TT
 
3849
>)
 
3850
       is greedy (prefers longest match).
 
3851
      </P
 
3852
></LI
 
3853
><LI
 
3854
><P
 
3855
>       A quantified atom with a non-greedy quantifier (including
 
3856
       <TT
 
3857
CLASS="LITERAL"
 
3858
>{</TT
 
3859
><TT
 
3860
CLASS="REPLACEABLE"
 
3861
><I
 
3862
>m</I
 
3863
></TT
 
3864
><TT
 
3865
CLASS="LITERAL"
 
3866
>,</TT
 
3867
><TT
 
3868
CLASS="REPLACEABLE"
 
3869
><I
 
3870
>n</I
 
3871
></TT
 
3872
><TT
 
3873
CLASS="LITERAL"
 
3874
>}?</TT
 
3875
>
 
3876
       with <TT
 
3877
CLASS="REPLACEABLE"
 
3878
><I
 
3879
>m</I
 
3880
></TT
 
3881
> equal to <TT
 
3882
CLASS="REPLACEABLE"
 
3883
><I
 
3884
>n</I
 
3885
></TT
 
3886
>)
 
3887
       is non-greedy (prefers shortest match).
 
3888
      </P
 
3889
></LI
 
3890
><LI
 
3891
><P
 
3892
>       A branch &mdash; that is, an RE that has no top-level
 
3893
       <TT
 
3894
CLASS="LITERAL"
 
3895
>|</TT
 
3896
> operator &mdash; has the same greediness as the first
 
3897
       quantified atom in it that has a greediness attribute.
 
3898
      </P
 
3899
></LI
 
3900
><LI
 
3901
><P
 
3902
>       An RE consisting of two or more branches connected by the
 
3903
       <TT
 
3904
CLASS="LITERAL"
 
3905
>|</TT
 
3906
> operator is always greedy.
 
3907
      </P
 
3908
></LI
 
3909
></UL
 
3910
><P>
 
3911
   </P
 
3912
><P
 
3913
>    The above rules associate greediness attributes not only with individual
 
3914
    quantified atoms, but with branches and entire REs that contain quantified
 
3915
    atoms.  What that means is that the matching is done in such a way that
 
3916
    the branch, or whole RE, matches the longest or shortest possible
 
3917
    substring <SPAN
 
3918
CLASS="emphasis"
 
3919
><I
 
3920
CLASS="EMPHASIS"
 
3921
>as a whole</I
 
3922
></SPAN
 
3923
>.  Once the length of the entire match
 
3924
    is determined, the part of it that matches any particular subexpression
 
3925
    is determined on the basis of the greediness attribute of that
 
3926
    subexpression, with subexpressions starting earlier in the RE taking
 
3927
    priority over ones starting later.
 
3928
   </P
 
3929
><P
 
3930
>    An example of what this means:
 
3931
</P><PRE
 
3932
CLASS="SCREEN"
 
3933
>SELECT SUBSTRING('XY1234Z', 'Y*([0-9]{1,3})');
 
3934
<I
 
3935
CLASS="LINEANNOTATION"
 
3936
>Result: </I
 
3937
><SAMP
 
3938
CLASS="COMPUTEROUTPUT"
 
3939
>123</SAMP
 
3940
>
 
3941
SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
 
3942
<I
 
3943
CLASS="LINEANNOTATION"
 
3944
>Result: </I
 
3945
><SAMP
 
3946
CLASS="COMPUTEROUTPUT"
 
3947
>1</SAMP
 
3948
></PRE
 
3949
><P>
 
3950
    In the first case, the RE as a whole is greedy because <TT
 
3951
CLASS="LITERAL"
 
3952
>Y*</TT
 
3953
>
 
3954
    is greedy.  It can match beginning at the <TT
 
3955
CLASS="LITERAL"
 
3956
>Y</TT
 
3957
>, and it matches
 
3958
    the longest possible string starting there, i.e., <TT
 
3959
CLASS="LITERAL"
 
3960
>Y123</TT
 
3961
>.
 
3962
    The output is the parenthesized part of that, or <TT
 
3963
CLASS="LITERAL"
 
3964
>123</TT
 
3965
>.
 
3966
    In the second case, the RE as a whole is non-greedy because <TT
 
3967
CLASS="LITERAL"
 
3968
>Y*?</TT
 
3969
>
 
3970
    is non-greedy.  It can match beginning at the <TT
 
3971
CLASS="LITERAL"
 
3972
>Y</TT
 
3973
>, and it matches
 
3974
    the shortest possible string starting there, i.e., <TT
 
3975
CLASS="LITERAL"
 
3976
>Y1</TT
 
3977
>.
 
3978
    The subexpression <TT
 
3979
CLASS="LITERAL"
 
3980
>[0-9]{1,3}</TT
 
3981
> is greedy but it cannot change
 
3982
    the decision as to the overall match length; so it is forced to match
 
3983
    just <TT
 
3984
CLASS="LITERAL"
 
3985
>1</TT
 
3986
>.
 
3987
   </P
 
3988
><P
 
3989
>    In short, when an RE contains both greedy and non-greedy subexpressions,
 
3990
    the total match length is either as long as possible or as short as
 
3991
    possible, according to the attribute assigned to the whole RE.  The
 
3992
    attributes assigned to the subexpressions only affect how much of that
 
3993
    match they are allowed to <SPAN
 
3994
CLASS="QUOTE"
 
3995
>"eat"</SPAN
 
3996
> relative to each other.
 
3997
   </P
 
3998
><P
 
3999
>    The quantifiers <TT
 
4000
CLASS="LITERAL"
 
4001
>{1,1}</TT
 
4002
> and <TT
 
4003
CLASS="LITERAL"
 
4004
>{1,1}?</TT
 
4005
>
 
4006
    can be used to force greediness or non-greediness, respectively,
 
4007
    on a subexpression or a whole RE.
 
4008
   </P
 
4009
><P
 
4010
>    Match lengths are measured in characters, not collating elements.
 
4011
    An empty string is considered longer than no match at all.
 
4012
    For example:
 
4013
    <TT
 
4014
CLASS="LITERAL"
 
4015
>bb*</TT
 
4016
>
 
4017
    matches the three middle characters of <TT
 
4018
CLASS="LITERAL"
 
4019
>abbbc</TT
 
4020
>;
 
4021
    <TT
 
4022
CLASS="LITERAL"
 
4023
>(week|wee)(night|knights)</TT
 
4024
>
 
4025
    matches all ten characters of <TT
 
4026
CLASS="LITERAL"
 
4027
>weeknights</TT
 
4028
>;
 
4029
    when <TT
 
4030
CLASS="LITERAL"
 
4031
>(.*).*</TT
 
4032
>
 
4033
    is matched against <TT
 
4034
CLASS="LITERAL"
 
4035
>abc</TT
 
4036
> the parenthesized subexpression
 
4037
    matches all three characters; and when
 
4038
    <TT
 
4039
CLASS="LITERAL"
 
4040
>(a*)*</TT
 
4041
> is matched against <TT
 
4042
CLASS="LITERAL"
 
4043
>bc</TT
 
4044
>
 
4045
    both the whole RE and the parenthesized
 
4046
    subexpression match an empty string.
 
4047
   </P
 
4048
><P
 
4049
>    If case-independent matching is specified,
 
4050
    the effect is much as if all case distinctions had vanished from the
 
4051
    alphabet.
 
4052
    When an alphabetic that exists in multiple cases appears as an
 
4053
    ordinary character outside a bracket expression, it is effectively
 
4054
    transformed into a bracket expression containing both cases,
 
4055
    e.g., <TT
 
4056
CLASS="LITERAL"
 
4057
>x</TT
 
4058
> becomes <TT
 
4059
CLASS="LITERAL"
 
4060
>[xX]</TT
 
4061
>.
 
4062
    When it appears inside a bracket expression, all case counterparts
 
4063
    of it are added to the bracket expression, e.g.,
 
4064
    <TT
 
4065
CLASS="LITERAL"
 
4066
>[x]</TT
 
4067
> becomes <TT
 
4068
CLASS="LITERAL"
 
4069
>[xX]</TT
 
4070
>
 
4071
    and <TT
 
4072
CLASS="LITERAL"
 
4073
>[^x]</TT
 
4074
> becomes <TT
 
4075
CLASS="LITERAL"
 
4076
>[^xX]</TT
 
4077
>.
 
4078
   </P
 
4079
><P
 
4080
>    If newline-sensitive matching is specified, <TT
 
4081
CLASS="LITERAL"
 
4082
>.</TT
 
4083
>
 
4084
    and bracket expressions using <TT
 
4085
CLASS="LITERAL"
 
4086
>^</TT
 
4087
>
 
4088
    will never match the newline character
 
4089
    (so that matches will never cross newlines unless the RE
 
4090
    explicitly arranges it)
 
4091
    and <TT
 
4092
CLASS="LITERAL"
 
4093
>^</TT
 
4094
>and <TT
 
4095
CLASS="LITERAL"
 
4096
>$</TT
 
4097
>
 
4098
    will match the empty string after and before a newline
 
4099
    respectively, in addition to matching at beginning and end of string
 
4100
    respectively.
 
4101
    But the ARE escapes <TT
 
4102
CLASS="LITERAL"
 
4103
>\A</TT
 
4104
> and <TT
 
4105
CLASS="LITERAL"
 
4106
>\Z</TT
 
4107
>
 
4108
    continue to match beginning or end of string <SPAN
 
4109
CLASS="emphasis"
 
4110
><I
 
4111
CLASS="EMPHASIS"
 
4112
>only</I
 
4113
></SPAN
 
4114
>.
 
4115
   </P
 
4116
><P
 
4117
>    If partial newline-sensitive matching is specified,
 
4118
    this affects <TT
 
4119
CLASS="LITERAL"
 
4120
>.</TT
 
4121
> and bracket expressions
 
4122
    as with newline-sensitive matching, but not <TT
 
4123
CLASS="LITERAL"
 
4124
>^</TT
 
4125
>
 
4126
    and <TT
 
4127
CLASS="LITERAL"
 
4128
>$</TT
 
4129
>.
 
4130
   </P
 
4131
><P
 
4132
>    If inverse partial newline-sensitive matching is specified,
 
4133
    this affects <TT
 
4134
CLASS="LITERAL"
 
4135
>^</TT
 
4136
> and <TT
 
4137
CLASS="LITERAL"
 
4138
>$</TT
 
4139
>
 
4140
    as with newline-sensitive matching, but not <TT
 
4141
CLASS="LITERAL"
 
4142
>.</TT
 
4143
>
 
4144
    and bracket expressions.
 
4145
    This isn't very useful but is provided for symmetry.
 
4146
   </P
 
4147
></DIV
 
4148
><DIV
 
4149
CLASS="SECT3"
 
4150
><H3
 
4151
CLASS="SECT3"
 
4152
><A
 
4153
NAME="POSIX-LIMITS-COMPATIBILITY"
 
4154
>9.7.3.6. Limits and Compatibility</A
 
4155
></H3
 
4156
><P
 
4157
>    No particular limit is imposed on the length of REs in this
 
4158
    implementation.  However,
 
4159
    programs intended to be highly portable should not employ REs longer
 
4160
    than 256 bytes,
 
4161
    as a POSIX-compliant implementation can refuse to accept such REs.
 
4162
   </P
 
4163
><P
 
4164
>    The only feature of AREs that is actually incompatible with
 
4165
    POSIX EREs is that <TT
 
4166
CLASS="LITERAL"
 
4167
>\</TT
 
4168
> does not lose its special
 
4169
    significance inside bracket expressions.
 
4170
    All other ARE features use syntax which is illegal or has
 
4171
    undefined or unspecified effects in POSIX EREs;
 
4172
    the <TT
 
4173
CLASS="LITERAL"
 
4174
>***</TT
 
4175
> syntax of directors likewise is outside the POSIX
 
4176
    syntax for both BREs and EREs.
 
4177
   </P
 
4178
><P
 
4179
>    Many of the ARE extensions are borrowed from Perl, but some have
 
4180
    been changed to clean them up, and a few Perl extensions are not present.
 
4181
    Incompatibilities of note include <TT
 
4182
CLASS="LITERAL"
 
4183
>\b</TT
 
4184
>, <TT
 
4185
CLASS="LITERAL"
 
4186
>\B</TT
 
4187
>,
 
4188
    the lack of special treatment for a trailing newline,
 
4189
    the addition of complemented bracket expressions to the things
 
4190
    affected by newline-sensitive matching,
 
4191
    the restrictions on parentheses and back references in lookahead
 
4192
    constraints, and the longest/shortest-match (rather than first-match)
 
4193
    matching semantics.
 
4194
   </P
 
4195
><P
 
4196
>    Two significant incompatibilities exist between AREs and the ERE syntax
 
4197
    recognized by pre-7.4 releases of <SPAN
 
4198
CLASS="PRODUCTNAME"
 
4199
>PostgreSQL</SPAN
 
4200
>:
 
4201
 
 
4202
    <P
 
4203
></P
 
4204
></P><UL
 
4205
><LI
 
4206
><P
 
4207
>       In AREs, <TT
 
4208
CLASS="LITERAL"
 
4209
>\</TT
 
4210
> followed by an alphanumeric character is either
 
4211
       an escape or an error, while in previous releases, it was just another
 
4212
       way of writing the alphanumeric.
 
4213
       This should not be much of a problem because there was no reason to
 
4214
       write such a sequence in earlier releases.
 
4215
      </P
 
4216
></LI
 
4217
><LI
 
4218
><P
 
4219
>       In AREs, <TT
 
4220
CLASS="LITERAL"
 
4221
>\</TT
 
4222
> remains a special character within
 
4223
       <TT
 
4224
CLASS="LITERAL"
 
4225
>[]</TT
 
4226
>, so a literal <TT
 
4227
CLASS="LITERAL"
 
4228
>\</TT
 
4229
> within a bracket
 
4230
       expression must be written <TT
 
4231
CLASS="LITERAL"
 
4232
>\\</TT
 
4233
>.
 
4234
      </P
 
4235
></LI
 
4236
></UL
 
4237
><P>
 
4238
   </P
 
4239
></DIV
 
4240
><DIV
 
4241
CLASS="SECT3"
 
4242
><H3
 
4243
CLASS="SECT3"
 
4244
><A
 
4245
NAME="POSIX-BASIC-REGEXES"
 
4246
>9.7.3.7. Basic Regular Expressions</A
 
4247
></H3
 
4248
><P
 
4249
>    BREs differ from EREs in several respects.
 
4250
    In BREs, <TT
 
4251
CLASS="LITERAL"
 
4252
>|</TT
 
4253
>, <TT
 
4254
CLASS="LITERAL"
 
4255
>+</TT
 
4256
>, and <TT
 
4257
CLASS="LITERAL"
 
4258
>?</TT
 
4259
>
 
4260
    are ordinary characters and there is no equivalent
 
4261
    for their functionality.
 
4262
    The delimiters for bounds are
 
4263
    <TT
 
4264
CLASS="LITERAL"
 
4265
>\{</TT
 
4266
> and <TT
 
4267
CLASS="LITERAL"
 
4268
>\}</TT
 
4269
>,
 
4270
    with <TT
 
4271
CLASS="LITERAL"
 
4272
>{</TT
 
4273
> and <TT
 
4274
CLASS="LITERAL"
 
4275
>}</TT
 
4276
>
 
4277
    by themselves ordinary characters.
 
4278
    The parentheses for nested subexpressions are
 
4279
    <TT
 
4280
CLASS="LITERAL"
 
4281
>\(</TT
 
4282
> and <TT
 
4283
CLASS="LITERAL"
 
4284
>\)</TT
 
4285
>,
 
4286
    with <TT
 
4287
CLASS="LITERAL"
 
4288
>(</TT
 
4289
> and <TT
 
4290
CLASS="LITERAL"
 
4291
>)</TT
 
4292
> by themselves ordinary characters.
 
4293
    <TT
 
4294
CLASS="LITERAL"
 
4295
>^</TT
 
4296
> is an ordinary character except at the beginning of the
 
4297
    RE or the beginning of a parenthesized subexpression,
 
4298
    <TT
 
4299
CLASS="LITERAL"
 
4300
>$</TT
 
4301
> is an ordinary character except at the end of the
 
4302
    RE or the end of a parenthesized subexpression,
 
4303
    and <TT
 
4304
CLASS="LITERAL"
 
4305
>*</TT
 
4306
> is an ordinary character if it appears at the beginning
 
4307
    of the RE or the beginning of a parenthesized subexpression
 
4308
    (after a possible leading <TT
 
4309
CLASS="LITERAL"
 
4310
>^</TT
 
4311
>).
 
4312
    Finally, single-digit back references are available, and
 
4313
    <TT
 
4314
CLASS="LITERAL"
 
4315
>\&lt;</TT
 
4316
> and <TT
 
4317
CLASS="LITERAL"
 
4318
>\&gt;</TT
 
4319
>
 
4320
    are synonyms for
 
4321
    <TT
 
4322
CLASS="LITERAL"
 
4323
>[[:&lt;:]]</TT
 
4324
> and <TT
 
4325
CLASS="LITERAL"
 
4326
>[[:&gt;:]]</TT
 
4327
>
 
4328
    respectively; no other escapes are available in BREs.
 
4329
   </P
 
4330
></DIV
 
4331
></DIV
 
4332
></DIV
 
4333
><DIV
 
4334
CLASS="NAVFOOTER"
 
4335
><HR
 
4336
ALIGN="LEFT"
 
4337
WIDTH="100%"><TABLE
 
4338
SUMMARY="Footer navigation table"
 
4339
WIDTH="100%"
 
4340
BORDER="0"
 
4341
CELLPADDING="0"
 
4342
CELLSPACING="0"
 
4343
><TR
 
4344
><TD
 
4345
WIDTH="33%"
 
4346
ALIGN="left"
 
4347
VALIGN="top"
 
4348
><A
 
4349
HREF="functions-bitstring.html"
 
4350
ACCESSKEY="P"
 
4351
>Prev</A
 
4352
></TD
 
4353
><TD
 
4354
WIDTH="34%"
 
4355
ALIGN="center"
 
4356
VALIGN="top"
 
4357
><A
 
4358
HREF="index.html"
 
4359
ACCESSKEY="H"
 
4360
>Home</A
 
4361
></TD
 
4362
><TD
 
4363
WIDTH="33%"
 
4364
ALIGN="right"
 
4365
VALIGN="top"
 
4366
><A
 
4367
HREF="functions-formatting.html"
 
4368
ACCESSKEY="N"
 
4369
>Next</A
 
4370
></TD
 
4371
></TR
 
4372
><TR
 
4373
><TD
 
4374
WIDTH="33%"
 
4375
ALIGN="left"
 
4376
VALIGN="top"
 
4377
>Bit String Functions and Operators</TD
 
4378
><TD
 
4379
WIDTH="34%"
 
4380
ALIGN="center"
 
4381
VALIGN="top"
 
4382
><A
 
4383
HREF="functions.html"
 
4384
ACCESSKEY="U"
 
4385
>Up</A
 
4386
></TD
 
4387
><TD
 
4388
WIDTH="33%"
 
4389
ALIGN="right"
 
4390
VALIGN="top"
 
4391
>Data Type Formatting Functions</TD
 
4392
></TR
 
4393
></TABLE
 
4394
></DIV
 
4395
></BODY
 
4396
></HTML
 
4397
>
 
 
b'\\ No newline at end of file'