/
perlre.pod
6104 lines (4321 loc) · 193 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 line only at the left and right ends of the string to
matching them 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 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 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, all new in 5.14, affect which character-set semantics
(Unicode, etc.) are used, as described below in
L</Character set modifiers>.
=end original
5.14 から導入されたこれらの新しい修飾子は、どの文字集合意味論
(Unicode など) が使われるかに影響を与えます;
L</Character set modifiers> で後述します。
=back
=begin original
Regular expression modifiers are usually written in documentation
as e.g., "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> を
参照してください。
=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<(>,
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</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
The C</d>, C</u>, and C</l> modifiers are not likely to be of much use
to you, and so you need not worry about them very much. They exist for
Perl's internal use, so that complex regular expression data structures
can be automatically serialized and later exactly reconstituted,
including all their nuances. But, since Perl can't keep a secret, and
there may be rare instances where they are useful, they are documented
here.
=end original
C</d>, C</u>, C</l> 修飾子はよく使うことはないだろうものなので、
これらについてあまり心配する必要はありません。
これらは Perl の内部仕様のために存在しているので、
複雑な正規表現データ構造は自動的に直列化されて、その後全てのニュアンスを
含めて正確に再構成されます。
=begin original
The C</a> modifier, on the other hand, may be useful. Its purpose is to
allow code that is to work mostly on ASCII data to not have to concern
itself with Unicode.
=end original
一方、C</a> 修飾子は有用かもしれません。
この目的は、Unicode に関して考慮する必要がないように、コードを
ほとんど ASCII データとして動作するようにすることです。
=begin original
Briefly, C</l> sets the character set to that of whatever B<L>ocale is in
effect at the time of the execution of the pattern match.
=end original
簡単に言うと、C</l> は、文字集合をパターンマッチングの実行時に有効な
ロケール(B<L>ocale)に設定します。
=begin original
C</u> sets the character set to B<U>nicode.
=end original
C</u> は文字集合を B<U>nicode に設定します。
=begin original
C</a> also sets the character set to Unicode, BUT adds several
restrictions for B<A>SCII-safe matching.
=end original
C</a> も文字コードを Unicode に設定しますが、
B<A>SCII セーフなマッチングのためにいくつかの制限を加えます。
=begin original
C</d> is the old, problematic, pre-5.14 B<D>efault character set
behavior. Its only use is to force that old behavior.
=end original
C</d> は古くて問題のある、5.14 以前のデフォルト(B<D>efault)文字集合の
振る舞いです。
これは古い振る舞いを強制するためだけに使います。
=begin original
At any given time, exactly one of these modifiers is in effect. Their
existence allows Perl to keep the originally compiled behavior of a
regular expression, regardless of what rules are in effect when it is
actually executed. And if it is interpolated into a larger regex, the
original's rules continue to apply to it, and only it.
=end original
任意のある瞬間において、これらの修飾子の内正確に一つだけが有効になります。
これにより、
それが実際に実行されるときにどの規則が有効かに関わらず、
Perl が元々コンパイルされた正規表現の振る舞いを保存できるようにします。
そしてそれがより大きな正規表現に展開された場合、元の規則だけが
適用され続けます。
=begin original
The C</l> and C</u> modifiers are automatically selected for
regular expressions compiled within the scope of various pragmas,
and we recommend that in general, you use those pragmas instead of
specifying these modifiers explicitly. For one thing, the modifiers
affect only pattern matching, and do not extend to even any replacement
done, whereas using the pragmas give consistent results for all
appropriate operations within their scopes. For example,
=end original
C</l> と C</u> の修飾子は、様々なプラグマのスコープ内でコンパイルされた
正規表現で自動的に選択されます;
一般的にはこれらの修飾子を明示的に使うのではなく、これらのプラグマを
使うことを勧めます。
一例を挙げると、修飾子はパターンマッチングに対してのみ影響を与え、
置換には拡張されないことに注意してください;
いっぽうプラグマを使うと、そのスコープ内の全ての適切な操作について
一貫した結果となります。
例えば:
s/foo/\Ubar/il
=begin original
will match "foo" using the locale's rules for case-insensitive matching,
but the C</l> does not affect how the C<\U> operates. Most likely you
want both of them to use locale rules. To do this, instead compile the
regular expression within the scope of C<use locale>. This both
implicitly adds the C</l> and applies locale rules to the C<\U>. The
lesson is to C<use locale> and not C</l> explicitly.
=end original
これは大文字小文字マッチングにロケールの規則を使って "foo" に
マッチングしますが、C</l> は C<\U> がどう処理を行うかに影響を与えません。
あなたはほぼ確実にこれら二つにロケールの規則を使うことを臨むはずです。
これをするためには、代わりに
C<use locale> のスコープ内で正規表現をコンパイルします。
これらは両方とも暗黙に C</l> を追加し、C<\U> にロケールの規則を適用します。.
ここで学ぶべきことは、C<use locale> を使って、明示的に C</l> を
使わないことです。
=begin original
Similarly, it would be better to use C<use feature 'unicode_strings'>
instead of,
=end original
同様に、Unicode の規則にするには次のようにする代わりに
C<use feature 'unicode_strings'> を使った方が良いです:
s/foo/\Lbar/iu
=begin original
to get Unicode rules, as the C<\L> in the former (but not necessarily
the latter) would also use Unicode rules.
=end original
前者の C<\L> (しかし後者は必要ではないです) も Unicode の規則を
使うからです。
=begin original
More detail on each of the modifiers follows. Most likely you don't
need to know this detail for C</l>, C</u>, and C</d>, and can skip ahead
to L<E<sol>a|/E<sol>a (and E<sol>aa)>.
=end original
それぞれの修飾子に関するさらなる詳細は後述します。
ほとんど確実にあなたは C</l>, C</u>, C</d> の詳細を知る必要はなく、
L<E<sol>a|/E<sol>a (and E<sol>aa)> まで読み飛ばせます。
=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 (on ASCII platforms) 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> の基では認められません。
例えば、(ASCII プラットフォームで) 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).
(Otherwise Perl considers their meanings to be undefined.) Thus,
under this modifier, the ASCII platform effectively becomes a Unicode
platform; and 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 と同じです)。
(さもなければ Perl はこれらの意味は未定義として扱います。)
従って、この修飾子の基では、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> in
the world 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. Or the C</a> modifier can be used to force C<\d> to match
just the ASCII 0 through 9.
=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()> が使われます。
あるいは C</a> 修飾子は、C<\d> が単に ASCII の 0 から 9 に
マッチングすることを強制するために使えます。
=begin original
Also, under this modifier, 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
This modifier may be specified to be the default by C<use feature
'unicode_strings>, C<use locale ':not_characters'>, or
C<L<use 5.012|perlfunc/use VERSION>> (or higher),
but see L</Which character set modifier is in effect?>.
X</u>
=end original
この修飾子は C<use feature 'unicode_strings>,
C<use locale ':not_characters'>, C<L<use 5.012|perlfunc/use VERSION>>
(またはそれ以上) によってデフォルトに
設定されますが、L</Which character set modifier is in effect?> を
参照してください。
X</u>
=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{...}>); or
=end original
パターンが Unicode 特性 (C<\p{...}>) を使っている; または
=item 6
=begin original
the pattern uses L</C<(?[ ])>>
=end original
パターンが L</C<(?[ ])>> を使っている
=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">. The Unicode Bug has
become rather infamous, leading to yet another (printable) name for this
modifier, "Dodgy".
=end original
この修飾子のもう一つの記憶法は「依存」(Depends)です; 規則は実際には
様々なことに依存していること、また結果として予想外の
結果になるかもしれないからです。
L<perlunicode/The "Unicode Bug"> を参照してください。
Unicode バグは、悪名高くなり、この修飾子のもう一つの(表示可能な)
名前 "Dodgy" を引き起こしています。
=begin original
Unless the pattern or string are encoded in UTF-8, only ASCII characters
can match positively.
=end original
パターンや文字列が UTF-8 でエンコードされていない限り、
ASCII 文字のみが肯定的にマッチングします。
=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.
=begin original
This modifier is automatically selected by default when none of the
others are, so yet another name for it is "Default".
=end original
この修飾子は他のものが指定されなかった場合にデフォルトとして自動的に
洗濯されるので、これのもう一つの名前は "Default" です。
=begin original
Because of the unexpected behaviors associated with this modifier, you
probably should only use it to maintain weird backward compatibilities.
=end original
この修飾子に関する想定外の振る舞いにより、おそらくおかしな後方互換性を
維持するためだけにこれを使うべきでしょう。
=head4 /a (and /aa)
(/a (と /aa))
=begin original
This modifier stands for ASCII-restrict (or ASCII-safe). This modifier,
unlike the others, may be doubled-up to increase its effect.
=end original
この修飾子は ASCII 制限 (あるいは ASCII セーフ) を意味します。
この修飾子は、他のものと異なり、2 倍にすることで効果が増します。
=begin original
When it appears singly, it causes the sequences C<\d>, C<\s>, C<\w>, and
the Posix character classes to match only in the ASCII range. They thus
revert to their pre-5.6, pre-Unicode meanings. Under C</a>, C<\d>
always means precisely the digits C<"0"> to C<"9">; C<\s> means the five
characters C<[ \f\n\r\t]>, and starting in Perl v5.18, experimentally,
the vertical tab; 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<\d>, C<\s>, C<\w>, Posix 文字クラスは
ASCII の範囲のみにマッチングするようになります。
従って、これらは 5.6 以前の、Unicode 以前の意味に戻します。
C</a> の基では、C<\d> は常に正確に数字 C<"0"> から C<"9"> を意味します;
C<\s> は C<[ \f\n\r\t]> の 5 文字、および Perl v5.18 から実験的に垂直タブ、
を意味します;
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,
and who do not wish to be burdened with its complexities and security
concerns.
=end original
この修飾子は、偶然 Unicode を使っている人々で、その複雑さと
セキュリティの問題に関する重荷を背負いたくない人々にとっては有用です。
=begin original
With C</a>, 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 instead use C<\p{Digit}> (or C<\p{Word}> for C<\w>). There are
similar C<\p{...}> constructs that can match beyond ASCII both white
space (see L<perlrecharclass/Whitespace>), and Posix classes (see
L<perlrecharclass/POSIX Character Classes>). Thus, this modifier
doesn't mean you can't use Unicode, it means that to get Unicode
matching you must explicitly use a construct (C<\p{}>, C<\P{}>) that
signals Unicode.
=end original
C</a> を使うと、ASCII 文字だけにマッチングすることに自信を持って
書くことができ、ASCII を超えてマッチングする必要が発生したときには、
代わりに C<\p{Digit}> (または C<\w> として C<\p{Word}>) が使えます。
ASCII を超えたスペース (L<perlrecharclass/Whitespace> を参照してください)
と Posix クラス (L<perlrecharclass/POSIX Character Classes> を
参照してください) の両方にマッチングする似たような C<\p{...}> 構文があります。
従って、この修飾子は Unicode が使えなくなるということではなく、
Unicode のマッチングには明示的に Unicode を意味する構文
(C<\p{}>, C<\P{}>) を使わないといけないということです。
=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>. (The first
occurrence of "a" restricts the C<\d>, etc., and the second occurrence
adds the C</i> restrictions.) But, note that code points outside the
ASCII range will use Unicode rules for C</i> matching, so the modifier
doesn't really restrict things to just ASCII; it just forbids the
intermixing of ASCII and non-ASCII.
=end original
("k" と C<\N{KELVIN SIGN}> のような) ASCII/非-ASCII マッチングを禁止するには、
C</aai> や C</aia> のように "a" を 2 回指定します。
(最初の "a" は C<\d> などを制限し、2 番目は C</i> の制限を追加します。)
しかし、 ASCII の範囲外の符号位置は C</i> マッチングに Unicode 規則を
使うので、この修飾子は実際には単に ASCII に制限するものではないことに
注意してください; これは単に ASCII と非 ASCII を混ぜることを禁止します。
=begin original
To summarize, 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'>. If you do so, you may actually have occasion to use
the C</u> modifier explictly if there are a few regular expressions
where you do want full Unicode rules (but even here, it's best if
everything were under feature C<"unicode_strings">, along with the
C<use re '/aa'>). Also see L</Which character set modifier is in
effect?>.
X</a>
X</aa>
=end original
この修飾子は C<use re '/a'> または C<use re '/aa'> でデフォルトに
設定されます。
そうすると、もし完全な Unicode 規則を使いたい正規表現がある場合は、
C</u> 修飾子を明示的に使う機会があるかもしれません
(その場合でも、全てが C<"unicode_strings"> の基なら、
C<use re '/aa'> と共にするのが最良です)。
L</Which character set modifier is in effect?> も参照してください。
X</a>
X</aa>
=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. These have
been designed so that in general you don't have to worry about it, but