/
perlre.pod
executable file
·5715 lines (4047 loc) · 181 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 quick-start
introduction is available in L<perlrequick>, and a longer tutorial
introduction is available in L<perlretut>.
=end original
もしこれまでに正規表現を使ったことがないのであれば、
L<perlrequick> にクイックスタートが、L<perlretut> に
長めのチュートリアルがあります。
=begin original
For reference on how regular expressions are used in matching
operations, 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<perlop/"Regexp Quote-Like Operators"> にある
C<m//>, C<s///>, C<qr//>, C<??> の説明を参照して下さい。
=head2 Modifiers
(修飾子)
=begin original
Matching operations can have various modifiers. Modifiers
that relate to the interpretation of the regular expression inside
are listed below. Modifiers that alter the way a regular expression
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 m
X</m> X<regex, multiline> X<regexp, multiline> X<regular expression, multiline>
=begin original
Treat string as multiple lines. That is, change "^" and "$" from matching
the start or end of the string to matching the start or end of any
line anywhere within the string.
=end original
文字列を複数行として扱います。
つまり、"^" 及び "$" は文字列の最初と最後に対するマッチングから、
文字列中の各行の先頭と末尾に対するマッチングへと変更されます。
=item s
X</s> X<regex, single-line> X<regexp, single-line>
X<regular expression, single-line>
=begin original
Treat string as single line. That is, change "." to match any character
whatsoever, even a newline, which normally it would not match.
=end original
文字列を 1 行として扱います。
つまり、"." は任意の 1 文字、通常はマッチングしない改行でさえも
マッチングするように変更されます。
=begin original
Used together, as C</ms>, they let the "." match any character whatsoever,
while still allowing "^" and "$" to match, respectively, just after
and just before newlines within the string.
=end original
C</ms> として共に使うと、"^" 及び "$" はそれぞれ
文字列中の改行の直前及び直後のマッチングでありつつ、"." は任意の文字に
マッチングするようになります。
=item i
X</i> X<regex, case-insensitive> X<regexp, case-insensitive>
X<regular expression, case-insensitive>
=begin original
Do case-insensitive pattern matching.
=end original
大文字小文字を区別しないパターンマッチングを行います。
=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. See
L<perllocale>.
=end original
ロケールマッチングルールが有効になっている場合、符号位置 255 以下の場合は
現在のロケールから取られ、より大きい符号位置では Unicode ルールから
取られます。
しかし、Unicode ルールと非 Unicode ルールの境界(番号255/256) を
またぐマッチングは成功しません。
L<perllocale> を参照してください。
=begin original
There are a number of Unicode characters that match 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 an inverted bracketed
character class, which otherwise could be highly confusing. See
L<perlrecharclass/Negation>.
=end original
Perl は、否定大かっこ文字クラスの複数の文字にはマッチングしません;
さもなければとても混乱することがあるからです。
L<perlrecharclass/Negation> を参照して下さい。
=begin original
Also, Perl matching doesn't fully conform to the current Unicode C</i>
recommendations, which ask that the matching be made upon the NFD
(Normalization Form Decomposed) of the text. However, Unicode is
in the process of reconsidering and revising their recommendations.
=end original
また、Perl のマッチングは現在の Unicode の C</i> 勧告に完全には
準拠していません; テキストの NFD (Normalization Form Decomposed) に対しての
マッチングに関する部分です。
しかし、Unicode は勧告の再検討と改訂の作業中です。
=item x
X</x>
=begin original
Extend your pattern's legibility by permitting whitespace and comments.
Details in L</"/x">
=end original
空白やコメントを許可してパターンを読みやすくするように拡張します。
詳細は L</"/x"> にあります。
=item p
X</p> X<regex, preserve> X<regexp, preserve>
=begin original
Preserve the string matched such that ${^PREMATCH}, ${^MATCH}, and
${^POSTMATCH} are available for use after matching.
=end original
${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} といったマッチングされた文字列を
マッチングの後も使えるように維持します。
=item g and c
X</g> X</c>
(g と c)
=begin original
Global matching, and keep the Current position after failed matching.
Unlike i, m, s and x, these two flags affect the way the regex is used
rather than the regex itself. See
L<perlretut/"Using regular expressions in Perl"> for further explanation
of the g and c modifiers.
=end original
グローバル(Global)なマッチング、及びマッチング失敗後の現在位置の保持。
i, m, s, x とは違い、この二つのフラグは
正規表現そのものではなく正規表現の使われ方に作用します。
g 及び c 修飾子の詳細な説明は
L<perlretut/"Using regular expressions in Perl"> を参照してください。
=item a, d, l and u
X</a> X</d> X</l> X</u>
(a, d, l, u)
=begin original
These modifiers, new in 5.14, affect which character-set semantics
(Unicode, ASCII, etc.) are used, as described below in
L</Character set modifiers>.
=end original
5.14 から導入されたこれらの新しい修飾子は、どの文字集合意味論 (Unicode,
ASCII など) が使われるかに影響を与えます; L</Character set modifiers> で
後述します。
=back
=begin original
These are usually written as "the C</x> modifier", even though the delimiter
in question might not really be a slash. The modifiers C</imsxadlup>
may also be embedded within the regular expression itself using
the C<(?...)> construct, see L</Extended Patterns> below.
=end original
これらは通常「C</x> 修飾子」のように記述され、これは区切りが実際には
スラッシュでなくてもそう記述されます。
また、C</imsxadlup> 修飾子は C<(?...)> 構築子を使って正規表現内に
埋め込まれることもあります; 後述する L</Extended Patterns> を
参照してください。
=begin original
The C</x>, C</l>, C</u>, C</a> and C</d> modifiers need a little more
explanation.
=end original
C</x>, C</l>, C</u>, C</a>, C</d> 修飾子はもう少し説明が必要です。
=head3 /x
=begin original
C</x> tells
the regular expression parser to ignore most whitespace that is neither
backslashed nor within a character class. You can use this to break up
your regular expression into (slightly) more readable parts. The C<#>
character is also treated as a metacharacter introducing a comment,
just as in ordinary Perl code. This also means that if you want real
whitespace or C<#> characters in the pattern (outside a character
class, where they are unaffected by C</x>), then you'll either have to
escape them (using backslashes or C<\Q...\E>) or encode them using octal,
hex, or C<\N{}> escapes. Taken together, these features go a long way towards
making Perl's regular expressions more readable. Note that you have to
be careful not to include the pattern delimiter in the comment--perl has
no way of knowing you did not intend to close the pattern early. See
the C-comment deletion code in L<perlop>. Also note that anything inside
a C<\Q...\E> stays unaffected by C</x>. And note that C</x> doesn't affect
space interpretation within a single multi-character construct. For
example in C<\x{...}>, regardless of the C</x> modifier, there can be no
spaces. Same for a L<quantifier|/Quantifiers> such as C<{3}> or
C<{5,}>. Similarly, C<(?:...)> can't have a space between the C<?> and C<:>,
but can between the C<(> and C<?>. Within any delimiters for such a
construct, allowed spaces are not affected by C</x>, and depend on the
construct. For example, C<\x{...}> can't have spaces because hexadecimal
numbers don't have spaces in them. But, Unicode properties can have spaces, so
in C<\p{...}> there can be spaces that follow the Unicode rules, for which see
L<perluniprops/Properties accessible through \p{} and \P{}>.
X</x>
=end original
C</x> は、バックスラッシュでエスケープされたり文字クラスの中だったりしない
ほとんどの空白を無視するように正規表現パーサに伝えます。
これは正規表現を(少し)読みやすく部分に分割するために使えます。
また、C<#> は通常の Perl コードと同様コメントを開始するメタ文字として
扱われます。
これはまた、(C</x> の影響を受けない文字クラス内以外で)パターン中に本当の空白や
C<#> 文字を使いたい場合は、(逆スラッシュや C<\Q...\E> を使って)
エスケープするか、8 進数、16 進数、C<\N{}> エスケープのいずれかで
エンコードする必要があると言うことです。
まとめると、これらの機能は Perl の正規表現をより読みやすくするために
大きく役立ちます。
コメントにパターン区切りを含まないように注意する必要があります--perl は
早くパターンを終了したいわけではないと言うことを知る手段がありません。
L<perlop> の C 型式のコメントを削除するコードを参照してください。
また、C<\Q...\E> の内側のものは C</x> の影響を受けないことにも
注意してください。
例えば、C<\x{...}> の内部では、C</x> 修飾子に関わらず、スペースを
含むことはできません。
C<{3}> や C<{5,}> のような L<量指定子|/Quantifiers> も同様です。
また、C<(?:...)> も C<?> と C<:> の間にスペースを含むことはできませんが、
C<(> と C<?> の間には含むことができます。
このような構文の区切り文字の中では、スペースが許されるかどうかは
C</x> に影響されず、構文自身に影響されます。
例えば、16 進数はスペースを含むことができないので C<\x{...}> はスペースを
含むことができません。
しかし、Unicode 特性はスペースを含むことができるので、
C<\p{...}> は Unicode の規則に従ってスペースを含むことができます;
L<perluniprops/Properties accessible through \p{} and \P{}> を
参照してください。
X</x>
=head3 Character set modifiers
(文字集合修飾子)
=begin original
C</d>, C</u>, C</a>, and C</l>, available starting in 5.14, are called
the character set modifiers; they affect the character set semantics
used for the regular expression.
=end original
5.14 から利用可能な C</d>, C</u>, C</a>, C</l> は文字集合修飾子と呼ばれます;
これらは正規表現で使われる文字集合の意味論に影響を与えます。
=begin original
At any given time, exactly one of these modifiers is in effect. Once
compiled, the behavior doesn't change regardless of what rules are in
effect when the regular expression is executed. And if a regular
expression is interpolated into a larger one, the original's rules
continue to apply to it, and only it.
=end original
任意のある瞬間において、これらの修飾子の内正確に一つだけが有効になります。
一旦コンパイルされると、正規表現が実行されるときにどの規則が有効化に関わらず、
振る舞いは変更されません。
そして正規表現がより大きな正規表現に展開された場合、元の規則だけが
適用され続けます。
=begin original
Note that the modifiers affect only pattern matching, and do not extend
to any replacement done. For example,
=end original
修飾子はパターンマッチングに対してのみ影響を与え、置換には拡張されないことに
注意してください。
例えば:
s/foo/\Ubar/l
=begin original
will uppercase "bar", but the C</l> does not affect how the C<\U>
operates. If C<use locale> is in effect, the C<\U> will use locale
rules; if C<use feature 'unicode_strings'> is in effect, it will
use Unicode rules, etc.
=end original
これは大文字の "bar" にしますが、C</l> は C<\U> がどう処理を行うかに
影響を与えません。
C<use locale> が有効の場合は C<\U> はロケールの規則を使い、
C<use feature 'unicode_strings'> が有効の場合は Unicode の規則を使い、
以下同様です。
=head4 /l
=begin original
means to use the current locale's rules (see L<perllocale>) when pattern
matching. For example, C<\w> will match the "word" characters of that
locale, and C<"/i"> case-insensitive matching will match according to
the locale's case folding rules. The locale used will be the one in
effect at the time of execution of the pattern match. This may not be
the same as the compilation-time locale, and can differ from one match
to another if there is an intervening call of the
L<setlocale() function|perllocale/The setlocale function>.
=end original
これはパターンマッチングのときに現在のロケールの規則 (L<perllocale> 参照) を
使うことを意味します。
例えば、C<\w> はこのロケールの「単語」文字にマッチングし、
C<"/i"> の大文字小文字マッチングは、現在のロケールの大文字小文字畳み込み規則に
従ってマッチングします。
使われるロケールはパターンマッチングが実行される時点で有効なものです。
これはコンパイル時のロケールと同じではないかもしれませんし、
L<setlocale() 関数|perllocale/The setlocale function>
の呼び出しが間に入ると、呼び出し毎に異なることもあります。
=begin original
Perl only supports single-byte locales. This means that code points
above 255 are treated as Unicode no matter what locale is in effect.
Under Unicode rules, there are a few case-insensitive matches that cross
the 255/256 boundary. These are disallowed under C</l>. For example,
0xFF does not caselessly match the character at 0x178, C<LATIN CAPITAL
LETTER Y WITH DIAERESIS>, because 0xFF may not be C<LATIN SMALL LETTER Y
WITH DIAERESIS> in the current locale, and Perl has no way of knowing if
that character even exists in the locale, much less what code point it
is.
=end original
Perl は単一バイトロケールのみに対応します。
つまり、255 を越える符号位置は、どのロケールが有効であるかに関わらず
Unicode として扱われるということです。
Unicode の規則の基では、255/256 の境界をまたぐ大文字小文字を無視した
マッチングがいくつかあります。
これらは C</l> の基では認められません。
例えば、0xFF は 0x178,
C<LATIN CAPITAL LETTER Y WITH DIAERESIS> と大文字小文字を無視した
マッチングをしません; なぜなら 0xFF は現在のロケールでは
C<LATIN SMALL LETTER Y WITH DIAERESIS> ではないかもしれず、Perl は
このロケールでこの文字があるかどうかすら知る方法がなく、ましてや
これがどの符号位置かを知る方法もないからです。
=begin original
This modifier may be specified to be the default by C<use locale>, but
see L</Which character set modifier is in effect?>.
X</l>
=end original
この修飾子は C<use locale> によってデフォルトで設定されますが、
L</Which character set modifier is in effect?> を参照してください。
X</l>
=head4 /u
=begin original
means to use Unicode rules when pattern matching. On ASCII platforms,
this means that the code points between 128 and 255 take on their
Latin-1 (ISO-8859-1) meanings (which are the same as Unicode's), whereas
in strict ASCII their meanings are undefined. Thus the platform
effectively becomes a Unicode platform, hence, for example, C<\w> will
match any of the more than 100_000 word characters in Unicode.
=end original
これはパターンマッチングのときに Unicode の規則を使うことを意味します。
ASCII プラットフォームでは、これは符号位置 128 から 255 は
Latin-1 (ISO-8859-1) という意味になり (これは Unicode と同じです)、
一方厳密な ASCII ではこれらの意味は未定義です。
従って、事実上 Unicode プラットフォームになるプラットフォームでは、
例えば、C<\w> は Unicode の 100,000 以上の単語文字のどれにも
マッチングします。
=begin original
Unlike most locales, which are specific to a language and country pair,
Unicode classifies all the characters that are letters I<somewhere> as
C<\w>. For example, your locale might not think that C<LATIN SMALL
LETTER ETH> is a letter (unless you happen to speak Icelandic), but
Unicode does. Similarly, all the characters that are decimal digits
somewhere in the world will match C<\d>; this is hundreds, not 10,
possible matches. And some of those digits look like some of the 10
ASCII digits, but mean a different number, so a human could easily think
a number is a different quantity than it really is. For example,
C<BENGALI DIGIT FOUR> (U+09EA) looks very much like an
C<ASCII DIGIT EIGHT> (U+0038). And, C<\d+>, may match strings of digits
that are a mixture from different writing systems, creating a security
issue. L<Unicode::UCD/num()> can be used to sort this out.
=end original
特定の言語と国に固有であるほとんどのロケールと異なり、
Unicode は I<どこか> で字(letter)として扱われている全ての文字(character)を
C<\w> に分類します。
例えば、あなたのロケールは (あなたがたまたまアイスランド語を話さない限り)
C<LATIN SMALL LETTER ETH> を字として考えないかもしれません。
同様に、世界のどこかで数字である全ての文字は C<\d> にマッチングします;
これは 10 ではなく 100 のマッチングの可能性があります。
さらにこれらの数字の一部は 10 の ASCII 数字と似ていますが、
異なる数字を意味するため、人間はその数字が実際と異なる量であると
簡単に考えてしまいます。
例えば、 C<BENGALI DIGIT FOUR> (U+09EA) は C<ASCII DIGIT EIGHT> (U+0038) に
とてもよく似ています。
C<\d+> は、異なる記法から混ぜた数字の文字列にマッチングするので、
セキュリティ上の問題を作ります。
これを整理するために L<Unicode::UCD/num()> が使われます。
=begin original
Also, case-insensitive matching works on the full set of Unicode
characters. The C<KELVIN SIGN>, for example matches the letters "k" and
"K"; and C<LATIN SMALL LIGATURE FF> matches the sequence "ff", which,
if you're not prepared, might make it look like a hexadecimal constant,
presenting another potential security issue. See
L<http://unicode.org/reports/tr36> for a detailed discussion of Unicode
security issues.
=end original
また、大文字小文字を無視したマッチングは Unicode の完全な集合で動作します。
例えば C<KELVIN SIGN> は "k" と "K" にマッチングします;
C<LATIN SMALL LIGATURE FF> は、準備していなければ 16 進数定数のように
見えるかもしれない並び "ff" にマッチングし、もう一つの潜在的な
セキュリティ問題になります。
Unicode のセキュリティ問題に関する詳細な議論については
L<http://unicode.org/reports/tr36> を参照してください。
=begin original
On the EBCDIC platforms that Perl handles, the native character set is
equivalent to Latin-1. Thus this modifier changes behavior only when
the C<"/i"> modifier is also specified, and it turns out it affects only
two characters, giving them full Unicode semantics: the C<MICRO SIGN>
will match the Greek capital and small letters C<MU>, otherwise not; and
the C<LATIN CAPITAL LETTER SHARP S> will match any of C<SS>, C<Ss>,
C<sS>, and C<ss>, otherwise not.
=end original
Perl が扱える EBCDIC プラットフォームでは、ネイティブな文字集合は
Latin-1 と等価です。
従ってこの修飾子は、C<"/i"> 修飾子も指定されたときにのみ
振る舞いを変え、結果として二つの文字にだけ影響を与え、
完全な Unicode の意味論を与えます:
C<MICRO SIGN> はギリシャ語の大文字と小文字の C<MU> にマッチングし、
それ以外はマッチングしません;
また C<LATIN CAPITAL LETTER SHARP S> は C<SS>, C<Ss>,
C<sS>, C<ss> のいずれかにはマッチングし、それ以外にはマッチングしません。
=begin original
This modifier may be specified to be the default by C<use feature
'unicode_strings>, but see
L</Which character set modifier is in effect?>.
X</u>
=end original
この修飾子は C<use feature 'unicode_strings> によってデフォルトに
設定されますが、L</Which character set modifier is in effect?> を
参照してください。
X</u>
=head4 /a
=begin original
is the same as C</u>, except that C<\d>, C<\s>, C<\w>, and the
Posix character classes are restricted to matching in the ASCII range
only. That is, with this modifier, C<\d> always means precisely the
digits C<"0"> to C<"9">; C<\s> means the five characters C<[ \f\n\r\t]>;
C<\w> means the 63 characters C<[A-Za-z0-9_]>; and likewise, all the
Posix classes such as C<[[:print:]]> match only the appropriate
ASCII-range characters.
=end original
これは C</u> と同様ですが、C<\d>, C<\s>, C<\w>, Posix 文字クラスは
ASCII の範囲のみにマッチングするように制限されます。
つまり、この修飾子を使うと、C<\d> は常に正確に数字 C<"0"> から C<"9"> を
意味します;
C<\s> は C<[ \f\n\r\t]> の 5 文字を意味します;
C<\w> は C<[A-Za-z0-9_]> の 63 文字を意味します;
同様に、C<[[:print:]]> のような全ての Posix クラスは
適切な ASCII の範囲の文字にのみマッチングします。
=begin original
This modifier is useful for people who only incidentally use Unicode.
With it, one can write C<\d> with confidence that it will only match
ASCII characters, and should the need arise to match beyond ASCII, you
can use C<\p{Digit}>, or C<\p{Word}> for C<\w>. There are similar
C<\p{...}> constructs that can match white space and Posix classes
beyond ASCII. See L<perlrecharclass/POSIX Character Classes>.
=end original
この修飾子は、偶然 Unicode を使っている人々にとっては有用です。
これを使うと、ASCII 文字だけにマッチングすることに自信を持って
書くことができ、ASCII を超えてマッチングする必要が発生したときには、
C<\w> として C<\p{Digit}> や C<\p{Word}> が使えます。
ASCII を超えたスペースと Posix クラスにマッチングする
似たような C<\p{...}> 構文があります。
L<perlrecharclass/POSIX Character Classes> を参照してください。
=begin original
As you would expect, this modifier causes, for example, C<\D> to mean
the same thing as C<[^0-9]>; in fact, all non-ASCII characters match
C<\D>, C<\S>, and C<\W>. C<\b> still means to match at the boundary
between C<\w> and C<\W>, using the C</a> definitions of them (similarly
for C<\B>).
=end original
予想できるとおり、 この修飾子は、例えば、C<\D> を C<[^0-9]> と
同じことにします;
実際、全ての非 ASCII 文字は C<\D>, C<\S>, C<\W> にマッチングします。
C<\b> はまだ C<\w> と C<\W> の境界にマッチングします;
これらのために (C<\B> と同様) C</a> の定義を使います。
=begin original
Otherwise, C</a> behaves like the C</u> modifier, in that
case-insensitive matching uses Unicode semantics; for example, "k" will
match the Unicode C<\N{KELVIN SIGN}> under C</i> matching, and code
points in the Latin1 range, above ASCII will have Unicode rules when it
comes to case-insensitive matching.
=end original
さもなければ、C</a> は C</u> 修飾子のように振る舞います;
大文字小文字を無視したマッチングには Unicode の意味論を使います;
例えば、"k" は C</i> の基では C<\N{KELVIN SIGN}> にマッチングし、
ASCII の範囲を超える Latin1 の範囲の符号位置は、大文字小文字を無視した
マッチングで使われる場合は Unicode の規則を使います。
=begin original
To forbid ASCII/non-ASCII matches (like "k" with C<\N{KELVIN SIGN}>),
specify the "a" twice, for example C</aai> or C</aia>
=end original
("k" と C<\N{KELVIN SIGN}> のような) ASCII/非-ASCII マッチングを禁止するには、
C</aai> や C</aia> のように "a" を 2 回指定します。
=begin original
To reiterate, this modifier provides protection for applications that
don't wish to be exposed to all of Unicode. Specifying it twice
gives added protection.
=end original
繰り返すと、この修飾子は全ての Unicode に対して曝されることを望んでいない
アプリケーションに対する保護を提供します。
2 回指定することで追加の保護を提供します。
=begin original
This modifier may be specified to be the default by C<use re '/a'>
or C<use re '/aa'>, but see
L</Which character set modifier is in effect?>.
X</a>
X</aa>
=end original
この修飾子は C<use re '/a'> または C<use re '/aa'> でデフォルトに
設定されますが、L</Which character set modifier is in effect?> を
参照してください。
X</a>
X</aa>
=head4 /d
=begin original
This modifier means to use the "Default" native rules of the platform
except when there is cause to use Unicode rules instead, as follows:
=end original
この修飾子は、以下のように Unicode の規則が使われる場合を除いて、
プラットフォームの「デフォルトの」(Default) ネイティブな規則を使うことを
意味します:
=over 4
=item 1
=begin original
the target string is encoded in UTF-8; or
=end original
ターゲット文字列が UTF-8 でエンコードされている; または
=item 2
=begin original
the pattern is encoded in UTF-8; or
=end original
パターンが UTF-8 でエンコードされている; または
=item 3
=begin original
the pattern explicitly mentions a code point that is above 255 (say by
C<\x{100}>); or
=end original
パターンが、(C<\x{100}> のような形で)255 を超える符号位置に明示的に
言及している; または
=item 4
=begin original
the pattern uses a Unicode name (C<\N{...}>); or
=end original
パターンが Unicode 名 (C<\N{...}>) を使っている; または
=item 5
=begin original
the pattern uses a Unicode property (C<\p{...}>)
=end original
パターンが Unicode 特性 (C<\p{...}>) を使っている
=back
=begin original
Another mnemonic for this modifier is "Depends", as the rules actually
used depend on various things, and as a result you can get unexpected
results. See L<perlunicode/The "Unicode Bug">.
=end original
この修飾子のもう一つの記憶法は「依存」(Depends)です; 規則は実際には
様々なことに依存していること、また結果として予想外の
結果になるかもしれないからです。
L<perlunicode/The "Unicode Bug"> を参照してください。
=begin original
On ASCII platforms, the native rules are ASCII, and on EBCDIC platforms
(at least the ones that Perl handles), they are Latin-1.
=end original
ASCII プラットフォームでは、ネイティブな規則は ASCII で、(少なくとも
Perl が扱う) EBCDIC プラットフォームでは、これは Latin-1 です。
=begin original
Here are some examples of how that works on an ASCII platform:
=end original
以下は ASCII プラットフォームでどのように動作するかの例です:
$str = "\xDF"; # $str is not in UTF-8 format.
$str =~ /^\w/; # No match, as $str isn't in UTF-8 format.
$str .= "\x{0e0b}"; # Now $str is in UTF-8 format.
$str =~ /^\w/; # Match! $str is now in UTF-8 format.
chop $str;
$str =~ /^\w/; # Still a match! $str remains in UTF-8 format.
=head4 Which character set modifier is in effect?
(どの文字集合修飾子が有効?)
=begin original
Which of these modifiers is in effect at any given point in a regular
expression depends on a fairly complex set of interactions. As
explained below in L</Extended Patterns> it is possible to explicitly
specify modifiers that apply only to portions of a regular expression.
The innermost always has priority over any outer ones, and one applying
to the whole expression has priority over any of the default settings that are
described in the remainder of this section.
=end original
ある正規表現のあるポイントでどの修飾子が有効かは、かなり複雑な相互作用に
依存します。
L</Extended Patterns> で後述するとおり、正規表現の一部にだけ
適用する修飾子を明示的に指定することが可能です。
一番内側のものは常により外側のものより優先され、式全体に適用されるものは
この節の残りで記述されるデフォルト設定より優先されます。
=begin original
The C<L<use re 'E<sol>foo'|re/'E<sol>flags' mode">> pragma can be used to set
default modifiers (including these) for regular expressions compiled
within its scope. This pragma has precedence over the other pragmas
listed below that change the defaults.
=end original
C<L<use re 'E<sol>foo'|re/'E<sol>flags' mode">> プラグマは、
このスコープ内でコンパイルされる正規表現に対して(これらを含む)
デフォルトの修飾子を設定するのに使えます。
このプラグマは、デフォルトを変更する後述するその他のプラグマに優先します。
=begin original
Otherwise, C<L<use locale|perllocale>> sets the default modifier to C</l>;
and C<L<use feature 'unicode_strings|feature>> or
C<L<use 5.012|perlfunc/use VERSION>> (or higher) set the default to
C</u> when not in the same scope as either C<L<use locale|perllocale>>
or C<L<use bytes|bytes>>. Unlike the mechanisms mentioned above, these
affect operations besides regular expressions pattern matching, and so
give more consistent results with other operators, including using
C<\U>, C<\l>, etc. in substitution replacements.
=end original
さもなければ、C<L<use locale|perllocale>> はデフォルト修飾子を C</l> に
設定します; そして、C<L<use feature 'unicode_strings|feature>> か
C<L<use 5.012|perlfunc/use VERSION>> (またはそれ以上) は、
同じスコープに C<L<use locale|perllocale>> や C<L<use bytes|bytes>> が
なければ、デフォルトを C</u> に設定します。
前述した機構と異なり、これらは正規表現パターンマッチング以外の操作に
影響するので、置換での C<\U>, C<\l> を使うことを含むその他の操作と
より一貫性のある結果になります。
=begin original
If none of the above apply, for backwards compatibility reasons, the
C</d> modifier is the one in effect by default. As this can lead to
unexpected results, it is best to specify which other rule set should be
used.
=end original
前述のどれも適用されない場合、後方互換性のために、
C</d> 修飾子がデフォルトで有効になります。
これは想定外の結果になることがあるので、
その他の規則集合が使われるように指定するのが最良です。
=head4 Character set modifier behavior prior to Perl 5.14
(Perl 5.14 より前の文字集合修飾子の振る舞い)
=begin original
Prior to 5.14, there were no explicit modifiers, but C</l> was implied
for regexes compiled within the scope of C<use locale>, and C</d> was
implied otherwise. However, interpolating a regex into a larger regex
would ignore the original compilation in favor of whatever was in effect
at the time of the second compilation. There were a number of
inconsistencies (bugs) with the C</d> modifier, where Unicode rules
would be used when inappropriate, and vice versa. C<\p{}> did not imply
Unicode rules, and neither did all occurrences of C<\N{}>, until 5.12.
=end original
5.14 より前では、明示的な修飾子はありませんが、
C<use locale> のスコープ内でコンパイルされた正規表現に関しては
C</l> が仮定され、さもなければ C</d> が仮定されます。
しかし、ある正規表現をより大きな正規表現に展開した場合、元のコンパイル時の
状況は、2 回目のコンパイル時点で有効なもので上書きされます。
C</d> 演算子には、不適切なときに Unicode 規則が使われる、あるいはその逆の
多くの非一貫性(バグ)があります。
C<\p{}> および C<\N{}> は 5.12 まで Unicode 規則を仮定していません。
=head2 Regular Expressions
(正規表現)
=head3 Metacharacters
(メタ文字)
=begin original
The patterns used in Perl pattern matching evolved from those supplied in
the Version 8 regex routines. (The routines are derived
(distantly) from Henry Spencer's freely redistributable reimplementation
of the V8 routines.) See L<Version 8 Regular Expressions> for
details.
=end original
Perl のパターンマッチングで使われるパターンは Version 8 正規表現ルーチンで
提供されているものからの派生です。
(このルーチンは Henry Spencer の自由に再配布可能な V8 ルーチンの再実装から
(遠く)派生しています)。
詳細は L<Version 8 Regular Expressions> を参照してください。
=begin original
In particular the following metacharacters have their standard I<egrep>-ish
meanings:
X<metacharacter>
X<\> X<^> X<.> X<$> X<|> X<(> X<()> X<[> X<[]>
=end original
特に以下のメタ文字は標準の I<egrep> 風の意味を持っています:
X<metacharacter>
X<\> X<^> X<.> X<$> X<|> X<(> X<()> X<[> X<[]>
=begin original
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Bracketed Character class
=end original
\ 次のメタ文字をエスケープ
^ 行の先頭にマッチング
. 任意の文字にマッチング(但し改行は除く)
$ 行の終端にマッチング(または終端の改行の前)
| 代替
() グループ化
[] 文字クラス
=begin original
By default, the "^" character is guaranteed to match only the
beginning of the string, the "$" 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 "^" or "$". You may, however, wish to treat a
string as a multi-line buffer, such that the "^" will match after any
newline within the string (except if the newline is the last character in
the string), and "$" will match before any newline. At the
cost of a little more overhead, you can do this by using the /m modifier
on the pattern match operator. (Older programs did this by setting C<$*>,
but this option was removed in perl 5.9.)
X<^> X<$> X</m>
=end original
デフォルトでは、文字 "^" は文字列の先頭にのみ、そして文字 "$" は
末尾(または末尾の改行の前)にのみマッチングすることを保証し、そして Perl は
文字列が 1 行のみを含んでいるという仮定でいくつかの最適化を行います。
埋め込まれている改行文字は "^" や "$" とはマッチングしません。
しかし文字列には複数行が格納されていて、"^" は任意の改行の後(但し
改行文字が文字列の最後の文字だった場合は除く)、そして "$" は任意の改行の前で
マッチングさせたいこともあるでしょう。
小さなオーバーヘッドはありますが、これはパターンマッチングで /m 修飾子を
使うことで行うことができます。
(古いプログラムでは C<$*> を設定することでこれを行っていましたが