/
perlre.pod
7729 lines (5464 loc) · 245 KB
/
perlre.pod
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
=encoding euc-jp
=head1 NAME
X<regular expression> X<regex> X<regexp>
=begin original
perlre - Perl regular expressions
=end original
perlre - Perl の正規表現
=head1 DESCRIPTION
=begin original
This page describes the syntax of regular expressions in Perl.
=end original
このページでは Perl での正規表現の構文について説明します。
=begin original
If you haven't used regular expressions before, a tutorial introduction
is available in L<perlretut>. If you know just a little about them,
a quick-start introduction is available in L<perlrequick>.
=end original
もしこれまでに正規表現を使ったことがないなら、
L<perlretut> にチュートリアルがあります。
正規表現を少しだけ知っているなら、L<perlrequick> に
クイックスタートがあります。
=begin original
Except for L</The Basics> section, this page assumes you are familiar
with regular expression basics, like what is a "pattern", what does it
look like, and how it is basically used. For a reference on how they
are used, plus various examples of the same, see discussions of C<m//>,
C<s///>, C<qr//> and C<"??"> in L<perlop/"Regexp Quote-Like Operators">.
=end original
L</The Basics> 節を例外として、このページは、「パターン」とは何か、
それはどのような見た目のものか、基本的にどのようにして使われるか、といった
正規表現の基本に親しんでいることを仮定しています。
それらをどのように使うのかやそれに関する様々な例に
関しては、L<perlop/"Regexp Quote-Like Operators"> にある
C<m//>, C<s///>, C<qr//>, C<"??"> の説明を参照して下さい。
=begin original
New in v5.22, L<C<use re 'strict'>|re/'strict' mode> applies stricter
rules than otherwise when compiling regular expression patterns. It can
find things that, while legal, may not be what you intended.
=end original
v5.22 から、L<C<use re 'strict'>|re/'strict' mode> は、
正規表現パターンをコンパイルするときにその他よりもより厳しい規則を
適用します。
これは、正当ではあるけれども、意図していないかもしれないものを
見つけることができます。
=head2 The Basics
X<regular expression, version 8> X<regex, version 8> X<regexp, version 8>
(基本)
=begin original
Regular expressions are strings with the very particular syntax and
meaning described in this document and auxiliary documents referred to
by this one. The strings are called "patterns". Patterns are used to
determine if some other string, called the "target", has (or doesn't
have) the characteristics specified by the pattern. We call this
"matching" the target string against the pattern. Usually the match is
done by having the target be the first operand, and the pattern be the
second operand, of one of the two binary operators C<=~> and C<!~>,
listed in L<perlop/Binding Operators>; and the pattern will have been
converted from an ordinary string by one of the operators in
L<perlop/"Regexp Quote-Like Operators">, like so:
=end original
正規表現とは、この文書と、この文書で参照される補助文書で記述されている、
特定の構文と意味を持つ文字列です。
この文字列は「パターン」と呼ばれます。
パターンは、「ターゲット」と呼ばれる他の文字列が、パターンで指定された
特性を持つ(または持たない)かどうかを判別するために使われます。
これをパターンに対するターゲット文字列の「マッチング」と呼びます。
通常、マッチングは、L<perlop/Binding Operators> にリストされている
二つの二項演算子 C<=~> および C<!~> の一つの、ターゲットを
最初のオペランドにし、パターンを 2 番目のオペランドにすることによって
行われます;
そしてパターンは次のように、L<perlop/"Regexp Quote-Like Operators"> の
演算子の一つによって通常の文字列から変換されています。
$foo =~ m/abc/
=begin original
This evaluates to true if and only if the string in the variable C<$foo>
contains somewhere in it, the sequence of characters "a", "b", then "c".
(The C<=~ m>, or match operator, is described in
L<perlop/m/PATTERN/msixpodualngc>.)
=end original
これは、変数 C<$foo> 内の文字列のどこかに文字並び "a", "b", "c" が
含まれている場合にのみ真と評価されます。
(マッチング演算子 C<=~ m> は、L<perlop/m/PATTERN/msixpodualngc> で
説明されています。)
=begin original
Patterns that aren't already stored in some variable must be delimitted,
at both ends, by delimitter characters. These are often, as in the
example above, forward slashes, and the typical way a pattern is written
in documentation is with those slashes. In most cases, the delimitter
is the same character, fore and aft, but there are a few cases where a
character looks like it has a mirror-image mate, where the opening
version is the beginning delimiter, and the closing one is the ending
delimiter, like
=end original
何らかの変数にまだ格納されていないパターンは、両端が区切り文字で
区切られている必要があります。
これらは上の例のようにスラッシュであることが多く、文書中で
パターンを記述する一般的な方法はこれらのスラッシュです。
ほとんどの場合、区切り文字は前と後で同じ文字ですが、文字が鏡像のように
見える場合がいくつかあります;
次のように、開くのが開始区切り文字で、閉じるのが終了区切り文字です:
$foo =~ m<abc>
=begin original
Most times, the pattern is evaluated in double-quotish context, but it
is possible to choose delimiters to force single-quotish, like
=end original
ほとんどの場合、パターンはダブルクォート風コンテキストで評価されますが、
次のように区切り文字を選択してシングルクォート風を強制することもできます:
$foo =~ m'abc'
=begin original
If the pattern contains its delimiter within it, that delimiter must be
escaped. Prefixing it with a backslash (I<e.g.>, C<"/foo\/bar/">)
serves this purpose.
=end original
パターン内に区切り文字が含まれている場合は、その区切り文字を
エスケープする必要があります。
逆スラッシュを前置すると (例えば、C<"/foo\/bar/">)、この目的を
達成できます。
=begin original
Any single character in a pattern matches that same character in the
target string, unless the character is a I<metacharacter> with a special
meaning described in this document. A sequence of non-metacharacters
matches the same sequence in the target string, as we saw above with
C<m/abc/>.
=end original
パターン中のすべての単一の文字は、その文字が個々でまたはこの文書で
説明されている特別な意味を持った I<メタ文字> である場合以外は、
ターゲット文字列内の同じ文字にマッチングします。
非メタ文字の並びは、前述の C<m/abc/> で見たように、
ターゲット文字列の同じ並びにマッチングします。
=begin original
Only a few characters (all of them being ASCII punctuation characters)
are metacharacters. The most commonly used one is a dot C<".">, which
normally matches almost any character (including a dot itself).
=end original
ほんのいくつかの文字 (全て ASCII の句読点文字) がメタ文字です。
もっとも一般的に使われるものはドット C<"."> で、これは通常
(ドット自身を含む) ほとんどの文字にマッチングします。
=begin original
You can cause characters that normally function as metacharacters to be
interpreted literally by prefixing them with a C<"\">, just like the
pattern's delimiter must be escaped if it also occurs within the
pattern. Thus, C<"\."> matches just a literal dot, C<"."> instead of
its normal meaning. This means that the backslash is also a
metacharacter, so C<"\\"> matches a single C<"\">. And a sequence that
contains an escaped metacharacter matches the same sequence (but without
the escape) in the target string. So, the pattern C</blur\\fl/> would
match any target string that contains the sequence C<"blur\fl">.
=end original
文字は C<"\"> で前置されることで通常はメタ文字としての機能を持っている文字を
リテラルとして処理させられるようになります;
パターンの区切り文字がパターンの中に現れる場合は
エスケープされなければならないのと同様です。
従って、C<"\."> は、通常の意味ではなく、
単にリテラルなドット C<"."> にマッチングするようになります。
つまり、逆スラッシュもメタ文字なので、
"\\" は単一の C<"\"> にマッチングするということです。
エスケープされたメタ文字を含む並びは、ターゲット文字列の中の
同じ並び(但しエスケープなし)にマッチングします。
それで、パターン C</blur\\fl/> は
並び C<"blur\fl"> を含むターゲット文字列にマッチングします。
=begin original
The metacharacter C<"|"> is used to match one thing or another. Thus
=end original
メタ文字 C<"|"> は二つのもののどちらかをマッチングするのに使われます。
従って:
$foo =~ m/this|that/
=begin original
is TRUE if and only if C<$foo> contains either the sequence C<"this"> or
the sequence C<"that">. Like all metacharacters, prefixing the C<"|">
with a backslash makes it match the plain punctuation character; in its
case, the VERTICAL LINE.
=end original
これは C<$foo> に並び C<"this"> または並び C<"that"> のどちらかが
含まれている場合にのみ真になります。
全てのメタ文字と同様、C<"|"> に逆スラッシュを前置すると普通の句読点文字、
この場合は VERTICAL LINE にマッチングします。
$foo =~ m/this\|that/
=begin original
is TRUE if and only if C<$foo> contains the sequence C<"this|that">.
=end original
これは、C<$foo> に C<"this|that"> という並びを含んでいる場合にのみ
真になります。
=begin original
You aren't limited to just a single C<"|">.
=end original
単一の C<"|"> だけに制限されません。
$foo =~ m/fee|fie|foe|fum/
=begin original
is TRUE if and only if C<$foo> contains any of those 4 sequences from
the children's story "Jack and the Beanstalk".
=end original
これは、C<$foo> に童話「ジャックとまめの木」から取った
四つの並びのいずれがを含んでいる場合にのみ真になります。
=begin original
As you can see, the C<"|"> binds less tightly than a sequence of
ordinary characters. We can override this by using the grouping
metacharacters, the parentheses C<"("> and C<")">.
=end original
ここで見られるように、C<"|"> は通常の文字の並びより弱く結びつけます。
これはグループ化メタ文字であるかっこ C<"("> と C<")"> を使って
上書きできます。
$foo =~ m/th(is|at) thing/
=begin original
is TRUE if and only if C<$foo> contains either the sequence S<C<"this
thing">> or the sequence S<C<"that thing">>. The portions of the string
that match the portions of the pattern enclosed in parentheses are
normally made available separately for use later in the pattern,
substitution, or program. This is called "capturing", and it can get
complicated. See L</Capture groups>.
=end original
これは C<$foo> に並び S<C<"this thing">> または並び
S<C<"that thing">> のいずれかが含まれている場合にのみ TRUE が返されます。
かっこで囲まれたパターンの部分と一致する文字列の部分は、通常、後でパターン、
置換、プログラムで使用するために個別に使用できます。
これは「捕捉」(capturing)と呼ばれ、複雑になる場合があります。
L</Capture groups> を参照してください。
=begin original
The first alternative includes everything from the last pattern
delimiter (C<"(">, C<"(?:"> (described later), I<etc>. or the beginning
of the pattern) up to the first C<"|">, and the last alternative
contains everything from the last C<"|"> to the next closing pattern
delimiter. That's why it's common practice to include alternatives in
parentheses: to minimize confusion about where they start and end.
=end original
最初の代替には最後のパターン区切り (C<"(">, C<"(?:"> (後述) など、または
パターンの始まり)から最初の C<"|"> までのすべてが含まれ、
最後の代替には最後の C<"|"> から次の閉じパターン区切りまでが含まれます。
通常代替をかっこの中に入れるのは、その開始位置と終了位置が少しはわかりやすく
なるようにです。
=begin original
Alternatives are tried from left to right, so the first
alternative found for which the entire expression matches, is the one that
is chosen. This means that alternatives are not necessarily greedy. For
example: when matching C<foo|foot> against C<"barefoot">, only the C<"foo">
part will match, as that is the first alternative tried, and it successfully
matches the target string. (This might not seem important, but it is
important when you are capturing matched text using parentheses.)
=end original
代替は左から右へと試されます、なので最初の代替がその完全な式で
マッチングしたのならそれが選択されます。
これは代替は貪欲である必要はないということを意味します。
例えば: C<"barefoot"> に対して C<foo|foot> をマッチングさせると、
最初の代替から試されるので、C<"foo"> の部分がマッチングし、
これは対象の文字列に対して成功でマッチングします。
(これは重要ではないでしょうが、かっこを使ってマッチングしたテキストを
捕捉しているときには重要でしょう。)
=begin original
Besides taking away the special meaning of a metacharacter, a prefixed
backslash changes some letter and digit characters away from matching
just themselves to instead have special meaning. These are called
"escape sequences", and all such are described in L<perlrebackslash>. A
backslash sequence (of a letter or digit) that doesn't currently have
special meaning to Perl will raise a warning if warnings are enabled,
as those are reserved for potential future use.
=end original
接頭辞付き逆スラッシュは、メタ文字の特殊な意味を取り除くだけでなく、
一部の文字や数字をそれ自体と一致させないように変更し、
代わりに特殊な意味を持つようにします。
これらは「エスケープシーケンス」と呼ばれ、L<perlrebackslash> で
説明されています。
現在 Perl にとって特殊な意味を持たない (文字や数字の)
逆スラッシュシーケンスは、警告が有効になっている場合に警告を発します;
これらは将来使用するために予約されているためです。
=begin original
One such sequence is C<\b>, which matches a boundary of some sort.
C<\b{wb}> and a few others give specialized types of boundaries.
(They are all described in detail starting at
L<perlrebackslash/\b{}, \b, \B{}, \B>.) Note that these don't match
characters, but the zero-width spaces between characters. They are an
example of a L<zero-width assertion|/Assertions>. Consider again,
=end original
そのようなシーケンスのひとつは C<\b> です;
これはある種の境界にマッチします。
C<\b{wb}> やその他のいくつかは特定の境界を与えます。
(これらはすべて L<perlrebackslash/\b{}, \b, \B{}, \B> で詳細に
記述されています。)
これらは文字ではなく、文字と文字の間のゼロ幅の
スペースにマッチングすることに注意してください。
これらは L<ゼロ幅言明|/Assertions> の例です。
もう一度考えてみます:
$foo =~ m/fee|fie|foe|fum/
=begin original
It evaluates to TRUE if, besides those 4 words, any of the sequences
"feed", "field", "Defoe", "fume", and many others are in C<$foo>. By
judicious use of C<\b> (or better (because it is designed to handle
natural language) C<\b{wb}>), we can make sure that only the Giant's
words are matched:
=end original
これは、これら四つの単語以外に、"feed", "field", "Defoe", "fume",
その他多くのシーケンスのいずれかが C<$foo> にある場合、TRUE と評価されます。
C<\b>(または(自然言語を処理するように設計されているため) より良い
C<\b{wb}>)を慎重に使用することで、
確実に巨人の単語だけが一致するようにできます。
$foo =~ m/\b(fee|fie|foe|fum)\b/
$foo =~ m/\b{wb}(fee|fie|foe|fum)\b{wb}/
=begin original
The final example shows that the characters C<"{"> and C<"}"> are
metacharacters.
=end original
最後の例は、文字 C<"{"> と C<"}"> がメタ文字であることを示しています。
=begin original
Another use for escape sequences is to specify characters that cannot
(or which you prefer not to) be written literally. These are described
in detail in L<perlrebackslash/Character Escapes>, but the next three
paragraphs briefly describe some of them.
=end original
エスケープシーケンスのもう一つの使用法は、文字通りに書くことができない
(あるいは書きたくない)文字を指定することです。
これらについては
L<perlrebackslash/Character Escapes> で詳しく説明していますが、
次の三つの段落でその一部を簡単に説明します。
=begin original
Various control characters can be written in C language style: C<"\n">
matches a newline, C<"\t"> a tab, C<"\r"> a carriage return, C<"\f"> a
form feed, I<etc>.
=end original
様々な制御文字は C 言語形式で書くことができます:
"\n" は改行にマッチングし、C<"\t"> はタブに、C<"\r"> は復帰に、
C<"\f"> はフォームフィードにといった具合にマッチングします。
=begin original
More generally, C<\I<nnn>>, where I<nnn> is a string of three octal
digits, matches the character whose native code point is I<nnn>. You
can easily run into trouble if you don't have exactly three digits. So
always use three, or since Perl 5.14, you can use C<\o{...}> to specify
any number of octal digits.
=end original
より一般的に、C<\I<nnn>> (I<nnn> は 3 桁の 8 進数字) は
ネイティブな符号位置が I<nnn> の文字にマッチングします。
正確に 3 桁以外の数字を使うと、簡単に困難に陥ります。
従って、常に 3 桁で使うか、Perl 5.14 以降なら、
任意の桁の 8 進数を使うために C<\o{...}> を使えます。
=begin original
Similarly, C<\xI<nn>>, where I<nn> are hexadecimal digits, matches the
character whose native ordinal is I<nn>. Again, not using exactly two
digits is a recipe for disaster, but you can use C<\x{...}> to specify
any number of hex digits.
=end original
同じように、\xI<nn> (I<nn> は16進数字) はネイティブな数値で I<nn> に
なる文字にマッチングします。
再び、正確に 2 桁以外の数字を使うのは災いの元ですが、
任意の桁の 16 進数を指定するために C<\x{...}> を使えます。
=begin original
Besides being a metacharacter, the C<"."> is an example of a "character
class", something that can match any single character of a given set of
them. In its case, the set is just about all possible characters. Perl
predefines several character classes besides the C<".">; there is a
separate reference page about just these, L<perlrecharclass>.
=end original
メタ文字であることに加えて、C<"."> は、特定の集合の任意の 1 文字に
マッチングする「文字クラス」の例です。
この場合、集合ははほぼすべての可能な文字です。
Perlは C<"."> 以外にもいくつかの文字クラスを事前定義しています;
これらについては、L<perlrecharclass> という別のリファレンスページが
あります。
=begin original
You can define your own custom character classes, by putting into your
pattern in the appropriate place(s), a list of all the characters you
want in the set. You do this by enclosing the list within C<[]> bracket
characters. These are called "bracketed character classes" when we are
being precise, but often the word "bracketed" is dropped. (Dropping it
usually doesn't cause confusion.) This means that the C<"["> character
is another metacharacter. It doesn't match anything just by itself; it
is used only to tell Perl that what follows it is a bracketed character
class. If you want to match a literal left square bracket, you must
escape it, like C<"\[">. The matching C<"]"> is also a metacharacter;
again it doesn't match anything by itself, but just marks the end of
your custom class to Perl. It is an example of a "sometimes
metacharacter". It isn't a metacharacter if there is no corresponding
C<"[">, and matches its literal self:
=end original
独自のカスタム文字クラスを定義するには、パターン内の適切な場所に、集合内に
必要なすべての文字のリストを配置します。
これを行うには、リストを C<[]> 大かっこ文字で囲みます。
これらは、正確にであれば「大かっこ文字クラス」と呼ばれますが、
「大かっこ」という単語が削除されることがよくあります。
(通常は、これを削除しても混乱は生じません。)
これは、C<"["> 文字はもう一つのメタ文字であることを意味します。
これ自身だけでは何にもマッチングしません;
Perl に対して、後に続くものが大かっこ文字クラスであることを
伝えるためにのみ使用されます。
リテラルの左大かっこにマッチさせたい場合は、C<"\["> のように
エスケープする必要があります。
一致する C<"]"> もメタ文字です;
ここでも何にもマッチしませんが、カスタムクラスの終わりを Perl に
マークするだけです。
これは「時々メタ文字」の例です。
対応する C<"["> が存在しない場合はメタ文字ではなく、
リテラルにマッチングします。
print "]" =~ /]/; # prints 1
=begin original
The list of characters within the character class gives the set of
characters matched by the class. C<"[abc]"> matches a single "a" or "b"
or "c". But if the first character after the C<"["> is C<"^">, the
class instead matches any character not in the list. Within a list, the
C<"-"> character specifies a range of characters, so that C<a-z>
represents all characters between "a" and "z", inclusive. If you want
either C<"-"> or C<"]"> itself to be a member of a class, put it at the
start of the list (possibly after a C<"^">), or escape it with a
backslash. C<"-"> is also taken literally when it is at the end of the
list, just before the closing C<"]">. (The following all specify the
same class of three characters: C<[-az]>, C<[az-]>, and C<[a\-z]>. All
are different from C<[a-z]>, which specifies a class containing
twenty-six characters, even on EBCDIC-based character sets.)
=end original
文字クラスの中の文字のリストは、そのクラスがマッチングする
文字の集合を表しています。
C<"[abc]"> は単一の "a" または "b" または "c" にマッチングします。.
しかし、C<"["> の後の最初の文字が C<"^"> だったときには、その文字クラスは
リストの中にない任意の文字にマッチングします。
リストの中では、文字 C<"-"> は文字の範囲を意味します;
なので C<a-z> は "a" と "z" を含めてそれらの間にあるすべての文字を表します。
文字クラスの要素として C<"-"> または C<"]"> 自身を使いたい時には、
リストの先頭に (あるいは C<"^"> の後に) 置くか、逆スラッシュを使って
エスケープします。
C<"-"> はリストの終端、リストを閉じる C<"]"> の直前にあったときも
リテラルとして扱われます。
(次の例はすべて同じ3文字からなる文字クラスです: C<[-az]>, C<[az-]>,
C<[a\-z]>。
これらはすべて EBCDIC ベースの文字集合であっても26文字からなる文字集合
C<[a-z]> とは異なります。)
=begin original
There is lots more to bracketed character classes; full details are in
L<perlrecharclass/Bracketed Character Classes>.
=end original
大かっこ文字クラスにはもっと色々な要素があります; 完全な詳細は
L<perlrecharclass/Bracketed Character Classes> にあります。
=head3 Metacharacters
X<metacharacter>
X<\> X<^> X<.> X<$> X<|> X<(> X<()> X<[> X<[]>
(メタ文字)
=begin original
L</The Basics> introduced some of the metacharacters. This section
gives them all. Most of them have the same meaning as in the I<egrep>
command.
=end original
L</The Basics> ではメタ文字の一部を導入しました。
この節ではその全てを示します。
そのほとんどは I<egrep> コマンドと同じ意味を持ちます。
=begin original
Only the C<"\"> is always a metacharacter. The others are metacharacters
just sometimes. The following tables lists all of them, summarizes
their use, and gives the contexts where they are metacharacters.
Outside those contexts or if prefixed by a C<"\">, they match their
corresponding punctuation character. In some cases, their meaning
varies depending on various pattern modifiers that alter the default
behaviors. See L</Modifiers>.
=end original
C<"\"> のみが常にメタ文字です。
その他は時々にだけメタ文字です。
次の表は、すべてのメタ文字の一覧、使用方法の概要、
メタ文字になるコンテキストを示しています。
これらのコンテキスト以外では、または C<"\"> で始まる場合は、
対応する句読点文字とマッチングします。
場合によっては、既定の動作を変更するさまざまなパターン修飾子によって
意味が異なります。
L</Modifiers> を参照してください。
=begin original
PURPOSE WHERE
\ Escape the next character Always, except when
escaped by another \
^ Match the beginning of the string Not in []
(or line, if /m is used)
^ Complement the [] class At the beginning of []
. Match any single character except newline Not in []
(under /s, includes newline)
$ Match the end of the string Not in [], but can
(or before newline at the end of the mean interpolate a
string; or before any newline if /m is scalar
used)
| Alternation Not in []
() Grouping Not in []
[ Start Bracketed Character class Not in []
] End Bracketed Character class Only in [], and
not first
* Matches the preceding element 0 or more Not in []
times
+ Matches the preceding element 1 or more Not in []
times
? Matches the preceding element 0 or 1 Not in []
times
{ Starts a sequence that gives number(s) Not in []
of times the preceding element can be
matched
{ when following certain escape sequences
starts a modifier to the meaning of the
sequence
} End sequence started by {
- Indicates a range Only in [] interior
# Beginning of comment, extends to line end Only with /x modifier
=end original
目的 場所
\ 次の文字をエスケープ もう一つの \ で
エスケープしない限り常に
^ 文字列(または /m が使われていれば行) の [] の中以外
先頭にマッチング
^ [] クラスの補集合 [] の先頭
. 改行以外の任意の 1 文字にマッチング [] の中以外
(/s の下では改行を含む)
$ 文字列の末尾にマッチング [] の中以外、しかし
(または文字列の最後の改行の前; スカラの変数展開を
または /m が使われていれば改行の前) 意味する
| 代替 [] の中以外
() グループ化 [] の中以外
[ 大かっこ文字クラスの開始 [] の中以外
] 大かっこ文字クラスの終了 [] のみで先頭以外
* 前にある要素に 0 回以上マッチング [] の中以外
+ 前にある要素に 1 回以上マッチング [] の中以外
? 前にある要素に 0 回または 1 回マッチング [] の中以外
{ 前にある要素がマッチングする回数を指定する [] の中以外
並びの開始
{ 以下のいくつかのエスケープシーケンスで
シーケンスの意味の修飾子の開始
} { で開始した並びの終わり
- 範囲を示す [] の内部のみ
# コメントの開始; 行末まで /x 修飾子のみ
=begin original
Notice that most of the metacharacters lose their special meaning when
they occur in a bracketed character class, except C<"^"> has a different
meaning when it is at the beginning of such a class. And C<"-"> and C<"]">
are metacharacters only at restricted positions within bracketed
character classes; while C<"}"> is a metacharacter only when closing a
special construct started by C<"{">.
=end original
ほとんどのメタ文字は、かっこで囲まれた文字クラス内で出現すると
特殊な意味を失うことに注意してください;
ただし、C<"^"> は、そのようなクラスの先頭では異なる意味を持ちます。
また、C<"-"> と C<"]"> は、かっこ弧で囲まれた文字クラス内の限定された
位置でだけメタ文字になります;
一方、C<"}"> は、C<"{"> によって開始された特殊な構造体を
閉じるときにのみメタ文字です。
=begin original
In double-quotish context, as is usually the case, you need to be
careful about C<"$"> and the non-metacharacter C<"@">. Those could
interpolate variables, which may or may not be what you intended.
=end original
ダブルクォート風のコンテキストでは、通常の場合と同様、
C<"$"> とメタ文字でない C<"@"> に注意する必要があります。
これらは変数を補完することができますが、それは
意図したものである場合とない場合があります。
=begin original
These rules were designed for compactness of expression, rather than
legibility and maintainability. The L</E<sol>x and E<sol>xx> pattern
modifiers allow you to insert white space to improve readability. And
use of S<C<L<re 'strict'|re/'strict' mode>>> adds extra checking to
catch some typos that might silently compile into something unintended.
=end original
これらの規則は、読みやすさや保守性ではなく、表現のコンパクトさを
考慮して設計されています。
L</E<sol>x and E<sol>xx> パターン修飾子を使用すると、読みやすさを
向上させるために空白を挿入できます。
また、S<C<L<re 'strict'|re/'strict' mode>>> を使用すると、
意図しないものに暗黙的にコンパイルされる可能性のあるタイプミスを
捕捉するための追加チェックが追加されます。
=begin original
By default, the C<"^"> character is guaranteed to match only the
beginning of the string, the C<"$"> character only the end (or before the
newline at the end), and Perl does certain optimizations with the
assumption that the string contains only one line. Embedded newlines
will not be matched by C<"^"> or C<"$">. You may, however, wish to treat a
string as a multi-line buffer, such that the C<"^"> will match after any
newline within the string (except if the newline is the last character in
the string), and C<"$"> will match before any newline. At the
cost of a little more overhead, you can do this by using the
L</C<E<sol>m>> modifier on the pattern match operator. (Older programs
did this by setting C<$*>, but this option was removed in perl 5.10.)
X<^> X<$> X</m>
=end original
デフォルトでは、文字 C<"^"> は文字列の先頭にのみ、そして文字 C<"$"> は
末尾(または末尾の改行の前)にのみマッチングすることを保証し、そして Perl は
文字列が 1 行のみを含んでいるという仮定でいくつかの最適化を行います。
埋め込まれている改行文字は C<"^"> や C<"$"> とはマッチングしません。
しかし文字列には複数行が格納されていて、C<"^"> は任意の改行の後(但し
改行文字が文字列の最後の文字だった場合は除く)、そして C<"$"> は任意の改行の
前でマッチングさせたいこともあるでしょう。
小さなオーバーヘッドはありますが、これはパターンマッチングで
L</C<E<sol>m>> 修飾子を使うことで行うことができます。
(古いプログラムでは C<$*> を設定することでこれを行っていましたが
これは perl 5.10 では削除されています。)
X<^> X<$> X</m>
=begin original
To simplify multi-line substitutions, the C<"."> character never matches a
newline unless you use the L<C<E<sol>s>|/s> modifier, which in effect tells
Perl to pretend the string is a single line--even if it isn't.
X<.> X</s>
=end original
複数行での利用を簡単にするために、文字 C<"."> は L<C<E<sol>s>|/s> 修飾子を
使って Perl に文字列を 1 行として処理すると Perl に伝えない限り、
改行にはマッチングしません。
X<.> X</s>
=head2 Modifiers
(修飾子)
=head3 Overview
(概要)
=begin original
The default behavior for matching can be changed, using various
modifiers. Modifiers that relate to the interpretation of the pattern
are listed just below. Modifiers that alter the way a pattern is used
by Perl are detailed in L<perlop/"Regexp Quote-Like Operators"> and
L<perlop/"Gory details of parsing quoted constructs">.
=end original
マッチングのデフォルトの振る舞いは、様々な修飾子 (modifier) で
変更できます。
パターンの解釈に関連する修飾子は、直後に一覧にしています。
Perl がパターンを使う方法を変更する
修飾子は L<perlop/"Regexp Quote-Like Operators">
及び L<perlop/"Gory details of parsing quoted constructs"> に
説明されています。
=over 4
=item B<C<m>>
X</m> X<regex, multiline> X<regexp, multiline> X<regular expression, multiline>
=begin original
Treat the string being matched against as multiple lines. That is, change C<"^"> and C<"$"> from matching
the start of the string's first line and the end of its last line to
matching the start and end of each line within the string.
=end original
文字列を複数行としてマッチングするように扱います。
つまり、C<"^"> 及び C<"$"> は文字列の最初の行の先頭および最後の行の末尾に対する
マッチングから、文字列中の各行の銭湯と末尾に対するマッチングへと
変更されます。
=item B<C<s>>
X</s> X<regex, single-line> X<regexp, single-line>
X<regular expression, single-line>
=begin original
Treat the string as single line. That is, change C<"."> to match any character
whatsoever, even a newline, which normally it would not match.
=end original
文字列を 1 行として扱います。
つまり、C<"."> は任意の 1 文字、通常はマッチングしない改行でさえも
マッチングするように変更されます。
=begin original
Used together, as C</ms>, they let the C<"."> match any character whatsoever,
while still allowing C<"^"> and C<"$"> to match, respectively, just after
and just before newlines within the string.
=end original
C</ms> として共に使うと、C<"^"> 及び C<"$"> はそれぞれ
文字列中の改行の直前及び直後のマッチングでありつつ、C<"."> は任意の文字に
マッチングするようになります。
=item B<C<i>>
X</i> X<regex, case-insensitive> X<regexp, case-insensitive>
X<regular expression, case-insensitive>
=begin original
Do case-insensitive pattern matching. For example, "A" will match "a"
under C</i>.
=end original
大文字小文字を区別しないパターンマッチングを行います。
例えば、C</i> の下では "A" は "a" にマッチングします。
=begin original
If locale matching rules are in effect, the case map is taken from the
current
locale for code points less than 255, and from Unicode rules for larger
code points. However, matches that would cross the Unicode
rules/non-Unicode rules boundary (ords 255/256) will not succeed, unless
the locale is a UTF-8 one. See L<perllocale>.
=end original
ロケールマッチングルールが有効になっている場合、符号位置 255 以下の場合は
現在のロケールから取られ、より大きい符号位置では Unicode ルールから
取られます。
しかし、Unicode ルールと非 Unicode ルールの境界(番号255/256) を
またぐマッチングは、ロケールが UTF-8 のものでない限り成功しません。
L<perllocale> を参照してください。
=begin original
There are a number of Unicode characters that match a sequence of
multiple characters under C</i>. For example,
C<LATIN SMALL LIGATURE FI> should match the sequence C<fi>. Perl is not
currently able to do this when the multiple characters are in the pattern and
are split between groupings, or when one or more are quantified. Thus
=end original
C</i> の基で複数の文字の並びにマッチングする Unicode 文字はたくさんあります。
例えば、C<LATIN SMALL LIGATURE FI> は並び C<fi> にマッチングするべきです。
複数の文字がパターン中にあってグループ化で分割されている場合、または
どれかの文字に量指定子が付いている場合、Perl は今のところこれを行えません。
従って
=begin original
"\N{LATIN SMALL LIGATURE FI}" =~ /fi/i; # Matches
"\N{LATIN SMALL LIGATURE FI}" =~ /[fi][fi]/i; # Doesn't match!
"\N{LATIN SMALL LIGATURE FI}" =~ /fi*/i; # Doesn't match!
=end original
"\N{LATIN SMALL LIGATURE FI}" =~ /fi/i; # マッチング
"\N{LATIN SMALL LIGATURE FI}" =~ /[fi][fi]/i; # マッチングしない!
"\N{LATIN SMALL LIGATURE FI}" =~ /fi*/i; # マッチングしない!
=begin original
# The below doesn't match, and it isn't clear what $1 and $2 would
# be even if it did!!
"\N{LATIN SMALL LIGATURE FI}" =~ /(f)(i)/i; # Doesn't match!
=end original
# 次のものはマッチングしないし、もししたとしても $1 と $2 が何になるか
# はっきりしない!!
"\N{LATIN SMALL LIGATURE FI}" =~ /(f)(i)/i; # マッチングしない!
=begin original
Perl doesn't match multiple characters in a bracketed
character class unless the character that maps to them is explicitly
mentioned, and it doesn't match them at all if the character class is
inverted, which otherwise could be highly confusing. See
L<perlrecharclass/Bracketed Character Classes>, and
L<perlrecharclass/Negation>.
=end original
Perl は、明示的にマッピングについて言及されていない限り、
大かっこ文字クラスの複数の文字にはマッチングしません;
そして文字クラスが否定された場合はそれらには全くマッチングしません;
さもなければとても混乱することがあるからです。
L<perlrecharclass/Bracketed Character Classes> と
L<perlrecharclass/Negation> を参照して下さい。
=item B<C<x>> and B<C<xx>>
X</x>
(B<C<x>> と B<C<xx>>)
=begin original
Extend your pattern's legibility by permitting whitespace and comments.
Details in L</E<sol>x and E<sol>xx>
=end original
空白やコメントを許可してパターンを読みやすくするように拡張します。
詳細は L</E<sol>x and E<sol>xx> にあります。
=item B<C<p>>
X</p> X<regex, preserve> X<regexp, preserve>
=begin original
Preserve the string matched such that C<${^PREMATCH}>, C<${^MATCH}>, and
C<${^POSTMATCH}> are available for use after matching.
=end original
C<${^PREMATCH}>, C<${^MATCH}>, C<${^POSTMATCH}> といったマッチングされた
文字列をマッチングの後も使えるように維持します。
=begin original
In Perl 5.20 and higher this is ignored. Due to a new copy-on-write
mechanism, C<${^PREMATCH}>, C<${^MATCH}>, and C<${^POSTMATCH}> will be available
after the match regardless of the modifier.
=end original
Perl 5.20 以降ではこれは無視されます。
新しいコピーオンライト機構により、
C<${^PREMATCH}>, C<${^MATCH}>, and C<${^POSTMATCH}> はこの修飾子に関わらず
マッチングの後も利用可能です。
=item B<C<a>>, B<C<d>>, B<C<l>>, and B<C<u>>
X</a> X</d> X</l> X</u>
(B<C<a>>, B<C<d>>, B<C<l>>, B<C<u>>)
=begin original
These modifiers, all new in 5.14, affect which character-set rules
(Unicode, I<etc>.) are used, as described below in
L</Character set modifiers>.
=end original
5.14 から導入されたこれらの新しい修飾子は、どの文字集合規則
(Unicode など) が使われるかに影響を与えます;
L</Character set modifiers> で後述します。
=item B<C<n>>
X</n> X<regex, non-capture> X<regexp, non-capture>
X<regular expression, non-capture>
=begin original