forked from PCRE2Project/pcre2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcre2pattern.3
3884 lines (3884 loc) · 163 KB
/
pcre2pattern.3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.TH PCRE2PATTERN 3 "04 October 2023" "PCRE2 10.43"
.SH NAME
PCRE2 - Perl-compatible regular expressions (revised API)
.SH "PCRE2 REGULAR EXPRESSION DETAILS"
.rs
.sp
The syntax and semantics of the regular expressions that are supported by PCRE2
are described in detail below. There is a quick-reference syntax summary in the
.\" HREF
\fBpcre2syntax\fP
.\"
page. PCRE2 tries to match Perl syntax and semantics as closely as it can.
PCRE2 also supports some alternative regular expression syntax (which does not
conflict with the Perl syntax) in order to provide some compatibility with
regular expressions in Python, .NET, and Oniguruma.
.P
Perl's regular expressions are described in its own documentation, and regular
expressions in general are covered in a number of books, some of which have
copious examples. Jeffrey Friedl's "Mastering Regular Expressions", published
by O'Reilly, covers regular expressions in great detail. This description of
PCRE2's regular expressions is intended as reference material.
.P
This document discusses the regular expression patterns that are supported by
PCRE2 when its main matching function, \fBpcre2_match()\fP, is used. PCRE2 also
has an alternative matching function, \fBpcre2_dfa_match()\fP, which matches
using a different algorithm that is not Perl-compatible. Some of the features
discussed below are not available when DFA matching is used. The advantages and
disadvantages of the alternative function, and how it differs from the normal
function, are discussed in the
.\" HREF
\fBpcre2matching\fP
.\"
page.
.
.
.SH "SPECIAL START-OF-PATTERN ITEMS"
.rs
.sp
A number of options that can be passed to \fBpcre2_compile()\fP can also be set
by special items at the start of a pattern. These are not Perl-compatible, but
are provided to make these options accessible to pattern writers who are not
able to change the program that processes the pattern. Any number of these
items may appear, but they must all be together right at the start of the
pattern string, and the letters must be in upper case.
.
.
.SS "UTF support"
.rs
.sp
In the 8-bit and 16-bit PCRE2 libraries, characters may be coded either as
single code units, or as multiple UTF-8 or UTF-16 code units. UTF-32 can be
specified for the 32-bit library, in which case it constrains the character
values to valid Unicode code points. To process UTF strings, PCRE2 must be
built to include Unicode support (which is the default). When using UTF strings
you must either call the compiling function with one or both of the PCRE2_UTF
or PCRE2_MATCH_INVALID_UTF options, or the pattern must start with the special
sequence (*UTF), which is equivalent to setting the relevant PCRE2_UTF. How
setting a UTF mode affects pattern matching is mentioned in several places
below. There is also a summary of features in the
.\" HREF
\fBpcre2unicode\fP
.\"
page.
.P
Some applications that allow their users to supply patterns may wish to
restrict them to non-UTF data for security reasons. If the PCRE2_NEVER_UTF
option is passed to \fBpcre2_compile()\fP, (*UTF) is not allowed, and its
appearance in a pattern causes an error.
.
.
.SS "Unicode property support"
.rs
.sp
Another special sequence that may appear at the start of a pattern is (*UCP).
This has the same effect as setting the PCRE2_UCP option: it causes sequences
such as \ed and \ew to use Unicode properties to determine character types,
instead of recognizing only characters with codes less than 256 via a lookup
table. If also causes upper/lower casing operations to use Unicode properties
for characters with code points greater than 127, even when UTF is not set.
These behaviours can be changed within the pattern; see the section entitled
.\" HTML <a href="#internaloptions">
.\" </a>
"Internal Option Setting"
.\"
below.
.P
Some applications that allow their users to supply patterns may wish to
restrict them for security reasons. If the PCRE2_NEVER_UCP option is passed to
\fBpcre2_compile()\fP, (*UCP) is not allowed, and its appearance in a pattern
causes an error.
.
.
.SS "Locking out empty string matching"
.rs
.sp
Starting a pattern with (*NOTEMPTY) or (*NOTEMPTY_ATSTART) has the same effect
as passing the PCRE2_NOTEMPTY or PCRE2_NOTEMPTY_ATSTART option to whichever
matching function is subsequently called to match the pattern. These options
lock out the matching of empty strings, either entirely, or only at the start
of the subject.
.
.
.SS "Disabling auto-possessification"
.rs
.sp
If a pattern starts with (*NO_AUTO_POSSESS), it has the same effect as setting
the PCRE2_NO_AUTO_POSSESS option. This stops PCRE2 from making quantifiers
possessive when what follows cannot match the repeated item. For example, by
default a+b is treated as a++b. For more details, see the
.\" HREF
\fBpcre2api\fP
.\"
documentation.
.
.
.SS "Disabling start-up optimizations"
.rs
.sp
If a pattern starts with (*NO_START_OPT), it has the same effect as setting the
PCRE2_NO_START_OPTIMIZE option. This disables several optimizations for quickly
reaching "no match" results. For more details, see the
.\" HREF
\fBpcre2api\fP
.\"
documentation.
.
.
.SS "Disabling automatic anchoring"
.rs
.sp
If a pattern starts with (*NO_DOTSTAR_ANCHOR), it has the same effect as
setting the PCRE2_NO_DOTSTAR_ANCHOR option. This disables optimizations that
apply to patterns whose top-level branches all start with .* (match any number
of arbitrary characters). For more details, see the
.\" HREF
\fBpcre2api\fP
.\"
documentation.
.
.
.SS "Disabling JIT compilation"
.rs
.sp
If a pattern that starts with (*NO_JIT) is successfully compiled, an attempt by
the application to apply the JIT optimization by calling
\fBpcre2_jit_compile()\fP is ignored.
.
.
.SS "Setting match resource limits"
.rs
.sp
The \fBpcre2_match()\fP function contains a counter that is incremented every
time it goes round its main loop. The caller of \fBpcre2_match()\fP can set a
limit on this counter, which therefore limits the amount of computing resource
used for a match. The maximum depth of nested backtracking can also be limited;
this indirectly restricts the amount of heap memory that is used, but there is
also an explicit memory limit that can be set.
.P
These facilities are provided to catch runaway matches that are provoked by
patterns with huge matching trees. A common example is a pattern with nested
unlimited repeats applied to a long string that does not match. When one of
these limits is reached, \fBpcre2_match()\fP gives an error return. The limits
can also be set by items at the start of the pattern of the form
.sp
(*LIMIT_HEAP=d)
(*LIMIT_MATCH=d)
(*LIMIT_DEPTH=d)
.sp
where d is any number of decimal digits. However, the value of the setting must
be less than the value set (or defaulted) by the caller of \fBpcre2_match()\fP
for it to have any effect. In other words, the pattern writer can lower the
limits set by the programmer, but not raise them. If there is more than one
setting of one of these limits, the lower value is used. The heap limit is
specified in kibibytes (units of 1024 bytes).
.P
Prior to release 10.30, LIMIT_DEPTH was called LIMIT_RECURSION. This name is
still recognized for backwards compatibility.
.P
The heap limit applies only when the \fBpcre2_match()\fP or
\fBpcre2_dfa_match()\fP interpreters are used for matching. It does not apply
to JIT. The match limit is used (but in a different way) when JIT is being
used, or when \fBpcre2_dfa_match()\fP is called, to limit computing resource
usage by those matching functions. The depth limit is ignored by JIT but is
relevant for DFA matching, which uses function recursion for recursions within
the pattern and for lookaround assertions and atomic groups. In this case, the
depth limit controls the depth of such recursion.
.
.
.\" HTML <a name="newlines"></a>
.SS "Newline conventions"
.rs
.sp
PCRE2 supports six different conventions for indicating line breaks in
strings: a single CR (carriage return) character, a single LF (linefeed)
character, the two-character sequence CRLF, any of the three preceding, any
Unicode newline sequence, or the NUL character (binary zero). The
.\" HREF
\fBpcre2api\fP
.\"
page has
.\" HTML <a href="pcre2api.html#newlines">
.\" </a>
further discussion
.\"
about newlines, and shows how to set the newline convention when calling
\fBpcre2_compile()\fP.
.P
It is also possible to specify a newline convention by starting a pattern
string with one of the following sequences:
.sp
(*CR) carriage return
(*LF) linefeed
(*CRLF) carriage return, followed by linefeed
(*ANYCRLF) any of the three above
(*ANY) all Unicode newline sequences
(*NUL) the NUL character (binary zero)
.sp
These override the default and the options given to the compiling function. For
example, on a Unix system where LF is the default newline sequence, the pattern
.sp
(*CR)a.b
.sp
changes the convention to CR. That pattern matches "a\enb" because LF is no
longer a newline. If more than one of these settings is present, the last one
is used.
.P
The newline convention affects where the circumflex and dollar assertions are
true. It also affects the interpretation of the dot metacharacter when
PCRE2_DOTALL is not set, and the behaviour of \eN when not followed by an
opening brace. However, it does not affect what the \eR escape sequence
matches. By default, this is any Unicode newline sequence, for Perl
compatibility. However, this can be changed; see the next section and the
description of \eR in the section entitled
.\" HTML <a href="#newlineseq">
.\" </a>
"Newline sequences"
.\"
below. A change of \eR setting can be combined with a change of newline
convention.
.
.
.SS "Specifying what \eR matches"
.rs
.sp
It is possible to restrict \eR to match only CR, LF, or CRLF (instead of the
complete set of Unicode line endings) by setting the option PCRE2_BSR_ANYCRLF
at compile time. This effect can also be achieved by starting a pattern with
(*BSR_ANYCRLF). For completeness, (*BSR_UNICODE) is also recognized,
corresponding to PCRE2_BSR_UNICODE.
.
.
.SH "EBCDIC CHARACTER CODES"
.rs
.sp
PCRE2 can be compiled to run in an environment that uses EBCDIC as its
character code instead of ASCII or Unicode (typically a mainframe system). In
the sections below, character code values are ASCII or Unicode; in an EBCDIC
environment these characters may have different code values, and there are no
code points greater than 255.
.
.
.SH "CHARACTERS AND METACHARACTERS"
.rs
.sp
A regular expression is a pattern that is matched against a subject string from
left to right. Most characters stand for themselves in a pattern, and match the
corresponding characters in the subject. As a trivial example, the pattern
.sp
The quick brown fox
.sp
matches a portion of a subject string that is identical to itself. When
caseless matching is specified (the PCRE2_CASELESS option or (?i) within the
pattern), letters are matched independently of case. Note that there are two
ASCII characters, K and S, that, in addition to their lower case ASCII
equivalents, are case-equivalent with Unicode U+212A (Kelvin sign) and U+017F
(long S) respectively when either PCRE2_UTF or PCRE2_UCP is set, unless the
PCRE2_EXTRA_CASELESS_RESTRICT option is in force (either passed to
\fBpcre2_compile()\fP or set by (?r) within the pattern).
.P
The power of regular expressions comes from the ability to include wild cards,
character classes, alternatives, and repetitions in the pattern. These are
encoded in the pattern by the use of \fImetacharacters\fP, which do not stand
for themselves but instead are interpreted in some special way.
.P
There are two different sets of metacharacters: those that are recognized
anywhere in the pattern except within square brackets, and those that are
recognized within square brackets. Outside square brackets, the metacharacters
are as follows:
.sp
\e general escape character with several uses
^ assert start of string (or line, in multiline mode)
$ assert end of string (or line, in multiline mode)
. match any character except newline (by default)
[ start character class definition
| start of alternative branch
( start group or control verb
) end group or control verb
* 0 or more quantifier
+ 1 or more quantifier; also "possessive quantifier"
? 0 or 1 quantifier; also quantifier minimizer
{ potential start of min/max quantifier
.sp
Brace characters { and } are also used to enclose data for constructions such
as \eg{2} or \ek{name}. In almost all uses of braces, space and/or horizontal
tab characters that follow { or precede } are allowed and are ignored. In the
case of quantifiers, they may also appear before or after the comma. The
exception to this is \eu{...} which is an ECMAScript compatibility feature
that is recognized only when the PCRE2_EXTRA_ALT_BSUX option is set. ECMAScript
does not ignore such white space; it causes the item to be interpreted as
literal.
.P
Part of a pattern that is in square brackets is called a "character class". In
a character class the only metacharacters are:
.sp
\e general escape character
^ negate the class, but only if the first character
- indicates character range
[ POSIX character class (if followed by POSIX syntax)
] terminates the character class
.sp
If a pattern is compiled with the PCRE2_EXTENDED option, most white space in
the pattern, other than in a character class, within a \eQ...\eE sequence, or
between a # outside a character class and the next newline, inclusive, are
ignored. An escaping backslash can be used to include a white space or a #
character as part of the pattern. If the PCRE2_EXTENDED_MORE option is set, the
same applies, but in addition unescaped space and horizontal tab characters are
ignored inside a character class. Note: only these two characters are ignored,
not the full set of pattern white space characters that are ignored outside a
character class. Option settings can be changed within a pattern; see the
section entitled
.\" HTML <a href="#internaloptions">
.\" </a>
"Internal Option Setting"
.\"
below.
.P
The following sections describe the use of each of the metacharacters.
.
.
.SH BACKSLASH
.rs
.sp
The backslash character has several uses. Firstly, if it is followed by a
character that is not a digit or a letter, it takes away any special meaning
that character may have. This use of backslash as an escape character applies
both inside and outside character classes.
.P
For example, if you want to match a * character, you must write \e* in the
pattern. This escaping action applies whether or not the following character
would otherwise be interpreted as a metacharacter, so it is always safe to
precede a non-alphanumeric with backslash to specify that it stands for itself.
In particular, if you want to match a backslash, you write \e\e.
.P
Only ASCII digits and letters have any special meaning after a backslash. All
other characters (in particular, those whose code points are greater than 127)
are treated as literals.
.P
If you want to treat all characters in a sequence as literals, you can do so by
putting them between \eQ and \eE. Note that this includes white space even when
the PCRE2_EXTENDED option is set so that most other white space is ignored. The
behaviour is different from Perl in that $ and @ are handled as literals in
\eQ...\eE sequences in PCRE2, whereas in Perl, $ and @ cause variable
interpolation. Also, Perl does "double-quotish backslash interpolation" on any
backslashes between \eQ and \eE which, its documentation says, "may lead to
confusing results". PCRE2 treats a backslash between \eQ and \eE just like any
other character. Note the following examples:
.sp
Pattern PCRE2 matches Perl matches
.sp
.\" JOIN
\eQabc$xyz\eE abc$xyz abc followed by the
contents of $xyz
\eQabc\e$xyz\eE abc\e$xyz abc\e$xyz
\eQabc\eE\e$\eQxyz\eE abc$xyz abc$xyz
\eQA\eB\eE A\eB A\eB
\eQ\e\eE \e \e\eE
.sp
The \eQ...\eE sequence is recognized both inside and outside character classes.
An isolated \eE that is not preceded by \eQ is ignored. If \eQ is not followed
by \eE later in the pattern, the literal interpretation continues to the end of
the pattern (that is, \eE is assumed at the end). If the isolated \eQ is inside
a character class, this causes an error, because the character class is then
not terminated by a closing square bracket.
.
.
.\" HTML <a name="digitsafterbackslash"></a>
.SS "Non-printing characters"
.rs
.sp
A second use of backslash provides a way of encoding non-printing characters
in patterns in a visible manner. There is no restriction on the appearance of
non-printing characters in a pattern, but when a pattern is being prepared by
text editing, it is often easier to use one of the following escape sequences
instead of the binary character it represents. In an ASCII or Unicode
environment, these escapes are as follows:
.sp
\ea alarm, that is, the BEL character (hex 07)
\ecx "control-x", where x is a non-control ASCII character
\ee escape (hex 1B)
\ef form feed (hex 0C)
\en linefeed (hex 0A)
\er carriage return (hex 0D) (but see below)
\et tab (hex 09)
\e0dd character with octal code 0dd
\eddd character with octal code ddd, or backreference
\eo{ddd..} character with octal code ddd..
\exhh character with hex code hh
\ex{hhh..} character with hex code hhh..
\eN{U+hhh..} character with Unicode hex code point hhh..
.sp
By default, after \ex that is not followed by {, from zero to two hexadecimal
digits are read (letters can be in upper or lower case). Any number of
hexadecimal digits may appear between \ex{ and }. If a character other than a
hexadecimal digit appears between \ex{ and }, or if there is no terminating },
an error occurs.
.P
Characters whose code points are less than 256 can be defined by either of the
two syntaxes for \ex or by an octal sequence. There is no difference in the way
they are handled. For example, \exdc is exactly the same as \ex{dc} or \e334.
However, using the braced versions does make such sequences easier to read.
.P
Support is available for some ECMAScript (aka JavaScript) escape sequences via
two compile-time options. If PCRE2_ALT_BSUX is set, the sequence \ex followed
by { is not recognized. Only if \ex is followed by two hexadecimal digits is it
recognized as a character escape. Otherwise it is interpreted as a literal "x"
character. In this mode, support for code points greater than 256 is provided
by \eu, which must be followed by four hexadecimal digits; otherwise it is
interpreted as a literal "u" character.
.P
PCRE2_EXTRA_ALT_BSUX has the same effect as PCRE2_ALT_BSUX and, in addition,
\eu{hhh..} is recognized as the character specified by hexadecimal code point.
There may be any number of hexadecimal digits, but unlike other places that
also use curly brackets, spaces are not allowed and would result in the string
being interpreted as a literal. This syntax is from ECMAScript 6.
.P
The \eN{U+hhh..} escape sequence is recognized only when PCRE2 is operating in
UTF mode. Perl also uses \eN{name} to specify characters by Unicode name; PCRE2
does not support this. Note that when \eN is not followed by an opening brace
(curly bracket) it has an entirely different meaning, matching any character
that is not a newline.
.P
There are some legacy applications where the escape sequence \er is expected to
match a newline. If the PCRE2_EXTRA_ESCAPED_CR_IS_LF option is set, \er in a
pattern is converted to \en so that it matches a LF (linefeed) instead of a CR
(carriage return) character.
.P
An error occurs if \ec is not followed by a character whose ASCII code point
is in the range 32 to 126. The precise effect of \ecx is as follows: if x is a
lower case letter, it is converted to upper case. Then bit 6 of the character
(hex 40) is inverted. Thus \ecA to \ecZ become hex 01 to hex 1A (A is 41, Z is
5A), but \ec{ becomes hex 3B ({ is 7B), and \ec; becomes hex 7B (; is 3B). If
the code unit following \ec has a code point less than 32 or greater than 126,
a compile-time error occurs.
.P
When PCRE2 is compiled in EBCDIC mode, \eN{U+hhh..} is not supported. \ea, \ee,
\ef, \en, \er, and \et generate the appropriate EBCDIC code values. The \ec
escape is processed as specified for Perl in the \fBperlebcdic\fP document. The
only characters that are allowed after \ec are A-Z, a-z, or one of @, [, \e, ],
^, _, or ?. Any other character provokes a compile-time error. The sequence
\ec@ encodes character code 0; after \ec the letters (in either case) encode
characters 1-26 (hex 01 to hex 1A); [, \e, ], ^, and _ encode characters 27-31
(hex 1B to hex 1F), and \ec? becomes either 255 (hex FF) or 95 (hex 5F).
.P
Thus, apart from \ec?, these escapes generate the same character code values as
they do in an ASCII environment, though the meanings of the values mostly
differ. For example, \ecG always generates code value 7, which is BEL in ASCII
but DEL in EBCDIC.
.P
The sequence \ec? generates DEL (127, hex 7F) in an ASCII environment, but
because 127 is not a control character in EBCDIC, Perl makes it generate the
APC character. Unfortunately, there are several variants of EBCDIC. In most of
them the APC character has the value 255 (hex FF), but in the one Perl calls
POSIX-BC its value is 95 (hex 5F). If certain other characters have POSIX-BC
values, PCRE2 makes \ec? generate 95; otherwise it generates 255.
.P
After \e0 up to two further octal digits are read. If there are fewer than two
digits, just those that are present are used. Thus the sequence \e0\ex\e015
specifies two binary zeros followed by a CR character (code value 13). Make
sure you supply two digits after the initial zero if the pattern character that
follows is itself an octal digit.
.P
The escape \eo must be followed by a sequence of octal digits, enclosed in
braces. An error occurs if this is not the case. This escape is a recent
addition to Perl; it provides way of specifying character code points as octal
numbers greater than 0777, and it also allows octal numbers and backreferences
to be unambiguously specified.
.P
For greater clarity and unambiguity, it is best to avoid following \e by a
digit greater than zero. Instead, use \eo{...} or \ex{...} to specify numerical
character code points, and \eg{...} to specify backreferences. The following
paragraphs describe the old, ambiguous syntax.
.P
The handling of a backslash followed by a digit other than 0 is complicated,
and Perl has changed over time, causing PCRE2 also to change.
.P
Outside a character class, PCRE2 reads the digit and any following digits as a
decimal number. If the number is less than 10, begins with the digit 8 or 9, or
if there are at least that many previous capture groups in the expression, the
entire sequence is taken as a \fIbackreference\fP. A description of how this
works is given
.\" HTML <a href="#backreferences">
.\" </a>
later,
.\"
following the discussion of
.\" HTML <a href="#group">
.\" </a>
parenthesized groups.
.\"
Otherwise, up to three octal digits are read to form a character code.
.P
Inside a character class, PCRE2 handles \e8 and \e9 as the literal characters
"8" and "9", and otherwise reads up to three octal digits following the
backslash, using them to generate a data character. Any subsequent digits stand
for themselves. For example, outside a character class:
.sp
\e040 is another way of writing an ASCII space
.\" JOIN
\e40 is the same, provided there are fewer than 40
previous capture groups
\e7 is always a backreference
.\" JOIN
\e11 might be a backreference, or another way of
writing a tab
\e011 is always a tab
\e0113 is a tab followed by the character "3"
.\" JOIN
\e113 might be a backreference, otherwise the
character with octal code 113
.\" JOIN
\e377 might be a backreference, otherwise
the value 255 (decimal)
\e81 is always a backreference
.sp
Note that octal values of 100 or greater that are specified using this syntax
must not be introduced by a leading zero, because no more than three octal
digits are ever read.
.
.
.SS "Constraints on character values"
.rs
.sp
Characters that are specified using octal or hexadecimal numbers are
limited to certain values, as follows:
.sp
8-bit non-UTF mode no greater than 0xff
16-bit non-UTF mode no greater than 0xffff
32-bit non-UTF mode no greater than 0xffffffff
All UTF modes no greater than 0x10ffff and a valid code point
.sp
Invalid Unicode code points are all those in the range 0xd800 to 0xdfff (the
so-called "surrogate" code points). The check for these can be disabled by the
caller of \fBpcre2_compile()\fP by setting the option
PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES. However, this is possible only in UTF-8
and UTF-32 modes, because these values are not representable in UTF-16.
.
.
.SS "Escape sequences in character classes"
.rs
.sp
All the sequences that define a single character value can be used both inside
and outside character classes. In addition, inside a character class, \eb is
interpreted as the backspace character (hex 08).
.P
When not followed by an opening brace, \eN is not allowed in a character class.
\eB, \eR, and \eX are not special inside a character class. Like other
unrecognized alphabetic escape sequences, they cause an error. Outside a
character class, these sequences have different meanings.
.
.
.SS "Unsupported escape sequences"
.rs
.sp
In Perl, the sequences \eF, \el, \eL, \eu, and \eU are recognized by its string
handler and used to modify the case of following characters. By default, PCRE2
does not support these escape sequences in patterns. However, if either of the
PCRE2_ALT_BSUX or PCRE2_EXTRA_ALT_BSUX options is set, \eU matches a "U"
character, and \eu can be used to define a character by code point, as
described above.
.
.
.SS "Absolute and relative backreferences"
.rs
.sp
The sequence \eg followed by a signed or unsigned number, optionally enclosed
in braces, is an absolute or relative backreference. A named backreference
can be coded as \eg{name}. Backreferences are discussed
.\" HTML <a href="#backreferences">
.\" </a>
later,
.\"
following the discussion of
.\" HTML <a href="#group">
.\" </a>
parenthesized groups.
.\"
.
.
.SS "Absolute and relative subroutine calls"
.rs
.sp
For compatibility with Oniguruma, the non-Perl syntax \eg followed by a name or
a number enclosed either in angle brackets or single quotes, is an alternative
syntax for referencing a capture group as a subroutine. Details are discussed
.\" HTML <a href="#onigurumasubroutines">
.\" </a>
later.
.\"
Note that \eg{...} (Perl syntax) and \eg<...> (Oniguruma syntax) are \fInot\fP
synonymous. The former is a backreference; the latter is a
.\" HTML <a href="#groupsassubroutines">
.\" </a>
subroutine
.\"
call.
.
.
.\" HTML <a name="genericchartypes"></a>
.SS "Generic character types"
.rs
.sp
Another use of backslash is for specifying generic character types:
.sp
\ed any decimal digit
\eD any character that is not a decimal digit
\eh any horizontal white space character
\eH any character that is not a horizontal white space character
\eN any character that is not a newline
\es any white space character
\eS any character that is not a white space character
\ev any vertical white space character
\eV any character that is not a vertical white space character
\ew any "word" character
\eW any "non-word" character
.sp
The \eN escape sequence has the same meaning as
.\" HTML <a href="#fullstopdot">
.\" </a>
the "." metacharacter
.\"
when PCRE2_DOTALL is not set, but setting PCRE2_DOTALL does not change the
meaning of \eN. Note that when \eN is followed by an opening brace it has a
different meaning. See the section entitled
.\" HTML <a href="#digitsafterbackslash">
.\" </a>
"Non-printing characters"
.\"
above for details. Perl also uses \eN{name} to specify characters by Unicode
name; PCRE2 does not support this.
.P
Each pair of lower and upper case escape sequences partitions the complete set
of characters into two disjoint sets. Any given character matches one, and only
one, of each pair. The sequences can appear both inside and outside character
classes. They each match one character of the appropriate type. If the current
matching point is at the end of the subject string, all of them fail, because
there is no character to match.
.P
The default \es characters are HT (9), LF (10), VT (11), FF (12), CR (13), and
space (32), which are defined as white space in the "C" locale. This list may
vary if locale-specific matching is taking place. For example, in some locales
the "non-breaking space" character (\exA0) is recognized as white space, and in
others the VT character is not.
.P
A "word" character is an underscore or any character that is a letter or digit.
By default, the definition of letters and digits is controlled by PCRE2's
low-valued character tables, and may vary if locale-specific matching is taking
place (see
.\" HTML <a href="pcre2api.html#localesupport">
.\" </a>
"Locale support"
.\"
in the
.\" HREF
\fBpcre2api\fP
.\"
page). For example, in a French locale such as "fr_FR" in Unix-like systems,
or "french" in Windows, some character codes greater than 127 are used for
accented letters, and these are then matched by \ew. The use of locales with
Unicode is discouraged.
.P
By default, characters whose code points are greater than 127 never match \ed,
\es, or \ew, and always match \eD, \eS, and \eW, although this may be different
for characters in the range 128-255 when locale-specific matching is happening.
These escape sequences retain their original meanings from before Unicode
support was available, mainly for efficiency reasons. If the PCRE2_UCP option
is set, the behaviour is changed so that Unicode properties are used to
determine character types, as follows:
.sp
\ed any character that matches \ep{Nd} (decimal digit)
\es any character that matches \ep{Z} or \eh or \ev
\ew any character that matches \ep{L}, \ep{N}, \ep{Mn}, or \ep{Pc}
.sp
The addition of \ep{Mn} (non-spacing mark) and the replacement of an explicit
test for underscore with a test for \ep{Pc} (connector punctuation) happened in
PCRE2 release 10.43. This brings PCRE2 into line with Perl.
.P
The upper case escapes match the inverse sets of characters. Note that \ed
matches only decimal digits, whereas \ew matches any Unicode digit, as well as
other character categories. Note also that PCRE2_UCP affects \eb, and
\eB because they are defined in terms of \ew and \eW. Matching these sequences
is noticeably slower when PCRE2_UCP is set.
.P
The effect of PCRE2_UCP on any one of these escape sequences can be negated by
the options PCRE2_EXTRA_ASCII_BSD, PCRE2_EXTRA_ASCII_BSS, and
PCRE2_EXTRA_ASCII_BSW, respectively. These options can be set and reset within
a pattern by means of an internal option setting
.\" HTML <a href="#internaloptions">
.\" </a>
(see below).
.\"
.P
The sequences \eh, \eH, \ev, and \eV, in contrast to the other sequences, which
match only ASCII characters by default, always match a specific list of code
points, whether or not PCRE2_UCP is set. The horizontal space characters are:
.sp
U+0009 Horizontal tab (HT)
U+0020 Space
U+00A0 Non-break space
U+1680 Ogham space mark
U+180E Mongolian vowel separator
U+2000 En quad
U+2001 Em quad
U+2002 En space
U+2003 Em space
U+2004 Three-per-em space
U+2005 Four-per-em space
U+2006 Six-per-em space
U+2007 Figure space
U+2008 Punctuation space
U+2009 Thin space
U+200A Hair space
U+202F Narrow no-break space
U+205F Medium mathematical space
U+3000 Ideographic space
.sp
The vertical space characters are:
.sp
U+000A Linefeed (LF)
U+000B Vertical tab (VT)
U+000C Form feed (FF)
U+000D Carriage return (CR)
U+0085 Next line (NEL)
U+2028 Line separator
U+2029 Paragraph separator
.sp
In 8-bit, non-UTF-8 mode, only the characters with code points less than 256
are relevant.
.
.
.\" HTML <a name="newlineseq"></a>
.SS "Newline sequences"
.rs
.sp
Outside a character class, by default, the escape sequence \eR matches any
Unicode newline sequence. In 8-bit non-UTF-8 mode \eR is equivalent to the
following:
.sp
(?>\er\en|\en|\ex0b|\ef|\er|\ex85)
.sp
This is an example of an "atomic group", details of which are given
.\" HTML <a href="#atomicgroup">
.\" </a>
below.
.\"
This particular group matches either the two-character sequence CR followed by
LF, or one of the single characters LF (linefeed, U+000A), VT (vertical tab,
U+000B), FF (form feed, U+000C), CR (carriage return, U+000D), or NEL (next
line, U+0085). Because this is an atomic group, the two-character sequence is
treated as a single unit that cannot be split.
.P
In other modes, two additional characters whose code points are greater than 255
are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029).
Unicode support is not needed for these characters to be recognized.
.P
It is possible to restrict \eR to match only CR, LF, or CRLF (instead of the
complete set of Unicode line endings) by setting the option PCRE2_BSR_ANYCRLF
at compile time. (BSR is an abbreviation for "backslash R".) This can be made
the default when PCRE2 is built; if this is the case, the other behaviour can
be requested via the PCRE2_BSR_UNICODE option. It is also possible to specify
these settings by starting a pattern string with one of the following
sequences:
.sp
(*BSR_ANYCRLF) CR, LF, or CRLF only
(*BSR_UNICODE) any Unicode newline sequence
.sp
These override the default and the options given to the compiling function.
Note that these special settings, which are not Perl-compatible, are recognized
only at the very start of a pattern, and that they must be in upper case. If
more than one of them is present, the last one is used. They can be combined
with a change of newline convention; for example, a pattern can start with:
.sp
(*ANY)(*BSR_ANYCRLF)
.sp
They can also be combined with the (*UTF) or (*UCP) special sequences. Inside a
character class, \eR is treated as an unrecognized escape sequence, and causes
an error.
.
.
.\" HTML <a name="uniextseq"></a>
.SS Unicode character properties
.rs
.sp
When PCRE2 is built with Unicode support (the default), three additional escape
sequences that match characters with specific properties are available. They
can be used in any mode, though in 8-bit and 16-bit non-UTF modes these
sequences are of course limited to testing characters whose code points are
less than U+0100 and U+10000, respectively. In 32-bit non-UTF mode, code points
greater than 0x10ffff (the Unicode limit) may be encountered. These are all
treated as being in the Unknown script and with an unassigned type.
.P
Matching characters by Unicode property is not fast, because PCRE2 has to do a
multistage table lookup in order to find a character's property. That is why
the traditional escape sequences such as \ed and \ew do not use Unicode
properties in PCRE2 by default, though you can make them do so by setting the
PCRE2_UCP option or by starting the pattern with (*UCP).
.P
The extra escape sequences that provide property support are:
.sp
\ep{\fIxx\fP} a character with the \fIxx\fP property
\eP{\fIxx\fP} a character without the \fIxx\fP property
\eX a Unicode extended grapheme cluster
.sp
The property names represented by \fIxx\fP above are not case-sensitive, and in
accordance with Unicode's "loose matching" rules, spaces, hyphens, and
underscores are ignored. There is support for Unicode script names, Unicode
general category properties, "Any", which matches any character (including
newline), Bidi_Class, a number of binary (yes/no) properties, and some special
PCRE2 properties (described
.\" HTML <a href="#extraprops">
.\" </a>
below).
.\"
Certain other Perl properties such as "InMusicalSymbols" are not supported by
PCRE2. Note that \eP{Any} does not match any characters, so always causes a
match failure.
.
.
.
.SS "Script properties for \ep and \eP"
.rs
.sp
There are three different syntax forms for matching a script. Each Unicode
character has a basic script and, optionally, a list of other scripts ("Script
Extensions") with which it is commonly used. Using the Adlam script as an
example, \ep{sc:Adlam} matches characters whose basic script is Adlam, whereas
\ep{scx:Adlam} matches, in addition, characters that have Adlam in their
extensions list. The full names "script" and "script extensions" for the
property types are recognized, and a equals sign is an alternative to the
colon. If a script name is given without a property type, for example,
\ep{Adlam}, it is treated as \ep{scx:Adlam}. Perl changed to this
interpretation at release 5.26 and PCRE2 changed at release 10.40.
.P
Unassigned characters (and in non-UTF 32-bit mode, characters with code points
greater than 0x10FFFF) are assigned the "Unknown" script. Others that are not
part of an identified script are lumped together as "Common". The current list
of recognized script names and their 4-character abbreviations can be obtained
by running this command:
.sp
pcre2test -LS
.sp
.
.
.
.SS "The general category property for \ep and \eP"
.rs
.sp
Each character has exactly one Unicode general category property, specified by
a two-letter abbreviation. For compatibility with Perl, negation can be
specified by including a circumflex between the opening brace and the property
name. For example, \ep{^Lu} is the same as \eP{Lu}.
.P
If only one letter is specified with \ep or \eP, it includes all the general
category properties that start with that letter. In this case, in the absence
of negation, the curly brackets in the escape sequence are optional; these two
examples have the same effect:
.sp
\ep{L}
\epL
.sp
The following general category property codes are supported:
.sp
C Other
Cc Control
Cf Format
Cn Unassigned
Co Private use
Cs Surrogate
.sp
L Letter
Ll Lower case letter
Lm Modifier letter
Lo Other letter
Lt Title case letter
Lu Upper case letter
.sp
M Mark
Mc Spacing mark
Me Enclosing mark
Mn Non-spacing mark
.sp
N Number
Nd Decimal number
Nl Letter number
No Other number
.sp
P Punctuation
Pc Connector punctuation
Pd Dash punctuation
Pe Close punctuation
Pf Final punctuation
Pi Initial punctuation
Po Other punctuation
Ps Open punctuation
.sp
S Symbol
Sc Currency symbol
Sk Modifier symbol
Sm Mathematical symbol
So Other symbol
.sp
Z Separator
Zl Line separator
Zp Paragraph separator
Zs Space separator
.sp
The special property LC, which has the synonym L&, is also supported: it
matches a character that has the Lu, Ll, or Lt property, in other words, a
letter that is not classified as a modifier or "other".
.P
The Cs (Surrogate) property applies only to characters whose code points are in
the range U+D800 to U+DFFF. These characters are no different to any other
character when PCRE2 is not in UTF mode (using the 16-bit or 32-bit library).
However, they are not valid in Unicode strings and so cannot be tested by PCRE2
in UTF mode, unless UTF validity checking has been turned off (see the
discussion of PCRE2_NO_UTF_CHECK in the
.\" HREF
\fBpcre2api\fP
.\"
page).
.P
The long synonyms for property names that Perl supports (such as \ep{Letter})
are not supported by PCRE2, nor is it permitted to prefix any of these
properties with "Is".
.P
No character that is in the Unicode table has the Cn (unassigned) property.
Instead, this property is assumed for any code point that is not in the
Unicode table.
.P
Specifying caseless matching does not affect these escape sequences. For
example, \ep{Lu} always matches only upper case letters. This is different from
the behaviour of current versions of Perl.
.
.
.SS "Binary (yes/no) properties for \ep and \eP"
.rs
.sp
Unicode defines a number of binary properties, that is, properties whose only
values are true or false. You can obtain a list of those that are recognized by
\ep and \eP, along with their abbreviations, by running this command:
.sp
pcre2test -LP
.sp
.
.
.SS "The Bidi_Class property for \ep and \eP"
.rs
.sp
\ep{Bidi_Class:<class>} matches a character with the given class
\ep{BC:<class>} matches a character with the given class
.sp
The recognized classes are:
.sp
AL Arabic letter
AN Arabic number
B paragraph separator
BN boundary neutral
CS common separator
EN European number
ES European separator
ET European terminator
FSI first strong isolate
L left-to-right
LRE left-to-right embedding
LRI left-to-right isolate
LRO left-to-right override
NSM non-spacing mark
ON other neutral
PDF pop directional format
PDI pop directional isolate
R right-to-left
RLE right-to-left embedding
RLI right-to-left isolate
RLO right-to-left override
S segment separator
WS which space
.sp
An equals sign may be used instead of a colon. The class names are
case-insensitive; only the short names listed above are recognized.
.
.