perlre > 5.16.1 との差分

perlre 5.16.1 と 5.30.0 の差分

11
22=encoding euc-jp
33
44=head1 NAME
55X<regular expression> X<regex> X<regexp>
66
77=begin original
88
99perlre - Perl regular expressions
1010
1111=end original
1212
1313perlre - Perl の正規表現
1414
1515=head1 DESCRIPTION
1616
1717=begin original
1818
1919This page describes the syntax of regular expressions in Perl.
2020
2121=end original
2222
2323このページでは Perl での正規表現の構文について説明します。
2424
2525=begin original
2626
27If you haven't used regular expressions before, a quick-start
27If you haven't used regular expressions before, a tutorial introduction
28introduction is available in L<perlrequick>, and a longer tutorial
28is available in L<perlretut>. If you know just a little about them,
29introduction is available in L<perlretut>.
29a quick-start introduction is available in L<perlrequick>.
3030
3131=end original
3232
33もしこれまでに正規表現を使ったことがないのであれば
33もしこれまでに正規表現を使ったことがないなら
34L<perlrequick> にクイックスタートが、L<perlretut> に
34L<perlretut> にチュートリアルあります。
35長めのチュートリアルがあります。
35正規表現を少しだけ知っているなら、L<perlrequick> に
36クイックスタートがあります。
3637
3738=begin original
3839
39For reference on how regular expressions are used in matching
40Except for L</The Basics> section, this page assumes you are familiar
40operations, plus various examples of the same, see discussions of
41with regular expression basics, like what is a "pattern", what does it
41C<m//>, C<s///>, C<qr//> and C<??> in L<perlop/"Regexp Quote-Like
42look like, and how it is basically used. For a reference on how they
42Operators">.
43are used, plus various examples of the same, see discussions of C<m//>,
44C<s///>, C<qr//> and C<"??"> in L<perlop/"Regexp Quote-Like Operators">.
4345
4446=end original
4547
46正規表現マッチング操作でどように使うやそれに関する様々な例に
48L</The Basics> 節例外として、こページは、「パターン」とは何
49それはどのような見た目のものか、基本的にどのようにして使われるか、といった
50正規表現の基本に親しんでいることを仮定しています。
51それらをどのように使うのかやそれに関する様々な例に
4752関しては、L<perlop/"Regexp Quote-Like Operators"> にある
48C<m//>, C<s///>, C<qr//>, C<??> の説明を参照して下さい。
53C<m//>, C<s///>, C<qr//>, C<"??"> の説明を参照して下さい。
4954
55=begin original
56
57New in v5.22, L<C<use re 'strict'>|re/'strict' mode> applies stricter
58rules than otherwise when compiling regular expression patterns. It can
59find things that, while legal, may not be what you intended.
60
61=end original
62
63v5.22 から、L<C<use re 'strict'>|re/'strict' mode> は、
64正規表現パターンをコンパイルするときにその他よりもより厳しい規則を
65適用します。
66これは、正当ではあるけれども、意図していないかもしれないものを
67見つけることができます。
68
69=head2 The Basics
70X<regular expression, version 8> X<regex, version 8> X<regexp, version 8>
71
72(基本)
73
74=begin original
75
76Regular expressions are strings with the very particular syntax and
77meaning described in this document and auxiliary documents referred to
78by this one. The strings are called "patterns". Patterns are used to
79determine if some other string, called the "target", has (or doesn't
80have) the characteristics specified by the pattern. We call this
81"matching" the target string against the pattern. Usually the match is
82done by having the target be the first operand, and the pattern be the
83second operand, of one of the two binary operators C<=~> and C<!~>,
84listed in L<perlop/Binding Operators>; and the pattern will have been
85converted from an ordinary string by one of the operators in
86L<perlop/"Regexp Quote-Like Operators">, like so:
87
88=end original
89
90正規表現とは、この文書と、この文書で参照される補助文書で記述されている、
91特定の構文と意味を持つ文字列です。
92この文字列は「パターン」と呼ばれます。
93パターンは、「ターゲット」と呼ばれる他の文字列が、パターンで指定された
94特性を持つ(または持たない)かどうかを判別するために使われます。
95これをパターンに対するターゲット文字列の「マッチング」と呼びます。
96通常、マッチングは、L<perlop/Binding Operators> にリストされている
97二つの二項演算子 C<=~> および C<!~> の一つの、ターゲットを
98最初のオペランドにし、パターンを 2 番目のオペランドにすることによって
99行われます;
100そしてパターンは次のように、L<perlop/"Regexp Quote-Like Operators"> の
101演算子の一つによって通常の文字列から変換されています。
102
103 $foo =~ m/abc/
104
105=begin original
106
107This evaluates to true if and only if the string in the variable C<$foo>
108contains somewhere in it, the sequence of characters "a", "b", then "c".
109(The C<=~ m>, or match operator, is described in
110L<perlop/m/PATTERN/msixpodualngc>.)
111
112=end original
113
114これは、変数 C<$foo> 内の文字列のどこかに文字並び "a", "b", "c" が
115含まれている場合にのみ真と評価されます。
116(マッチング演算子 C<=~ m> は、L<perlop/m/PATTERN/msixpodualngc> で
117説明されています。)
118
119=begin original
120
121Patterns that aren't already stored in some variable must be delimitted,
122at both ends, by delimitter characters. These are often, as in the
123example above, forward slashes, and the typical way a pattern is written
124in documentation is with those slashes. In most cases, the delimitter
125is the same character, fore and aft, but there are a few cases where a
126character looks like it has a mirror-image mate, where the opening
127version is the beginning delimiter, and the closing one is the ending
128delimiter, like
129
130=end original
131
132何らかの変数にまだ格納されていないパターンは、両端が区切り文字で
133区切られている必要があります。
134これらは上の例のようにスラッシュであることが多く、文書中で
135パターンを記述する一般的な方法はこれらのスラッシュです。
136ほとんどの場合、区切り文字は前と後で同じ文字ですが、文字が鏡像のように
137見える場合がいくつかあります;
138次のように、開くのが開始区切り文字で、閉じるのが終了区切り文字です:
139
140 $foo =~ m<abc>
141
142=begin original
143
144Most times, the pattern is evaluated in double-quotish context, but it
145is possible to choose delimiters to force single-quotish, like
146
147=end original
148
149ほとんどの場合、パターンはダブルクォート風コンテキストで評価されますが、
150次のように区切り文字を選択してシングルクォート風を強制することもできます:
151
152 $foo =~ m'abc'
153
154=begin original
155
156If the pattern contains its delimiter within it, that delimiter must be
157escaped. Prefixing it with a backslash (I<e.g.>, C<"/foo\/bar/">)
158serves this purpose.
159
160=end original
161
162パターン内に区切り文字が含まれている場合は、その区切り文字を
163エスケープする必要があります。
164逆スラッシュを前置すると (例えば、C<"/foo\/bar/">)、この目的を
165達成できます。
166
167=begin original
168
169Any single character in a pattern matches that same character in the
170target string, unless the character is a I<metacharacter> with a special
171meaning described in this document. A sequence of non-metacharacters
172matches the same sequence in the target string, as we saw above with
173C<m/abc/>.
174
175=end original
176
177パターン中のすべての単一の文字は、その文字が個々でまたはこの文書で
178説明されている特別な意味を持った I<メタ文字> である場合以外は、
179ターゲット文字列内の同じ文字にマッチングします。
180非メタ文字の並びは、前述の C<m/abc/> で見たように、
181ターゲット文字列の同じ並びにマッチングします。
182
183=begin original
184
185Only a few characters (all of them being ASCII punctuation characters)
186are metacharacters. The most commonly used one is a dot C<".">, which
187normally matches almost any character (including a dot itself).
188
189=end original
190
191ほんのいくつかの文字 (全て ASCII の句読点文字) がメタ文字です。
192もっとも一般的に使われるものはドット C<"."> で、これは通常
193(ドット自身を含む) ほとんどの文字にマッチングします。
194
195=begin original
196
197You can cause characters that normally function as metacharacters to be
198interpreted literally by prefixing them with a C<"\">, just like the
199pattern's delimiter must be escaped if it also occurs within the
200pattern. Thus, C<"\."> matches just a literal dot, C<"."> instead of
201its normal meaning. This means that the backslash is also a
202metacharacter, so C<"\\"> matches a single C<"\">. And a sequence that
203contains an escaped metacharacter matches the same sequence (but without
204the escape) in the target string. So, the pattern C</blur\\fl/> would
205match any target string that contains the sequence C<"blur\fl">.
206
207=end original
208
209文字は C<"\"> で前置されることで通常はメタ文字としての機能を持っている文字を
210リテラルとして処理させられるようになります;
211パターンの区切り文字がパターンの中に現れる場合は
212エスケープされなければならないのと同様です。
213従って、C<"\."> は、通常の意味ではなく、
214単にリテラルなドット C<"."> にマッチングするようになります。
215つまり、逆スラッシュもメタ文字なので、
216"\\" は単一の C<"\"> にマッチングするということです。
217エスケープされたメタ文字を含む並びは、ターゲット文字列の中の
218同じ並び(但しエスケープなし)にマッチングします。
219それで、パターン C</blur\\fl/> は
220並び C<"blur\fl"> を含むターゲット文字列にマッチングします。
221
222=begin original
223
224The metacharacter C<"|"> is used to match one thing or another. Thus
225
226=end original
227
228メタ文字 C<"|"> は二つのもののどちらかをマッチングするのに使われます。
229従って:
230
231 $foo =~ m/this|that/
232
233=begin original
234
235is TRUE if and only if C<$foo> contains either the sequence C<"this"> or
236the sequence C<"that">. Like all metacharacters, prefixing the C<"|">
237with a backslash makes it match the plain punctuation character; in its
238case, the VERTICAL LINE.
239
240=end original
241
242これは C<$foo> に並び C<"this"> または並び C<"that"> のどちらかが
243含まれている場合にのみ真になります。
244全てのメタ文字と同様、C<"|"> に逆スラッシュを前置すると普通の句読点文字、
245この場合は VERTICAL LINE にマッチングします。
246
247 $foo =~ m/this\|that/
248
249=begin original
250
251is TRUE if and only if C<$foo> contains the sequence C<"this|that">.
252
253=end original
254
255これは、C<$foo> に C<"this|that"> という並びを含んでいる場合にのみ
256真になります。
257
258=begin original
259
260You aren't limited to just a single C<"|">.
261
262=end original
263
264単一の C<"|"> だけに制限されません。
265
266 $foo =~ m/fee|fie|foe|fum/
267
268=begin original
269
270is TRUE if and only if C<$foo> contains any of those 4 sequences from
271the children's story "Jack and the Beanstalk".
272
273=end original
274
275これは、C<$foo> に童話「ジャックとまめの木」から取った
276四つの並びのいずれがを含んでいる場合にのみ真になります。
277
278=begin original
279
280As you can see, the C<"|"> binds less tightly than a sequence of
281ordinary characters. We can override this by using the grouping
282metacharacters, the parentheses C<"("> and C<")">.
283
284=end original
285
286ここで見られるように、C<"|"> は通常の文字の並びより弱く結びつけます。
287これはグループ化メタ文字であるかっこ C<"("> と C<")"> を使って
288上書きできます。
289
290 $foo =~ m/th(is|at) thing/
291
292=begin original
293
294is TRUE if and only if C<$foo> contains either the sequence S<C<"this
295thing">> or the sequence S<C<"that thing">>. The portions of the string
296that match the portions of the pattern enclosed in parentheses are
297normally made available separately for use later in the pattern,
298substitution, or program. This is called "capturing", and it can get
299complicated. See L</Capture groups>.
300
301=end original
302
303これは C<$foo> に並び S<C<"this thing">> または並び
304S<C<"that thing">> のいずれかが含まれている場合にのみ TRUE が返されます。
305かっこで囲まれたパターンの部分と一致する文字列の部分は、通常、後でパターン、
306置換、プログラムで使用するために個別に使用できます。
307これは「捕捉」(capturing)と呼ばれ、複雑になる場合があります。
308L</Capture groups> を参照してください。
309
310=begin original
311
312The first alternative includes everything from the last pattern
313delimiter (C<"(">, C<"(?:"> (described later), I<etc>. or the beginning
314of the pattern) up to the first C<"|">, and the last alternative
315contains everything from the last C<"|"> to the next closing pattern
316delimiter. That's why it's common practice to include alternatives in
317parentheses: to minimize confusion about where they start and end.
318
319=end original
320
321最初の代替には最後のパターン区切り (C<"(">, C<"(?:"> (後述) など、または
322パターンの始まり)から最初の C<"|"> までのすべてが含まれ、
323最後の代替には最後の C<"|"> から次の閉じパターン区切りまでが含まれます。
324通常代替をかっこの中に入れるのは、その開始位置と終了位置が少しはわかりやすく
325なるようにです。
326
327=begin original
328
329Alternatives are tried from left to right, so the first
330alternative found for which the entire expression matches, is the one that
331is chosen. This means that alternatives are not necessarily greedy. For
332example: when matching C<foo|foot> against C<"barefoot">, only the C<"foo">
333part will match, as that is the first alternative tried, and it successfully
334matches the target string. (This might not seem important, but it is
335important when you are capturing matched text using parentheses.)
336
337=end original
338
339代替は左から右へと試されます、なので最初の代替がその完全な式で
340マッチングしたのならそれが選択されます。
341これは代替は貪欲である必要はないということを意味します。
342例えば: C<"barefoot"> に対して C<foo|foot> をマッチングさせると、
343最初の代替から試されるので、C<"foo"> の部分がマッチングし、
344これは対象の文字列に対して成功でマッチングします。
345(これは重要ではないでしょうが、かっこを使ってマッチングしたテキストを
346捕捉しているときには重要でしょう。)
347
348=begin original
349
350Besides taking away the special meaning of a metacharacter, a prefixed
351backslash changes some letter and digit characters away from matching
352just themselves to instead have special meaning. These are called
353"escape sequences", and all such are described in L<perlrebackslash>. A
354backslash sequence (of a letter or digit) that doesn't currently have
355special meaning to Perl will raise a warning if warnings are enabled,
356as those are reserved for potential future use.
357
358=end original
359
360接頭辞付き逆スラッシュは、メタ文字の特殊な意味を取り除くだけでなく、
361一部の文字や数字をそれ自体と一致させないように変更し、
362代わりに特殊な意味を持つようにします。
363これらは「エスケープシーケンス」と呼ばれ、L<perlrebackslash> で
364説明されています。
365現在 Perl にとって特殊な意味を持たない (文字や数字の)
366逆スラッシュシーケンスは、警告が有効になっている場合に警告を発します;
367これらは将来使用するために予約されているためです。
368
369=begin original
370
371One such sequence is C<\b>, which matches a boundary of some sort.
372C<\b{wb}> and a few others give specialized types of boundaries.
373(They are all described in detail starting at
374L<perlrebackslash/\b{}, \b, \B{}, \B>.) Note that these don't match
375characters, but the zero-width spaces between characters. They are an
376example of a L<zero-width assertion|/Assertions>. Consider again,
377
378=end original
379
380そのようなシーケンスのひとつは C<\b> です;
381これはある種の境界にマッチします。
382C<\b{wb}> やその他のいくつかは特定の境界を与えます。
383(これらはすべて L<perlrebackslash/\b{}, \b, \B{}, \B> で詳細に
384記述されています。)
385これらは文字ではなく、文字と文字の間のゼロ幅の
386スペースにマッチングすることに注意してください。
387これらは L<ゼロ幅言明|/Assertions> の例です。
388もう一度考えてみます:
389
390 $foo =~ m/fee|fie|foe|fum/
391
392=begin original
393
394It evaluates to TRUE if, besides those 4 words, any of the sequences
395"feed", "field", "Defoe", "fume", and many others are in C<$foo>. By
396judicious use of C<\b> (or better (because it is designed to handle
397natural language) C<\b{wb}>), we can make sure that only the Giant's
398words are matched:
399
400=end original
401
402これは、これら四つの単語以外に、"feed", "field", "Defoe", "fume",
403その他多くのシーケンスのいずれかが C<$foo> にある場合、TRUE と評価されます。
404C<\b>(または(自然言語を処理するように設計されているため) より良い
405C<\b{wb}>)を慎重に使用することで、
406確実に巨人の単語だけが一致するようにできます。
407
408 $foo =~ m/\b(fee|fie|foe|fum)\b/
409 $foo =~ m/\b{wb}(fee|fie|foe|fum)\b{wb}/
410
411=begin original
412
413The final example shows that the characters C<"{"> and C<"}"> are
414metacharacters.
415
416=end original
417
418最後の例は、文字 C<"{"> と C<"}"> がメタ文字であることを示しています。
419
420=begin original
421
422Another use for escape sequences is to specify characters that cannot
423(or which you prefer not to) be written literally. These are described
424in detail in L<perlrebackslash/Character Escapes>, but the next three
425paragraphs briefly describe some of them.
426
427=end original
428
429エスケープシーケンスのもう一つの使用法は、文字通りに書くことができない
430(あるいは書きたくない)文字を指定することです。
431これらについては
432L<perlrebackslash/Character Escapes> で詳しく説明していますが、
433次の三つの段落でその一部を簡単に説明します。
434
435=begin original
436
437Various control characters can be written in C language style: C<"\n">
438matches a newline, C<"\t"> a tab, C<"\r"> a carriage return, C<"\f"> a
439form feed, I<etc>.
440
441=end original
442
443様々な制御文字は C 言語形式で書くことができます:
444"\n" は改行にマッチングし、C<"\t"> はタブに、C<"\r"> は復帰に、
445C<"\f"> はフォームフィードにといった具合にマッチングします。
446
447=begin original
448
449More generally, C<\I<nnn>>, where I<nnn> is a string of three octal
450digits, matches the character whose native code point is I<nnn>. You
451can easily run into trouble if you don't have exactly three digits. So
452always use three, or since Perl 5.14, you can use C<\o{...}> to specify
453any number of octal digits.
454
455=end original
456
457より一般的に、C<\I<nnn>> (I<nnn> は 3 桁の 8 進数字) は
458ネイティブな符号位置が I<nnn> の文字にマッチングします。
459正確に 3 桁以外の数字を使うと、簡単に困難に陥ります。
460従って、常に 3 桁で使うか、Perl 5.14 以降なら、
461任意の桁の 8 進数を使うために C<\o{...}> を使えます。
462
463=begin original
464
465Similarly, C<\xI<nn>>, where I<nn> are hexadecimal digits, matches the
466character whose native ordinal is I<nn>. Again, not using exactly two
467digits is a recipe for disaster, but you can use C<\x{...}> to specify
468any number of hex digits.
469
470=end original
471
472同じように、\xI<nn> (I<nn> は16進数字) はネイティブな数値で I<nn> に
473なる文字にマッチングします。
474再び、正確に 2 桁以外の数字を使うのは災いの元ですが、
475任意の桁の 16 進数を指定するために C<\x{...}> を使えます。
476
477=begin original
478
479Besides being a metacharacter, the C<"."> is an example of a "character
480class", something that can match any single character of a given set of
481them. In its case, the set is just about all possible characters. Perl
482predefines several character classes besides the C<".">; there is a
483separate reference page about just these, L<perlrecharclass>.
484
485=end original
486
487メタ文字であることに加えて、C<"."> は、特定の集合の任意の 1 文字に
488マッチングする「文字クラス」の例です。
489この場合、集合ははほぼすべての可能な文字です。
490Perlは C<"."> 以外にもいくつかの文字クラスを事前定義しています;
491これらについては、L<perlrecharclass> という別のリファレンスページが
492あります。
493
494=begin original
495
496You can define your own custom character classes, by putting into your
497pattern in the appropriate place(s), a list of all the characters you
498want in the set. You do this by enclosing the list within C<[]> bracket
499characters. These are called "bracketed character classes" when we are
500being precise, but often the word "bracketed" is dropped. (Dropping it
501usually doesn't cause confusion.) This means that the C<"["> character
502is another metacharacter. It doesn't match anything just by itself; it
503is used only to tell Perl that what follows it is a bracketed character
504class. If you want to match a literal left square bracket, you must
505escape it, like C<"\[">. The matching C<"]"> is also a metacharacter;
506again it doesn't match anything by itself, but just marks the end of
507your custom class to Perl. It is an example of a "sometimes
508metacharacter". It isn't a metacharacter if there is no corresponding
509C<"[">, and matches its literal self:
510
511=end original
512
513独自のカスタム文字クラスを定義するには、パターン内の適切な場所に、集合内に
514必要なすべての文字のリストを配置します。
515これを行うには、リストを C<[]> 大かっこ文字で囲みます。
516これらは、正確にであれば「大かっこ文字クラス」と呼ばれますが、
517「大かっこ」という単語が削除されることがよくあります。
518(通常は、これを削除しても混乱は生じません。)
519これは、C<"["> 文字はもう一つのメタ文字であることを意味します。
520これ自身だけでは何にもマッチングしません;
521Perl に対して、後に続くものが大かっこ文字クラスであることを
522伝えるためにのみ使用されます。
523リテラルの左大かっこにマッチさせたい場合は、C<"\["> のように
524エスケープする必要があります。
525一致する C<"]"> もメタ文字です;
526ここでも何にもマッチしませんが、カスタムクラスの終わりを Perl に
527マークするだけです。
528これは「時々メタ文字」の例です。
529対応する C<"["> が存在しない場合はメタ文字ではなく、
530リテラルにマッチングします。
531
532 print "]" =~ /]/; # prints 1
533
534=begin original
535
536The list of characters within the character class gives the set of
537characters matched by the class. C<"[abc]"> matches a single "a" or "b"
538or "c". But if the first character after the C<"["> is C<"^">, the
539class instead matches any character not in the list. Within a list, the
540C<"-"> character specifies a range of characters, so that C<a-z>
541represents all characters between "a" and "z", inclusive. If you want
542either C<"-"> or C<"]"> itself to be a member of a class, put it at the
543start of the list (possibly after a C<"^">), or escape it with a
544backslash. C<"-"> is also taken literally when it is at the end of the
545list, just before the closing C<"]">. (The following all specify the
546same class of three characters: C<[-az]>, C<[az-]>, and C<[a\-z]>. All
547are different from C<[a-z]>, which specifies a class containing
548twenty-six characters, even on EBCDIC-based character sets.)
549
550=end original
551
552文字クラスの中の文字のリストは、そのクラスがマッチングする
553文字の集合を表しています。
554C<"[abc]"> は単一の "a" または "b" または "c" にマッチングします。.
555しかし、C<"["> の後の最初の文字が C<"^"> だったときには、その文字クラスは
556リストの中にない任意の文字にマッチングします。
557リストの中では、文字 C<"-"> は文字の範囲を意味します;
558なので C<a-z> は "a" と "z" を含めてそれらの間にあるすべての文字を表します。
559文字クラスの要素として C<"-"> または C<"]"> 自身を使いたい時には、
560リストの先頭に (あるいは C<"^"> の後に) 置くか、逆スラッシュを使って
561エスケープします。
562C<"-"> はリストの終端、リストを閉じる C<"]"> の直前にあったときも
563リテラルとして扱われます。
564(次の例はすべて同じ3文字からなる文字クラスです: C<[-az]>, C<[az-]>,
565C<[a\-z]>。
566これらはすべて EBCDIC ベースの文字集合であっても26文字からなる文字集合
567C<[a-z]> とは異なります。)
568
569=begin original
570
571There is lots more to bracketed character classes; full details are in
572L<perlrecharclass/Bracketed Character Classes>.
573
574=end original
575
576大かっこ文字クラスにはもっと色々な要素があります; 完全な詳細は
577L<perlrecharclass/Bracketed Character Classes> にあります。
578
579=head3 Metacharacters
580X<metacharacter>
581X<\> X<^> X<.> X<$> X<|> X<(> X<()> X<[> X<[]>
582
583(メタ文字)
584
585=begin original
586
587L</The Basics> introduced some of the metacharacters. This section
588gives them all. Most of them have the same meaning as in the I<egrep>
589command.
590
591=end original
592
593L</The Basics> ではメタ文字の一部を導入しました。
594この節ではその全てを示します。
595そのほとんどは I<egrep> コマンドと同じ意味を持ちます。
596
597=begin original
598
599Only the C<"\"> is always a metacharacter. The others are metacharacters
600just sometimes. The following tables lists all of them, summarizes
601their use, and gives the contexts where they are metacharacters.
602Outside those contexts or if prefixed by a C<"\">, they match their
603corresponding punctuation character. In some cases, their meaning
604varies depending on various pattern modifiers that alter the default
605behaviors. See L</Modifiers>.
606
607=end original
608
609C<"\"> のみが常にメタ文字です。
610その他は時々にだけメタ文字です。
611次の表は、すべてのメタ文字の一覧、使用方法の概要、
612メタ文字になるコンテキストを示しています。
613これらのコンテキスト以外では、または C<"\"> で始まる場合は、
614対応する句読点文字とマッチングします。
615場合によっては、既定の動作を変更するさまざまなパターン修飾子によって
616意味が異なります。
617L</Modifiers> を参照してください。
618
619=begin original
620
621 PURPOSE WHERE
622 \ Escape the next character Always, except when
623 escaped by another \
624 ^ Match the beginning of the string Not in []
625 (or line, if /m is used)
626 ^ Complement the [] class At the beginning of []
627 . Match any single character except newline Not in []
628 (under /s, includes newline)
629 $ Match the end of the string Not in [], but can
630 (or before newline at the end of the mean interpolate a
631 string; or before any newline if /m is scalar
632 used)
633 | Alternation Not in []
634 () Grouping Not in []
635 [ Start Bracketed Character class Not in []
636 ] End Bracketed Character class Only in [], and
637 not first
638 * Matches the preceding element 0 or more Not in []
639 times
640 + Matches the preceding element 1 or more Not in []
641 times
642 ? Matches the preceding element 0 or 1 Not in []
643 times
644 { Starts a sequence that gives number(s) Not in []
645 of times the preceding element can be
646 matched
647 { when following certain escape sequences
648 starts a modifier to the meaning of the
649 sequence
650 } End sequence started by {
651 - Indicates a range Only in [] interior
652 # Beginning of comment, extends to line end Only with /x modifier
653
654=end original
655
656 目的 場所
657 \ 次の文字をエスケープ もう一つの \ で
658 エスケープしない限り常に
659 ^ 文字列(または /m が使われていれば行) の [] の中以外
660 先頭にマッチング
661 ^ [] クラスの補集合 [] の先頭
662 . 改行以外の任意の 1 文字にマッチング [] の中以外
663 (/s の下では改行を含む)
664 $ 文字列の末尾にマッチング [] の中以外、しかし
665 (または文字列の最後の改行の前; スカラの変数展開を
666 または /m が使われていれば改行の前) 意味する
667 | 代替 [] の中以外
668 () グループ化 [] の中以外
669 [ 大かっこ文字クラスの開始 [] の中以外
670 ] 大かっこ文字クラスの終了 [] のみで先頭以外
671 * 前にある要素に 0 回以上マッチング [] の中以外
672 + 前にある要素に 1 回以上マッチング [] の中以外
673 ? 前にある要素に 0 回または 1 回マッチング [] の中以外
674 { 前にある要素がマッチングする回数を指定する [] の中以外
675 並びの開始
676 { 以下のいくつかのエスケープシーケンスで
677 シーケンスの意味の修飾子の開始
678 } { で開始した並びの終わり
679 - 範囲を示す [] の内部のみ
680 # コメントの開始; 行末まで /x 修飾子のみ
681
682=begin original
683
684Notice that most of the metacharacters lose their special meaning when
685they occur in a bracketed character class, except C<"^"> has a different
686meaning when it is at the beginning of such a class. And C<"-"> and C<"]">
687are metacharacters only at restricted positions within bracketed
688character classes; while C<"}"> is a metacharacter only when closing a
689special construct started by C<"{">.
690
691=end original
692
693ほとんどのメタ文字は、かっこで囲まれた文字クラス内で出現すると
694特殊な意味を失うことに注意してください;
695ただし、C<"^"> は、そのようなクラスの先頭では異なる意味を持ちます。
696また、C<"-"> と C<"]"> は、かっこ弧で囲まれた文字クラス内の限定された
697位置でだけメタ文字になります;
698一方、C<"}"> は、C<"{"> によって開始された特殊な構造体を
699閉じるときにのみメタ文字です。
700
701=begin original
702
703In double-quotish context, as is usually the case, you need to be
704careful about C<"$"> and the non-metacharacter C<"@">. Those could
705interpolate variables, which may or may not be what you intended.
706
707=end original
708
709ダブルクォート風のコンテキストでは、通常の場合と同様、
710C<"$"> とメタ文字でない C<"@"> に注意する必要があります。
711これらは変数を補完することができますが、それは
712意図したものである場合とない場合があります。
713
714=begin original
715
716These rules were designed for compactness of expression, rather than
717legibility and maintainability. The L</E<sol>x and E<sol>xx> pattern
718modifiers allow you to insert white space to improve readability. And
719use of S<C<L<re 'strict'|re/'strict' mode>>> adds extra checking to
720catch some typos that might silently compile into something unintended.
721
722=end original
723
724これらの規則は、読みやすさや保守性ではなく、表現のコンパクトさを
725考慮して設計されています。
726L</E<sol>x and E<sol>xx> パターン修飾子を使用すると、読みやすさを
727向上させるために空白を挿入できます。
728また、S<C<L<re 'strict'|re/'strict' mode>>> を使用すると、
729意図しないものに暗黙的にコンパイルされる可能性のあるタイプミスを
730捕捉するための追加チェックが追加されます。
731
732=begin original
733
734By default, the C<"^"> character is guaranteed to match only the
735beginning of the string, the C<"$"> character only the end (or before the
736newline at the end), and Perl does certain optimizations with the
737assumption that the string contains only one line. Embedded newlines
738will not be matched by C<"^"> or C<"$">. You may, however, wish to treat a
739string as a multi-line buffer, such that the C<"^"> will match after any
740newline within the string (except if the newline is the last character in
741the string), and C<"$"> will match before any newline. At the
742cost of a little more overhead, you can do this by using the
743L</C<E<sol>m>> modifier on the pattern match operator. (Older programs
744did this by setting C<$*>, but this option was removed in perl 5.10.)
745X<^> X<$> X</m>
746
747=end original
748
749デフォルトでは、文字 C<"^"> は文字列の先頭にのみ、そして文字 C<"$"> は
750末尾(または末尾の改行の前)にのみマッチングすることを保証し、そして Perl は
751文字列が 1 行のみを含んでいるという仮定でいくつかの最適化を行います。
752埋め込まれている改行文字は C<"^"> や C<"$"> とはマッチングしません。
753しかし文字列には複数行が格納されていて、C<"^"> は任意の改行の後(但し
754改行文字が文字列の最後の文字だった場合は除く)、そして C<"$"> は任意の改行の
755前でマッチングさせたいこともあるでしょう。
756小さなオーバーヘッドはありますが、これはパターンマッチングで
757L</C<E<sol>m>> 修飾子を使うことで行うことができます。
758(古いプログラムでは C<$*> を設定することでこれを行っていましたが
759これは perl 5.10 では削除されています。)
760X<^> X<$> X</m>
761
762=begin original
763
764To simplify multi-line substitutions, the C<"."> character never matches a
765newline unless you use the L<C<E<sol>s>|/s> modifier, which in effect tells
766Perl to pretend the string is a single line--even if it isn't.
767X<.> X</s>
768
769=end original
770
771複数行での利用を簡単にするために、文字 C<"."> は L<C<E<sol>s>|/s> 修飾子を
772使って Perl に文字列を 1 行として処理すると Perl に伝えない限り、
773改行にはマッチングしません。
774X<.> X</s>
775
50776=head2 Modifiers
51777
52778(修飾子)
53779
780=head3 Overview
781
782(概要)
783
54784=begin original
55785
56Matching operations can have various modifiers. Modifiers
786The default behavior for matching can be changed, using various
57that relate to the interpretation of the regular expression inside
787modifiers. Modifiers that relate to the interpretation of the pattern
58are listed below. Modifiers that alter the way a regular expression
788are listed just below. Modifiers that alter the way a pattern is used
59is used by Perl are detailed in L<perlop/"Regexp Quote-Like Operators"> and
789by Perl are detailed in L<perlop/"Regexp Quote-Like Operators"> and
60790L<perlop/"Gory details of parsing quoted constructs">.
61791
62792=end original
63793
64マッチング操作には様々な修飾子(modifier)があります。
794マッチングのデフォルトの振る舞い様々な修飾子 (modifier)
65修飾子は正規表現内の解釈に関連する物、次に一覧にしています。
795変更ます。
66Perl が正規表現を使う方法を変更する
796パターンの解釈に関連する修飾子は、直後に一覧にしています。
797Perl がパターンを使う方法を変更する
67798修飾子は L<perlop/"Regexp Quote-Like Operators">
68799及び L<perlop/"Gory details of parsing quoted constructs"> に
69800説明されています。
70801
71802=over 4
72803
73=item m
804=item B<C<m>>
74805X</m> X<regex, multiline> X<regexp, multiline> X<regular expression, multiline>
75806
76807=begin original
77808
78Treat string as multiple lines. That is, change "^" and "$" from matching
809Treat the string being matched against as multiple lines. That is, change C<"^"> and C<"$"> from matching
79the start or end of the string to matching the start or end of any
810the start of the string's first line and the end of its last line to
80line anywhere within the string.
811matching the start and end of each line within the string.
81812
82813=end original
83814
84文字列を複数行として扱います。
815文字列を複数行としてマッチングするように扱います。
85つまり、"^" 及び "$" は文字列の最初最後に対するマッチングから、
816つまり、C<"^"> 及び C<"$"> は文字列の最初の行の先頭および最後の行の末尾に対する
86文字列中の各行の先頭と末尾に対するマッチングへと変更されます。
817マッチングから、文字列中の各行の銭湯と末尾に対するマッチングへと
818変更されます。
87819
88=item s
820=item B<C<s>>
89821X</s> X<regex, single-line> X<regexp, single-line>
90822X<regular expression, single-line>
91823
92824=begin original
93825
94Treat string as single line. That is, change "." to match any character
826Treat the string as single line. That is, change C<"."> to match any character
95827whatsoever, even a newline, which normally it would not match.
96828
97829=end original
98830
99831文字列を 1 行として扱います。
100つまり、"." は任意の 1 文字、通常はマッチングしない改行でさえも
832つまり、C<"."> は任意の 1 文字、通常はマッチングしない改行でさえも
101833マッチングするように変更されます。
102834
103835=begin original
104836
105Used together, as C</ms>, they let the "." match any character whatsoever,
837Used together, as C</ms>, they let the C<"."> match any character whatsoever,
106while still allowing "^" and "$" to match, respectively, just after
838while still allowing C<"^"> and C<"$"> to match, respectively, just after
107839and just before newlines within the string.
108840
109841=end original
110842
111C</ms> として共に使うと、"^" 及び "$" はそれぞれ
843C</ms> として共に使うと、C<"^"> 及び C<"$"> はそれぞれ
112文字列中の改行の直前及び直後のマッチングでありつつ、"." は任意の文字に
844文字列中の改行の直前及び直後のマッチングでありつつ、C<"."> は任意の文字に
113845マッチングするようになります。
114846
115=item i
847=item B<C<i>>
116848X</i> X<regex, case-insensitive> X<regexp, case-insensitive>
117849X<regular expression, case-insensitive>
118850
119851=begin original
120852
121Do case-insensitive pattern matching.
853Do case-insensitive pattern matching. For example, "A" will match "a"
854under C</i>.
122855
123856=end original
124857
125858大文字小文字を区別しないパターンマッチングを行います。
859例えば、C</i> の下では "A" は "a" にマッチングします。
126860
127861=begin original
128862
129863If locale matching rules are in effect, the case map is taken from the
130864current
131865locale for code points less than 255, and from Unicode rules for larger
132866code points. However, matches that would cross the Unicode
133rules/non-Unicode rules boundary (ords 255/256) will not succeed. See
867rules/non-Unicode rules boundary (ords 255/256) will not succeed, unless
134L<perllocale>.
868the locale is a UTF-8 one. See L<perllocale>.
135869
136870=end original
137871
138872ロケールマッチングルールが有効になっている場合、符号位置 255 以下の場合は
139873現在のロケールから取られ、より大きい符号位置では Unicode ルールから
140874取られます。
141875しかし、Unicode ルールと非 Unicode ルールの境界(番号255/256) を
142またぐマッチングは成功しません。
876またぐマッチングは、ロケールが UTF-8 のものでない限り成功しません。
143877L<perllocale> を参照してください。
144878
145879=begin original
146880
147There are a number of Unicode characters that match multiple characters
881There are a number of Unicode characters that match a sequence of
148under C</i>. For example, C<LATIN SMALL LIGATURE FI>
882multiple characters under C</i>. For example,
149should match the sequence C<fi>. Perl is not
883C<LATIN SMALL LIGATURE FI> should match the sequence C<fi>. Perl is not
150884currently able to do this when the multiple characters are in the pattern and
151885are split between groupings, or when one or more are quantified. Thus
152886
153887=end original
154888
155C</i> の基で複数の文字にマッチングする Unicode 文字はたくさんあります。
889C</i> の基で複数の文字の並びにマッチングする Unicode 文字はたくさんあります。
156890例えば、C<LATIN SMALL LIGATURE FI> は並び C<fi> にマッチングするべきです。
157891複数の文字がパターン中にあってグループ化で分割されている場合、または
158892どれかの文字に量指定子が付いている場合、Perl は今のところこれを行えません。
159893従って
160894
161895=begin original
162896
163897 "\N{LATIN SMALL LIGATURE FI}" =~ /fi/i; # Matches
164898 "\N{LATIN SMALL LIGATURE FI}" =~ /[fi][fi]/i; # Doesn't match!
165899 "\N{LATIN SMALL LIGATURE FI}" =~ /fi*/i; # Doesn't match!
166900
167901=end original
168902
169903 "\N{LATIN SMALL LIGATURE FI}" =~ /fi/i; # マッチング
170904 "\N{LATIN SMALL LIGATURE FI}" =~ /[fi][fi]/i; # マッチングしない!
171905 "\N{LATIN SMALL LIGATURE FI}" =~ /fi*/i; # マッチングしない!
172906
173907=begin original
174908
175909 # The below doesn't match, and it isn't clear what $1 and $2 would
176910 # be even if it did!!
177911 "\N{LATIN SMALL LIGATURE FI}" =~ /(f)(i)/i; # Doesn't match!
178912
179913=end original
180914
181915 # 次のものはマッチングしないし、もししたとしても $1 と $2 が何になるか
182916 # はっきりしない!!
183917 "\N{LATIN SMALL LIGATURE FI}" =~ /(f)(i)/i; # マッチングしない!
184918
185919=begin original
186920
187Perl doesn't match multiple characters in an inverted bracketed
921Perl doesn't match multiple characters in a bracketed
188character class, which otherwise could be highly confusing. See
922character class unless the character that maps to them is explicitly
923mentioned, and it doesn't match them at all if the character class is
924inverted, which otherwise could be highly confusing. See
925L<perlrecharclass/Bracketed Character Classes>, and
189926L<perlrecharclass/Negation>.
190927
191928=end original
192929
193Perl は、否定大かっこ文字クラスの複数の文字マッングしません;
930Perl は、明示的にマッングについて言及されていない限り、
931大かっこ文字クラスの複数の文字にはマッチングしません;
932そして文字クラスが否定された場合はそれらには全くマッチングしません;
194933さもなければとても混乱することがあるからです。
934L<perlrecharclass/Bracketed Character Classes> と
195935L<perlrecharclass/Negation> を参照して下さい。
196936
937=item B<C<x>> and B<C<xx>>
938X</x>
939
940(B<C<x>> と B<C<xx>>)
941
197942=begin original
198943
199Another bug involves character classes that match both a sequence of
944Extend your pattern's legibility by permitting whitespace and comments.
200multiple characters, and an initial sub-string of that sequence. For
945Details in L</E<sol>x and E<sol>xx>
201example,
202946
203947=end original
204948
205一つのバグは、複数文字の並びと、並びの最初の部分文字列の両方
949空白やコメントを許可してパターンを読みやすくするように拡張します。
206マッチングする文字クラスよるものです。
950詳細は L</E<sol>x and E<sol>xx> あります。
207例えば:
208951
209 /[s\xDF]/i
952=item B<C<p>>
953X</p> X<regex, preserve> X<regexp, preserve>
210954
211955=begin original
212956
213should match both a single and a double "s", since C<\xDF> (on ASCII
957Preserve the string matched such that C<${^PREMATCH}>, C<${^MATCH}>, and
214platforms) matches "ss". However, this bug
958C<${^POSTMATCH}> are available for use after matching.
215(L<[perl #89774]|https://rt.perl.org/rt3/Ticket/Display.html?id=89774>)
216causes it to only match a single "s", even if the final larger match
217fails, and matching the double "ss" would have succeeded.
218959
219960=end original
220961
221これは "s" 一つ二つの両方にマッチングします; なぜなら
962C<${^PREMATCH}>, C<${^MATCH}>, C<${^POSTMATCH}> いったマッチングされた
222(ASCII プラットフォームでの) C<\xDF> は "ss" にマッチングからです。
963文字列をマッチングの後も使えように維持します。
223しかし、このバグ
224(L<[perl #89774]|https://rt.perl.org/rt3/Ticket/Display.html?id=89774>) は
225例え最後のより大きいマッチングが失敗し、"ss" が成功しても、
226単一の "s2 にのみマッチングするようになります。
227964
228965=begin original
229966
230Also, Perl matching doesn't fully conform to the current Unicode C</i>
967In Perl 5.20 and higher this is ignored. Due to a new copy-on-write
231recommendations, which ask that the matching be made upon the NFD
968mechanism, C<${^PREMATCH}>, C<${^MATCH}>, and C<${^POSTMATCH}> will be available
232(Normalization Form Decomposed) of the text. However, Unicode is
969after the match regardless of the modifier.
233in the process of reconsidering and revising their recommendations.
234970
235971=end original
236972
237また、Perl のマッチングは現在の Unicode の C</i> 勧告に完全に
973Perl 5.20 以降でこれは無視されます。
238準拠ません; テキスの NFD (Normalization Form Decomposed) 対しての
974しいコピーオンライ機構より、
239マッチングに関する部分です。
975C<${^PREMATCH}>, C<${^MATCH}>, and C<${^POSTMATCH}> はこの修飾子に関わらず
240しかし、Unicode は勧告再検討と改訂の作業中です。
976マッチング後も利用可能です。
241977
242=item x
978=item B<C<a>>, B<C<d>>, B<C<l>>, and B<C<u>>
243X</x>
979X</a> X</d> X</l> X</u>
244980
981(B<C<a>>, B<C<d>>, B<C<l>>, B<C<u>>)
982
245983=begin original
246984
247Extend your pattern's legibility by permitting whitespace and comments.
985These modifiers, all new in 5.14, affect which character-set rules
248Details in L</"/x">
986(Unicode, I<etc>.) are used, as described below in
987L</Character set modifiers>.
249988
250989=end original
251990
252空白やコメントを許可てパターンを読みやすくするように拡張します。
9915.14 から導入されたこれらの新い修飾子は、どの文字集合規則
253詳細は L</"/x">あります
992(Unicode など) が使われるか影響を与えます;
993L</Character set modifiers> で後述します。
254994
255=item p
995=item B<C<n>>
256X</p> X<regex, preserve> X<regexp, preserve>
996X</n> X<regex, non-capture> X<regexp, non-capture>
997X<regular expression, non-capture>
257998
258999=begin original
2591000
260Preserve the string matched such that ${^PREMATCH}, ${^MATCH}, and
1001Prevent the grouping metacharacters C<()> from capturing. This modifier,
261${^POSTMATCH} are available for use after matching.
1002new in 5.22, will stop C<$1>, C<$2>, I<etc>... from being filled in.
2621003
2631004=end original
2641005
265${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} とったマッチングされた文字列を
1006グループ化メタ文字 C<()> が捕捉しなようにします。
266マッチング後も使えように維持します。
10075.22 からこの修飾子は、C<$1>, C<$2> などを埋めのを止めます。
2671008
268=item g and c
1009 "hello" =~ /(hi|hello)/; # $1 is "hello"
269X</g> X</c>
1010 "hello" =~ /(hi|hello)/n; # $1 is undef
2701011
271(g と c)
1012=begin original
2721013
1014This is equivalent to putting C<?:> at the beginning of every capturing group:
1015
1016=end original
1017
1018これは各捕捉グループの始めに C<?:> を置くのと等価です:
1019
1020 "hello" =~ /(?:hi|hello)/; # $1 is undef
1021
2731022=begin original
2741023
275Global matching, and keep the Current position after failed matching.
1024C</n> can be negated on a per-group basis. Alternatively, named captures
276Unlike i, m, s and x, these two flags affect the way the regex is used
1025may still be used.
277rather than the regex itself. See
278L<perlretut/"Using regular expressions in Perl"> for further explanation
279of the g and c modifiers.
2801026
2811027=end original
2821028
283ローバ(Global)なマッチング、及びマッチング失敗後の現在置の保持
1029C</n> はグループ単で否定できます
284i, m, s, x とは違い、こ二つのフラグ
1030代わりに、名前付き捕捉まだ使えます。
285正規表現そのものではなく正規表現の使われ方に作用します。
286g 及び c 修飾子の詳細な説明は
287L<perlretut/"Using regular expressions in Perl"> を参照してください。
2881031
289=item a, d, l and u
1032 "hello" =~ /(?-n:(hi|hello))/n; # $1 is "hello"
290X</a> X</d> X</l> X</u>
1033 "hello" =~ /(?<greet>hi|hello)/n; # $1 is "hello", $+{greet} is
1034 # "hello"
2911035
292(a, d, l, u)
1036=item Other Modifiers
2931037
1038(その他の修飾子)
1039
2941040=begin original
2951041
296These modifiers, all new in 5.14, affect which character-set semantics
1042There are a number of flags that can be found at the end of regular
297(Unicode, etc.) are used, as described below in
1043expression constructs that are I<not> generic regular expression flags, but
298L</Character set modifiers>.
1044apply to the operation being performed, like matching or substitution (C<m//>
1045or C<s///> respectively).
2991046
3001047=end original
3011048
3025.14 から導入されたこれらの新しい修飾子は、どの文字集合意味論
1049一般的な正規表現フラグ I<でない> ですがマッチングや置換 (それぞれ C<m//>
303(Unicode など) が使われる影響を与えます;
1050 C<s///>) のような操作実行される適用される
304L</Character set modifiers> で後述します。
1051多くのフラグが正規表現構文の末尾に見つけられます。
3051052
1053=begin original
1054
1055Flags described further in
1056L<perlretut/"Using regular expressions in Perl"> are:
1057
1058=end original
1059
1060L<perlretut/"Using regular expressions in Perl"> に
1061さらに記述されているフラグは:
1062
1063 c - keep the current position during repeated matching
1064 g - globally match the pattern repeatedly in the string
1065
1066=begin original
1067
1068Substitution-specific modifiers described in
1069L<perlop/"s/PATTERN/REPLACEMENT/msixpodualngcer"> are:
1070
1071=end original
1072
1073置換専用の修飾子で
1074L<perlop/"s/PATTERN/REPLACEMENT/msixpodualngcer"> に記述されているのは:
1075
1076 e - evaluate the right-hand side as an expression
1077 ee - evaluate the right side as a string then eval the result
1078 o - pretend to optimize your code, but actually introduce bugs
1079 r - perform non-destructive substitution and return the new value
1080
3061081=back
3071082
3081083=begin original
3091084
3101085Regular expression modifiers are usually written in documentation
311as e.g., "the C</x> modifier", even though the delimiter
1086as I<e.g.>, "the C</x> modifier", even though the delimiter
312in question might not really be a slash. The modifiers C</imsxadlup>
1087in question might not really be a slash. The modifiers C</imnsxadlup>
3131088may also be embedded within the regular expression itself using
3141089the C<(?...)> construct, see L</Extended Patterns> below.
3151090
3161091=end original
3171092
3181093正規表現修飾子は文書中では通常「C</x> 修飾子」のように記述され、
3191094これは区切りが実際にはスラッシュでなくてもそう記述されます。
320また、C</imsxadlup> 修飾子は C<(?...)> 構築子を使って正規表現内に
1095また、C</imnsxadlup> 修飾子は C<(?...)> 構築子を使って正規表現内に
3211096埋め込まれることもあります; 後述する L</Extended Patterns> を
3221097参照してください。
3231098
324=head3 /x
1099=head3 Details on some modifiers
3251100
1101(一部の修飾子の詳細)
1102
3261103=begin original
3271104
328C</x> tells
1105Some of the modifiers require more explanation than given in the
1106L</Overview> above.
1107
1108=end original
1109
1110修飾子の一部は前述の L</Overview> よりもさらなる説明が必要です。
1111
1112=head4 C</x> and C</xx>
1113
1114(C</x> と C</xx>)
1115
1116=begin original
1117
1118A single C</x> tells
3291119the regular expression parser to ignore most whitespace that is neither
330backslashed nor within a character class. You can use this to break up
1120backslashed nor within a bracketed character class. You can use this to
331your regular expression into (slightly) more readable parts. The C<#>
1121break up your regular expression into more readable parts.
332character is also treated as a metacharacter introducing a comment,
1122Also, the C<"#"> character is treated as a metacharacter introducing a
333just as in ordinary Perl code. This also means that if you want real
1123comment that runs up to the pattern's closing delimiter, or to the end
334whitespace or C<#> characters in the pattern (outside a character
1124of the current line if the pattern extends onto the next line. Hence,
335class, where they are unaffected by C</x>), then you'll either have to
1125this is very much like an ordinary Perl code comment. (You can include
1126the closing delimiter within the comment only if you precede it with a
1127backslash, so be careful!)
1128
1129=end original
1130
1131単一の C</x> は、逆スラッシュでエスケープされたり大かっこ文字クラスの
1132中だったりしないほとんどの空白を無視するように正規表現パーサに伝えます。
1133これは正規表現を読みやすく部分に分割するために使えます。
1134また C<"#"> は、パターンの閉じ区切り文字まで、またはパターンが次の行に
1135続く場合は現在の行の末尾までのコメントを開始するメタ文字として扱われます。
1136従って、これは通常の Perl コードのコメントととても似ています。
1137(コメントの中の閉じ区切り文字は、逆スラッシュを前置した場合にのみ
1138含めることができます; 注意してください!)
1139
1140=begin original
1141
1142Use of C</x> means that if you want real
1143whitespace or C<"#"> characters in the pattern (outside a bracketed character
1144class, which is unaffected by C</x>), then you'll either have to
3361145escape them (using backslashes or C<\Q...\E>) or encode them using octal,
337hex, or C<\N{}> escapes. Taken together, these features go a long way towards
1146hex, or C<\N{}> escapes.
338making Perl's regular expressions more readable. Note that you have to
1147It is ineffective to try to continue a comment onto the next line by
339be careful not to include the pattern delimiter in the comment--perl has
1148escaping the C<\n> with a backslash or C<\Q>.
340no way of knowing you did not intend to close the pattern early. See
341the C-comment deletion code in L<perlop>. Also note that anything inside
1150=end original
1151
1152C</x> の使用はまた、(C</x> の影響を受けない大かっこ文字クラス内以外で)
1153パターン中に本当の空白や
1154C<"#"> 文字を使いたい場合は、(逆スラッシュや C<\Q...\E> を使って)
1155エスケープするか、8 進数、16 進数、C<\N{}> エスケープのいずれかで
1156エンコードする必要があると言うことです。
1157C<\n> を逆スラッシュや C<\Q> でエスケープすることで
1158コメントを次の行まで続けようとしても無効です。
1159
1160=begin original
1161
1162You can use L</(?#text)> to create a comment that ends earlier than the
1163end of the current line, but C<text> also can't contain the closing
1164delimiter unless escaped with a backslash.
1165
1166=end original
1167
1168現在の行の末尾より早く修了するコメントを作るために
1169L</(?#text)> が使えますが、やはり C<text> は逆スラッシュで
1170エスケープされない限り閉じ区切り文字を含むことはできません。
1171
1172=begin original
1173
1174A common pitfall is to forget that C<"#"> characters begin a comment under
1175C</x> and are not matched literally. Just keep that in mind when trying
1176to puzzle out why a particular C</x> pattern isn't working as expected.
1177
1178=end original
1179
1180よくある落とし穴は、C<"#"> 文字は C</x> の下ではコメントを始めるので、
1181文字通りにマッチしないことを忘れてしまうことです。
1182特定の C</x> パターンが期待通りに動作しない理由を解明しようとするときには、
1183このことを念頭に置いてください。
1184
1185=begin original
1186
1187Starting in Perl v5.26, if the modifier has a second C<"x"> within it,
1188it does everything that a single C</x> does, but additionally
1189non-backslashed SPACE and TAB characters within bracketed character
1190classes are also generally ignored, and hence can be added to make the
1191classes more readable.
1192
1193=end original
1194
1195Perl v5.26 以降では、修飾子に二つ目の C<"x"> が含まれている場合、
1196一つの C</x> が行うことはすべて行いますが、かっこで囲まれた
1197文字クラス内の逆スラッシュのない SPACE および TAB 文字も一般に無視されます;
1198したがって、クラスをより読みやすくするためにこれらを追加することができます。
1199
1200 / [d-e g-i 3-7]/xx
1201 /[ ! @ " # $ % ^ & * () = ? <> ' ]/xx
1202
1203=begin original
1204
1205may be easier to grasp than the squashed equivalents
1206
1207=end original
1208
1209これは、圧縮された同等物よりも掴みやすいかもしれません:
1210
1211 /[d-eg-i3-7]/
1212 /[!@"#$%^&*()=?<>']/
1213
1214=begin original
1215
1216Taken together, these features go a long way towards
1217making Perl's regular expressions more readable. Here's an example:
1218
1219=end original
1220
1221まとめると、これらの機能は Perl の正規表現をより読みやすくするために
1222大きく役立ちます。
1223以下は例です:
1224
1225 # Delete (most) C comments.
1226 $program =~ s {
1227 /\* # Match the opening delimiter.
1228 .*? # Match a minimal number of characters.
1229 \*/ # Match the closing delimiter.
1230 } []gsx;
1231
1232=begin original
1233
1234Note that anything inside
3421235a C<\Q...\E> stays unaffected by C</x>. And note that C</x> doesn't affect
3431236space interpretation within a single multi-character construct. For
3441237example in C<\x{...}>, regardless of the C</x> modifier, there can be no
3451238spaces. Same for a L<quantifier|/Quantifiers> such as C<{3}> or
346C<{5,}>. Similarly, C<(?:...)> can't have a space between the C<?> and C<:>,
1239C<{5,}>. Similarly, C<(?:...)> can't have a space between the C<"(">,
347but can between the C<(> and C<?>. Within any delimiters for such a
1240C<"?">, and C<":">. Within any delimiters for such a
3481241construct, allowed spaces are not affected by C</x>, and depend on the
3491242construct. For example, C<\x{...}> can't have spaces because hexadecimal
3501243numbers don't have spaces in them. But, Unicode properties can have spaces, so
3511244in C<\p{...}> there can be spaces that follow the Unicode rules, for which see
3521245L<perluniprops/Properties accessible through \p{} and \P{}>.
3531246X</x>
3541247
3551248=end original
3561249
357C</x> は、バックスラッシュでエスケープされたり文字クラス中だったりしない
1250C<\Q...\E> の内側のものは C</x> の影響を受けないことに注意してください。
358ほとんどの空白を無視するように正規表現パーサに伝えます。
359これは正規表現を(少し)読みやすく部分に分割するために使えます。
360また、C<#> は通常の Perl コードと同様コメントを開始するメタ文字として
361扱われます。
362これはまた、(C</x> の影響を受けない文字クラス内以外で)パターン中に本当の空白や
363C<#> 文字を使いたい場合は、(逆スラッシュや C<\Q...\E> を使って)
364エスケープするか、8 進数、16 進数、C<\N{}> エスケープのいずれかで
365エンコードする必要があると言うことです。
366まとめると、これらの機能は Perl の正規表現をより読みやすくするために
367大きく役立ちます。
368コメントにパターン区切りを含まないように注意する必要があります--perl は
369早くパターンを終了したいわけではないと言うことを知る手段がありません。
370L<perlop> の C 型式のコメントを削除するコードを参照してください。
371また、C<\Q...\E> の内側のものは C</x> の影響を受けないことにも
372注意してください。
3731251例えば、C<\x{...}> の内部では、C</x> 修飾子に関わらず、スペースを
3741252含むことはできません。
3751253C<{3}> や C<{5,}> のような L<量指定子|/Quantifiers> も同様です。
376また、C<(?:...)> も C<?> と C<:> の間にスペースを含むことはできませんが、
1254また、C<(?:...)> も
377C<(> C<?> の間に含むことできま
1255C<"(">, C<"?">, C<":"> の間にスペースを含むことできません
3781256このような構文の区切り文字の中では、スペースが許されるかどうかは
3791257C</x> に影響されず、構文自身に影響されます。
3801258例えば、16 進数はスペースを含むことができないので C<\x{...}> はスペースを
3811259含むことができません。
3821260しかし、Unicode 特性はスペースを含むことができるので、
3831261C<\p{...}> は Unicode の規則に従ってスペースを含むことができます;
3841262L<perluniprops/Properties accessible through \p{} and \P{}> を
3851263参照してください。
3861264X</x>
3871265
388=head3 Character set modifiers
1266=begin original
3891267
1268The set of characters that are deemed whitespace are those that Unicode
1269calls "Pattern White Space", namely:
1270
1271=end original
1272
1273空白と見なされる文字の集合は、Unicode が "Pattern White Space" と
1274呼ぶもので、次のものです:
1275
1276 U+0009 CHARACTER TABULATION
1277 U+000A LINE FEED
1278 U+000B LINE TABULATION
1279 U+000C FORM FEED
1280 U+000D CARRIAGE RETURN
1281 U+0020 SPACE
1282 U+0085 NEXT LINE
1283 U+200E LEFT-TO-RIGHT MARK
1284 U+200F RIGHT-TO-LEFT MARK
1285 U+2028 LINE SEPARATOR
1286 U+2029 PARAGRAPH SEPARATOR
1287
1288=head4 Character set modifiers
1289
3901290(文字集合修飾子)
3911291
3921292=begin original
3931293
3941294C</d>, C</u>, C</a>, and C</l>, available starting in 5.14, are called
395the character set modifiers; they affect the character set semantics
1295the character set modifiers; they affect the character set rules
3961296used for the regular expression.
3971297
3981298=end original
3991299
40013005.14 から利用可能な C</d>, C</u>, C</a>, C</l> は文字集合修飾子と呼ばれます;
401これらは正規表現で使われる文字集合の意味論に影響を与えます。
1301これらは正規表現で使われる文字集合規則に影響を与えます。
4021302
4031303=begin original
4041304
4051305The C</d>, C</u>, and C</l> modifiers are not likely to be of much use
4061306to you, and so you need not worry about them very much. They exist for
4071307Perl's internal use, so that complex regular expression data structures
4081308can be automatically serialized and later exactly reconstituted,
4091309including all their nuances. But, since Perl can't keep a secret, and
4101310there may be rare instances where they are useful, they are documented
4111311here.
4121312
4131313=end original
4141314
4151315C</d>, C</u>, C</l> 修飾子はよく使うことはないだろうものなので、
4161316これらについてあまり心配する必要はありません。
4171317これらは Perl の内部仕様のために存在しているので、
4181318複雑な正規表現データ構造は自動的に直列化されて、その後全てのニュアンスを
4191319含めて正確に再構成されます。
4201320
4211321=begin original
4221322
4231323The C</a> modifier, on the other hand, may be useful. Its purpose is to
4241324allow code that is to work mostly on ASCII data to not have to concern
4251325itself with Unicode.
4261326
4271327=end original
4281328
4291329一方、C</a> 修飾子は有用かもしれません。
4301330この目的は、Unicode に関して考慮する必要がないように、コードを
4311331ほとんど ASCII データとして動作するようにすることです。
4321332
4331333=begin original
4341334
4351335Briefly, C</l> sets the character set to that of whatever B<L>ocale is in
4361336effect at the time of the execution of the pattern match.
4371337
4381338=end original
4391339
4401340簡単に言うと、C</l> は、文字集合をパターンマッチングの実行時に有効な
4411341ロケール(B<L>ocale)に設定します。
4421342
4431343=begin original
4441344
4451345C</u> sets the character set to B<U>nicode.
4461346
4471347=end original
4481348
4491349C</u> は文字集合を B<U>nicode に設定します。
4501350
4511351=begin original
4521352
4531353C</a> also sets the character set to Unicode, BUT adds several
4541354restrictions for B<A>SCII-safe matching.
4551355
4561356=end original
4571357
4581358C</a> も文字コードを Unicode に設定しますが、
4591359B<A>SCII セーフなマッチングのためにいくつかの制限を加えます。
4601360
4611361=begin original
4621362
4631363C</d> is the old, problematic, pre-5.14 B<D>efault character set
4641364behavior. Its only use is to force that old behavior.
4651365
4661366=end original
4671367
4681368C</d> は古くて問題のある、5.14 以前のデフォルト(B<D>efault)文字集合の
4691369振る舞いです。
4701370これは古い振る舞いを強制するためだけに使います。
4711371
4721372=begin original
4731373
4741374At any given time, exactly one of these modifiers is in effect. Their
4751375existence allows Perl to keep the originally compiled behavior of a
4761376regular expression, regardless of what rules are in effect when it is
4771377actually executed. And if it is interpolated into a larger regex, the
478original's rules continue to apply to it, and only it.
1378original's rules continue to apply to it, and don't affect the other
1379parts.
4791380
4801381=end original
4811382
4821383任意のある瞬間において、これらの修飾子の内正確に一つだけが有効になります。
4831384これにより、
4841385それが実際に実行されるときにどの規則が有効かに関わらず、
4851386Perl が元々コンパイルされた正規表現の振る舞いを保存できるようにします。
486そしてそれがより大きな正規表現に展開された場合、元の規則だけが
1387そしてそれがより大きな正規表現に展開された場合、元の規則
487適用され続けま
1388その部分にだけ適用され続け、他の部分には影響を与えせん
4881389
4891390=begin original
4901391
4911392The C</l> and C</u> modifiers are automatically selected for
4921393regular expressions compiled within the scope of various pragmas,
4931394and we recommend that in general, you use those pragmas instead of
4941395specifying these modifiers explicitly. For one thing, the modifiers
4951396affect only pattern matching, and do not extend to even any replacement
496done, whereas using the pragmas give consistent results for all
1397done, whereas using the pragmas gives consistent results for all
4971398appropriate operations within their scopes. For example,
4981399
4991400=end original
5001401
5011402C</l> と C</u> の修飾子は、様々なプラグマのスコープ内でコンパイルされた
5021403正規表現で自動的に選択されます;
5031404一般的にはこれらの修飾子を明示的に使うのではなく、これらのプラグマを
5041405使うことを勧めます。
5051406一例を挙げると、修飾子はパターンマッチングに対してのみ影響を与え、
5061407置換には拡張されないことに注意してください;
5071408いっぽうプラグマを使うと、そのスコープ内の全ての適切な操作について
5081409一貫した結果となります。
5091410例えば:
5101411
5111412 s/foo/\Ubar/il
5121413
5131414=begin original
5141415
5151416will match "foo" using the locale's rules for case-insensitive matching,
5161417but the C</l> does not affect how the C<\U> operates. Most likely you
5171418want both of them to use locale rules. To do this, instead compile the
5181419regular expression within the scope of C<use locale>. This both
519implicitly adds the C</l> and applies locale rules to the C<\U>. The
1420implicitly adds the C</l>, and applies locale rules to the C<\U>. The
520lesson is to C<use locale> and not C</l> explicitly.
1421lesson is to C<use locale>, and not C</l> explicitly.
5211422
5221423=end original
5231424
5241425これは大文字小文字マッチングにロケールの規則を使って "foo" に
5251426マッチングしますが、C</l> は C<\U> がどう処理を行うかに影響を与えません。
5261427あなたはほぼ確実にこれら二つにロケールの規則を使うことを臨むはずです。
5271428これをするためには、代わりに
5281429C<use locale> のスコープ内で正規表現をコンパイルします。
529これらは両方とも暗黙に C</l> を追加し、C<\U> にロケールの規則を適用します。.
1430これらは両方とも暗黙に C</l> を追加し、C<\U> にロケールの規則を適用します。
5301431ここで学ぶべきことは、C<use locale> を使って、明示的に C</l> を
5311432使わないことです。
5321433
5331434=begin original
5341435
5351436Similarly, it would be better to use C<use feature 'unicode_strings'>
5361437instead of,
5371438
5381439=end original
5391440
5401441同様に、Unicode の規則にするには次のようにする代わりに
5411442C<use feature 'unicode_strings'> を使った方が良いです:
5421443
5431444 s/foo/\Lbar/iu
5441445
5451446=begin original
5461447
5471448to get Unicode rules, as the C<\L> in the former (but not necessarily
5481449the latter) would also use Unicode rules.
5491450
5501451=end original
5511452
5521453前者の C<\L> (しかし後者は必要ではないです) も Unicode の規則を
5531454使うからです。
5541455
5551456=begin original
5561457
5571458More detail on each of the modifiers follows. Most likely you don't
5581459need to know this detail for C</l>, C</u>, and C</d>, and can skip ahead
5591460to L<E<sol>a|/E<sol>a (and E<sol>aa)>.
5601461
5611462=end original
5621463
5631464それぞれの修飾子に関するさらなる詳細は後述します。
5641465ほとんど確実にあなたは C</l>, C</u>, C</d> の詳細を知る必要はなく、
5651466L<E<sol>a|/E<sol>a (and E<sol>aa)> まで読み飛ばせます。
5661467
5671468=head4 /l
5681469
5691470=begin original
5701471
5711472means to use the current locale's rules (see L<perllocale>) when pattern
5721473matching. For example, C<\w> will match the "word" characters of that
5731474locale, and C<"/i"> case-insensitive matching will match according to
5741475the locale's case folding rules. The locale used will be the one in
5751476effect at the time of execution of the pattern match. This may not be
5761477the same as the compilation-time locale, and can differ from one match
5771478to another if there is an intervening call of the
5781479L<setlocale() function|perllocale/The setlocale function>.
5791480
5801481=end original
5811482
5821483これはパターンマッチングのときに現在のロケールの規則 (L<perllocale> 参照) を
5831484使うことを意味します。
5841485例えば、C<\w> はこのロケールの「単語」文字にマッチングし、
5851486C<"/i"> の大文字小文字マッチングは、現在のロケールの大文字小文字畳み込み規則に
5861487従ってマッチングします。
5871488使われるロケールはパターンマッチングが実行される時点で有効なものです。
5881489これはコンパイル時のロケールと同じではないかもしれませんし、
5891490L<setlocale() 関数|perllocale/The setlocale function>
5901491の呼び出しが間に入ると、呼び出し毎に異なることもあります。
5911492
5921493=begin original
5931494
594Perl only supports single-byte locales. This means that code points
1495Prior to v5.20, Perl did not support multi-byte locales. Starting then,
595above 255 are treated as Unicode no matter what locale is in effect.
1496UTF-8 locales are supported. No other multi byte locales are ever
1497likely to be supported. However, in all locales, one can have code
1498points above 255 and these will always be treated as Unicode no matter
1499what locale is in effect.
1500
1501=end original
1502
1503v5.20 より前では、Perl は複数バイトロケールに対応していませんでした。
1504v5.20 から、UTF-8 ロケールに対応しました。
1505他の複数バイトロケールに対応することはおそらくありません。
1506しかし、全てのロケールで、255 を越える符号位置を保持することができ、
1507これはどのロケールが有効であるかに関わらず常に
1508Unicode として扱われます。
1509
1510=begin original
1511
5961512Under Unicode rules, there are a few case-insensitive matches that cross
597the 255/256 boundary. These are disallowed under C</l>. For example,
1513the 255/256 boundary. Except for UTF-8 locales in Perls v5.20 and
5980xFF (on ASCII platforms) does not caselessly match the character at
1514later, these are disallowed under C</l>. For example, 0xFF (on ASCII
5990x178, C<LATIN CAPITAL LETTER Y WITH DIAERESIS>, because 0xFF may not be
1515platforms) does not caselessly match the character at 0x178, C<LATIN
600C<LATIN SMALL LETTER Y WITH DIAERESIS> in the current locale, and Perl
1516CAPITAL LETTER Y WITH DIAERESIS>, because 0xFF may not be C<LATIN SMALL
601has no way of knowing if that character even exists in the locale, much
1517LETTER Y WITH DIAERESIS> in the current locale, and Perl has no way of
602less what code point it is.
1518knowing if that character even exists in the locale, much less what code
1519point it is.
6031520
6041521=end original
6051522
606Perl は単一バイトロケールのみに対応します。
607つまり、255 を越える符号位置は、どのロケールが有効であるかに関わらず
608Unicode として扱われるということです。
6091523Unicode の規則の基では、255/256 の境界をまたぐ大文字小文字を無視した
6101524マッチングがいくつかあります。
1525Perl v5.20 以降での UTF-8 ロケールを除いて、
6111526これらは C</l> の基では認められません。
6121527例えば、(ASCII プラットフォームで) 0xFF は 0x178,
6131528C<LATIN CAPITAL LETTER Y WITH DIAERESIS> と大文字小文字を無視した
6141529マッチングをしません; なぜなら 0xFF は現在のロケールでは
6151530C<LATIN SMALL LETTER Y WITH DIAERESIS> ではないかもしれず、Perl は
6161531このロケールでこの文字があるかどうかすら知る方法がなく、ましてや
6171532これがどの符号位置かを知る方法もないからです。
6181533
6191534=begin original
6201535
1536In a UTF-8 locale in v5.20 and later, the only visible difference
1537between locale and non-locale in regular expressions should be tainting
1538(see L<perlsec>).
1539
1540=end original
1541
1542v5.20 以降の UTF-8 ロケールでは、
1543正規表現でのロケールと非ロケールの目に見える唯一の違いは
1544汚染だけのはずです
1545(L<perlsec> 参照)。
1546
1547=begin original
1548
6211549This modifier may be specified to be the default by C<use locale>, but
6221550see L</Which character set modifier is in effect?>.
6231551X</l>
6241552
6251553=end original
6261554
6271555この修飾子は C<use locale> によってデフォルトで設定されますが、
6281556L</Which character set modifier is in effect?> を参照してください。
6291557X</l>
6301558
6311559=head4 /u
6321560
6331561=begin original
6341562
6351563means to use Unicode rules when pattern matching. On ASCII platforms,
6361564this means that the code points between 128 and 255 take on their
6371565Latin-1 (ISO-8859-1) meanings (which are the same as Unicode's).
6381566(Otherwise Perl considers their meanings to be undefined.) Thus,
6391567under this modifier, the ASCII platform effectively becomes a Unicode
6401568platform; and hence, for example, C<\w> will match any of the more than
6411569100_000 word characters in Unicode.
6421570
6431571=end original
6441572
6451573これはパターンマッチングのときに Unicode の規則を使うことを意味します。
6461574ASCII プラットフォームでは、これは符号位置 128 から 255 は
6471575Latin-1 (ISO-8859-1) という意味になります (これは Unicode と同じです)。
6481576(さもなければ Perl はこれらの意味は未定義として扱います。)
6491577従って、この修飾子の基では、ASCII プラットフォームは
6501578事実上 Unicode プラットフォームになります; 従って、
6511579例えば、C<\w> は Unicode の 100,000 以上の単語文字のどれにも
6521580マッチングします。
6531581
6541582=begin original
6551583
6561584Unlike most locales, which are specific to a language and country pair,
6571585Unicode classifies all the characters that are letters I<somewhere> in
6581586the world as
6591587C<\w>. For example, your locale might not think that C<LATIN SMALL
6601588LETTER ETH> is a letter (unless you happen to speak Icelandic), but
6611589Unicode does. Similarly, all the characters that are decimal digits
6621590somewhere in the world will match C<\d>; this is hundreds, not 10,
6631591possible matches. And some of those digits look like some of the 10
6641592ASCII digits, but mean a different number, so a human could easily think
6651593a number is a different quantity than it really is. For example,
6661594C<BENGALI DIGIT FOUR> (U+09EA) looks very much like an
667C<ASCII DIGIT EIGHT> (U+0038). And, C<\d+>, may match strings of digits
1595C<ASCII DIGIT EIGHT> (U+0038), and C<LEPCHA DIGIT SIX> (U+1C46) looks
668that are a mixture from different writing systems, creating a security
1596very much like an C<ASCII DIGIT FIVE> (U+0035). And, C<\d+>, may match
669issue. L<Unicode::UCD/num()> can be used to sort
1597strings of digits that are a mixture from different writing systems,
670this out. Or the C</a> modifier can be used to force C<\d> to match
1598creating a security issue. A fraudulent website, for example, could
671just the ASCII 0 through 9.
1599display the price of something using U+1C46, and it would appear to the
1600user that something cost 500 units, but it really costs 600. A browser
1601that enforced script runs (L</Script Runs>) would prevent that
1602fraudulent display. L<Unicode::UCD/num()> can also be used to sort this
1603out. Or the C</a> modifier can be used to force C<\d> to match just the
1604ASCII 0 through 9.
6721605
6731606=end original
6741607
6751608特定の言語と国に固有であるほとんどのロケールと異なり、
6761609Unicode は世界の I<どこか> で字(letter)として扱われている全ての
6771610文字(character)を C<\w> に分類します。
6781611例えば、あなたのロケールは (あなたがたまたまアイスランド語を話さない限り)
6791612C<LATIN SMALL LETTER ETH> を字として考えないかもしれません。
6801613同様に、世界のどこかで数字である全ての文字は C<\d> にマッチングします;
6811614これは 10 ではなく 100 のマッチングの可能性があります。
6821615さらにこれらの数字の一部は 10 の ASCII 数字と似ていますが、
6831616異なる数字を意味するため、人間はその数字が実際と異なる量であると
6841617簡単に考えてしまいます。
6851618例えば、 C<BENGALI DIGIT FOUR> (U+09EA) は C<ASCII DIGIT EIGHT> (U+0038) に
1619とてもよく似ていて、
1620C<LEPCHA DIGIT SIX> (U+1C46) は C<ASCII DIGIT FIVE> (U+0035) に
6861621とてもよく似ています。
6871622C<\d+> は、異なる記法から混ぜた数字の文字列にマッチングするので、
6881623セキュリティ上の問題を作ります。
689これを整理するために L<Unicode::UCD/num()> 使われます。
1624例えば、詐欺サイトは、何か U+1C46 使った価格を表示し、
1625何か 500 ユニットのコストであるかのようにユーザーに見えるけれども、
1626実際のコストは 600 にできます。
1627用字連続 (L</Script Runs>) を矯正するブラウザはこのような
1628詐欺的な表示を防ぎます。
1629これを整理するために L<Unicode::UCD/num()> も使えます。
6901630あるいは C</a> 修飾子は、C<\d> が単に ASCII の 0 から 9 に
6911631マッチングすることを強制するために使えます。
6921632
6931633=begin original
6941634
6951635Also, under this modifier, case-insensitive matching works on the full
6961636set of Unicode
6971637characters. The C<KELVIN SIGN>, for example matches the letters "k" and
6981638"K"; and C<LATIN SMALL LIGATURE FF> matches the sequence "ff", which,
6991639if you're not prepared, might make it look like a hexadecimal constant,
7001640presenting another potential security issue. See
7011641L<http://unicode.org/reports/tr36> for a detailed discussion of Unicode
7021642security issues.
7031643
7041644=end original
7051645
7061646また、この修飾子の基では、大文字小文字を無視したマッチングは Unicode の
7071647完全な集合で動作します。
7081648例えば C<KELVIN SIGN> は "k" と "K" にマッチングします;
7091649C<LATIN SMALL LIGATURE FF> は、準備していなければ 16 進数定数のように
7101650見えるかもしれない並び "ff" にマッチングし、もう一つの潜在的な
7111651セキュリティ問題になります。
7121652Unicode のセキュリティ問題に関する詳細な議論については
7131653L<http://unicode.org/reports/tr36> を参照してください。
7141654
7151655=begin original
7161656
717On the EBCDIC platforms that Perl handles, the native character set is
718equivalent to Latin-1. Thus this modifier changes behavior only when
719the C<"/i"> modifier is also specified, and it turns out it affects only
720two characters, giving them full Unicode semantics: the C<MICRO SIGN>
721will match the Greek capital and small letters C<MU>, otherwise not; and
722the C<LATIN CAPITAL LETTER SHARP S> will match any of C<SS>, C<Ss>,
723C<sS>, and C<ss>, otherwise not.
724
725=end original
726
727Perl が扱える EBCDIC プラットフォームでは、ネイティブな文字集合は
728Latin-1 と等価です。
729従ってこの修飾子は、C<"/i"> 修飾子も指定されたときにのみ
730振る舞いを変え、結果として二つの文字にだけ影響を与え、
731完全な Unicode の意味論を与えます:
732C<MICRO SIGN> はギリシャ語の大文字と小文字の C<MU> にマッチングし、
733それ以外はマッチングしません;
734また C<LATIN CAPITAL LETTER SHARP S> は C<SS>, C<Ss>,
735C<sS>, C<ss> のいずれかにはマッチングし、それ以外にはマッチングしません。
736
737=begin original
738
7391657This modifier may be specified to be the default by C<use feature
7401658'unicode_strings>, C<use locale ':not_characters'>, or
7411659C<L<use 5.012|perlfunc/use VERSION>> (or higher),
7421660but see L</Which character set modifier is in effect?>.
7431661X</u>
7441662
7451663=end original
7461664
7471665この修飾子は C<use feature 'unicode_strings>,
7481666C<use locale ':not_characters'>, C<L<use 5.012|perlfunc/use VERSION>>
7491667(またはそれ以上) によってデフォルトに
7501668設定されますが、L</Which character set modifier is in effect?> を
7511669参照してください。
7521670X</u>
7531671
7541672=head4 /d
7551673
7561674=begin original
7571675
7581676This modifier means to use the "Default" native rules of the platform
7591677except when there is cause to use Unicode rules instead, as follows:
7601678
7611679=end original
7621680
7631681この修飾子は、以下のように Unicode の規則が使われる場合を除いて、
7641682プラットフォームの「デフォルトの」(Default) ネイティブな規則を使うことを
7651683意味します:
7661684
7671685=over 4
7681686
7691687=item 1
7701688
7711689=begin original
7721690
7731691the target string is encoded in UTF-8; or
7741692
7751693=end original
7761694
7771695ターゲット文字列が UTF-8 でエンコードされている; または
7781696
7791697=item 2
7801698
7811699=begin original
7821700
7831701the pattern is encoded in UTF-8; or
7841702
7851703=end original
7861704
7871705パターンが UTF-8 でエンコードされている; または
7881706
7891707=item 3
7901708
7911709=begin original
7921710
7931711the pattern explicitly mentions a code point that is above 255 (say by
7941712C<\x{100}>); or
7951713
7961714=end original
7971715
7981716パターンが、(C<\x{100}> のような形で)255 を超える符号位置に明示的に
7991717言及している; または
8001718
8011719=item 4
8021720
8031721=begin original
8041722
8051723the pattern uses a Unicode name (C<\N{...}>); or
8061724
8071725=end original
8081726
8091727パターンが Unicode 名 (C<\N{...}>) を使っている; または
8101728
8111729=item 5
8121730
8131731=begin original
8141732
815the pattern uses a Unicode property (C<\p{...}>)
1733the pattern uses a Unicode property (C<\p{...}> or C<\P{...}>); or
8161734
8171735=end original
8181736
819パターンが Unicode 特性 (C<\p{...}>) を使っている
1737パターンが Unicode 特性 (C<\p{...}>) や C<\P{...}>) を使っている; または
8201738
1739=item 6
1740
1741=begin original
1742
1743the pattern uses a Unicode break (C<\b{...}> or C<\B{...}>); or
1744
1745=end original
1746
1747パターンが Unicode 単語境界 (C<\b{...}> または C<\B{...}>) を使っている;
1748または
1749
1750=item 7
1751
1752=begin original
1753
1754the pattern uses L</C<(?[ ])>>
1755
1756=end original
1757
1758パターンが L</C<(?[ ])>> を使っている
1759
1760=item 8
1761
1762=begin original
1763
1764the pattern uses L<C<(*script_run: ...)>|/Script Runs>
1765
1766=end original
1767
1768パターンが L<C<(*script_run: ...)>|/Script Runs> を使っている
1769
8211770=back
8221771
8231772=begin original
8241773
8251774Another mnemonic for this modifier is "Depends", as the rules actually
8261775used depend on various things, and as a result you can get unexpected
8271776results. See L<perlunicode/The "Unicode Bug">. The Unicode Bug has
828become rather infamous, leading to yet another (printable) name for this
1777become rather infamous, leading to yet another (without swearing) name
829modifier, "Dodgy".
1778for this modifier, "Dodgy".
8301779
8311780=end original
8321781
8331782この修飾子のもう一つの記憶法は「依存」(Depends)です; 規則は実際には
8341783様々なことに依存していること、また結果として予想外の
8351784結果になるかもしれないからです。
8361785L<perlunicode/The "Unicode Bug"> を参照してください。
837Unicode バグは、悪名高くなり、この修飾子のもう一つの(表示可能な)
1786Unicode バグは、悪名高くなり、この修飾子のもう一つの(罵りの)
8381787名前 "Dodgy" を引き起こしています。
8391788
8401789=begin original
8411790
842On ASCII platforms, the native rules are ASCII, and on EBCDIC platforms
1791Unless the pattern or string are encoded in UTF-8, only ASCII characters
843(at least the ones that Perl handles), they are Latin-1.
1792can match positively.
8441793
8451794=end original
8461795
847ASCII プラットフォムでは、ネイティブな規則は ASCII、(少くとも
1796パタンや文字列が UTF-8エンコードされていい限り、
848Perl が扱う) EBCDIC プラトフォームでは、これは Latin-1 です。
1797ASCII 文字のみが肯定的にマチングします。
8491798
8501799=begin original
8511800
8521801Here are some examples of how that works on an ASCII platform:
8531802
8541803=end original
8551804
8561805以下は ASCII プラットフォームでどのように動作するかの例です:
8571806
8581807 $str = "\xDF"; # $str is not in UTF-8 format.
8591808 $str =~ /^\w/; # No match, as $str isn't in UTF-8 format.
8601809 $str .= "\x{0e0b}"; # Now $str is in UTF-8 format.
8611810 $str =~ /^\w/; # Match! $str is now in UTF-8 format.
8621811 chop $str;
8631812 $str =~ /^\w/; # Still a match! $str remains in UTF-8 format.
8641813
8651814=begin original
8661815
8671816This modifier is automatically selected by default when none of the
8681817others are, so yet another name for it is "Default".
8691818
8701819=end original
8711820
8721821この修飾子は他のものが指定されなかった場合にデフォルトとして自動的に
8731822洗濯されるので、これのもう一つの名前は "Default" です。
8741823
8751824=begin original
8761825
8771826Because of the unexpected behaviors associated with this modifier, you
878probably should only use it to maintain weird backward compatibilities.
1827probably should only explicitly use it to maintain weird backward
1828compatibilities.
8791829
8801830=end original
8811831
8821832この修飾子に関する想定外の振る舞いにより、おそらくおかしな後方互換性を
883維持するためだけにこれを使うべきでしょう。
1833維持するためだけにこれを明示的に使うべきでしょう。
8841834
8851835=head4 /a (and /aa)
8861836
8871837(/a (と /aa))
8881838
8891839=begin original
8901840
891This modifier stands for ASCII-restrict (or ASCII-safe). This modifier,
1841This modifier stands for ASCII-restrict (or ASCII-safe). This modifier
892unlike the others, may be doubled-up to increase its effect.
1842may be doubled-up to increase its effect.
8931843
8941844=end original
8951845
8961846この修飾子は ASCII 制限 (あるいは ASCII セーフ) を意味します。
897この修飾子は、他のものと異なり、2 倍にすることで効果が増します。
1847この修飾子は、2 倍にすることで効果が増します。
8981848
8991849=begin original
9001850
9011851When it appears singly, it causes the sequences C<\d>, C<\s>, C<\w>, and
9021852the Posix character classes to match only in the ASCII range. They thus
9031853revert to their pre-5.6, pre-Unicode meanings. Under C</a>, C<\d>
9041854always means precisely the digits C<"0"> to C<"9">; C<\s> means the five
905characters C<[ \f\n\r\t]>; C<\w> means the 63 characters
1855characters C<[ \f\n\r\t]>, and starting in Perl v5.18, the vertical tab;
1856C<\w> means the 63 characters
9061857C<[A-Za-z0-9_]>; and likewise, all the Posix classes such as
9071858C<[[:print:]]> match only the appropriate ASCII-range characters.
9081859
9091860=end original
9101861
9111862これが単体で使われると、C<\d>, C<\s>, C<\w>, Posix 文字クラスは
9121863ASCII の範囲のみにマッチングするようになります。
9131864従って、これらは 5.6 以前の、Unicode 以前の意味に戻します。
9141865C</a> の基では、C<\d> は常に正確に数字 C<"0"> から C<"9"> を意味します;
915C<\s> は C<[ \f\n\r\t]> の 5 文字を意味します;
1866C<\s> は C<[ \f\n\r\t]> の 5 文字、および Perl v5.18 から垂直タブ、
1867を意味します;
9161868C<\w> は C<[A-Za-z0-9_]> の 63 文字を意味します;
9171869同様に、C<[[:print:]]> のような全ての Posix クラスは
9181870適切な ASCII の範囲の文字にのみマッチングします。
9191871
9201872=begin original
9211873
9221874This modifier is useful for people who only incidentally use Unicode,
9231875and who do not wish to be burdened with its complexities and security
9241876concerns.
9251877
9261878=end original
9271879
9281880この修飾子は、偶然 Unicode を使っている人々で、その複雑さと
9291881セキュリティの問題に関する重荷を背負いたくない人々にとっては有用です。
9301882
9311883=begin original
9321884
9331885With C</a>, one can write C<\d> with confidence that it will only match
9341886ASCII characters, and should the need arise to match beyond ASCII, you
9351887can instead use C<\p{Digit}> (or C<\p{Word}> for C<\w>). There are
9361888similar C<\p{...}> constructs that can match beyond ASCII both white
9371889space (see L<perlrecharclass/Whitespace>), and Posix classes (see
9381890L<perlrecharclass/POSIX Character Classes>). Thus, this modifier
9391891doesn't mean you can't use Unicode, it means that to get Unicode
9401892matching you must explicitly use a construct (C<\p{}>, C<\P{}>) that
9411893signals Unicode.
9421894
9431895=end original
9441896
9451897C</a> を使うと、ASCII 文字だけにマッチングすることに自信を持って
9461898書くことができ、ASCII を超えてマッチングする必要が発生したときには、
9471899代わりに C<\p{Digit}> (または C<\w> として C<\p{Word}>) が使えます。
9481900ASCII を超えたスペース (L<perlrecharclass/Whitespace> を参照してください)
9491901と Posix クラス (L<perlrecharclass/POSIX Character Classes> を
9501902参照してください) の両方にマッチングする似たような C<\p{...}> 構文があります。
9511903従って、この修飾子は Unicode が使えなくなるということではなく、
9521904Unicode のマッチングには明示的に Unicode を意味する構文
9531905(C<\p{}>, C<\P{}>) を使わないといけないということです。
9541906
9551907=begin original
9561908
9571909As you would expect, this modifier causes, for example, C<\D> to mean
9581910the same thing as C<[^0-9]>; in fact, all non-ASCII characters match
9591911C<\D>, C<\S>, and C<\W>. C<\b> still means to match at the boundary
9601912between C<\w> and C<\W>, using the C</a> definitions of them (similarly
9611913for C<\B>).
9621914
9631915=end original
9641916
9651917予想できるとおり、 この修飾子は、例えば、C<\D> を C<[^0-9]> と
9661918同じことにします;
9671919実際、全ての非 ASCII 文字は C<\D>, C<\S>, C<\W> にマッチングします。
9681920C<\b> はまだ C<\w> と C<\W> の境界にマッチングします;
9691921これらのために (C<\B> と同様) C</a> の定義を使います。
9701922
9711923=begin original
9721924
9731925Otherwise, C</a> behaves like the C</u> modifier, in that
974case-insensitive matching uses Unicode semantics; for example, "k" will
1926case-insensitive matching uses Unicode rules; for example, "k" will
9751927match the Unicode C<\N{KELVIN SIGN}> under C</i> matching, and code
9761928points in the Latin1 range, above ASCII will have Unicode rules when it
9771929comes to case-insensitive matching.
9781930
9791931=end original
9801932
9811933さもなければ、C</a> は C</u> 修飾子のように振る舞います;
982大文字小文字を無視したマッチングには Unicode の意味論を使います;
1934大文字小文字を無視したマッチングには Unicode の規則を使います;
9831935例えば、"k" は C</i> の基では C<\N{KELVIN SIGN}> にマッチングし、
9841936ASCII の範囲を超える Latin1 の範囲の符号位置は、大文字小文字を無視した
9851937マッチングで使われる場合は Unicode の規則を使います。
9861938
9871939=begin original
9881940
9891941To forbid ASCII/non-ASCII matches (like "k" with C<\N{KELVIN SIGN}>),
990specify the "a" twice, for example C</aai> or C</aia>. (The first
1942specify the C<"a"> twice, for example C</aai> or C</aia>. (The first
991occurrence of "a" restricts the C<\d>, etc., and the second occurrence
1943occurrence of C<"a"> restricts the C<\d>, I<etc>., and the second occurrence
9921944adds the C</i> restrictions.) But, note that code points outside the
9931945ASCII range will use Unicode rules for C</i> matching, so the modifier
9941946doesn't really restrict things to just ASCII; it just forbids the
9951947intermixing of ASCII and non-ASCII.
9961948
9971949=end original
9981950
9991951("k" と C<\N{KELVIN SIGN}> のような) ASCII/非-ASCII マッチングを禁止するには、
1000C</aai> や C</aia> のように "a" を 2 回指定します。
1952C</aai> や C</aia> のように C<"a"> を 2 回指定します。
1001(最初の "a" は C<\d> などを制限し、2 番目は C</i> の制限を追加します。)
1953(最初の C<"a"> は C<\d> などを制限し、2 番目は C</i> の制限を追加します。)
10021954しかし、 ASCII の範囲外の符号位置は C</i> マッチングに Unicode 規則を
10031955使うので、この修飾子は実際には単に ASCII に制限するものではないことに
10041956注意してください; これは単に ASCII と非 ASCII を混ぜることを禁止します。
10051957
10061958=begin original
10071959
10081960To summarize, this modifier provides protection for applications that
10091961don't wish to be exposed to all of Unicode. Specifying it twice
10101962gives added protection.
10111963
10121964=end original
10131965
10141966まとめると、この修飾子は全ての Unicode に対して曝されることを望んでいない
10151967アプリケーションに対する保護を提供します。
101619682 回指定することで追加の保護を提供します。
10171969
10181970=begin original
10191971
10201972This modifier may be specified to be the default by C<use re '/a'>
10211973or C<use re '/aa'>. If you do so, you may actually have occasion to use
1022the C</u> modifier explictly if there are a few regular expressions
1974the C</u> modifier explicitly if there are a few regular expressions
10231975where you do want full Unicode rules (but even here, it's best if
10241976everything were under feature C<"unicode_strings">, along with the
10251977C<use re '/aa'>). Also see L</Which character set modifier is in
10261978effect?>.
10271979X</a>
10281980X</aa>
10291981
10301982=end original
10311983
10321984この修飾子は C<use re '/a'> または C<use re '/aa'> でデフォルトに
10331985設定されます。
10341986そうすると、もし完全な Unicode 規則を使いたい正規表現がある場合は、
10351987C</u> 修飾子を明示的に使う機会があるかもしれません
10361988(その場合でも、全てが C<"unicode_strings"> の基なら、
10371989C<use re '/aa'> と共にするのが最良です)。
10381990L</Which character set modifier is in effect?> も参照してください。
10391991X</a>
10401992X</aa>
10411993
10421994=head4 Which character set modifier is in effect?
10431995
10441996(どの文字集合修飾子が有効?)
10451997
10461998=begin original
10471999
10482000Which of these modifiers is in effect at any given point in a regular
10492001expression depends on a fairly complex set of interactions. These have
10502002been designed so that in general you don't have to worry about it, but
10512003this section gives the gory details. As
10522004explained below in L</Extended Patterns> it is possible to explicitly
10532005specify modifiers that apply only to portions of a regular expression.
10542006The innermost always has priority over any outer ones, and one applying
10552007to the whole expression has priority over any of the default settings that are
10562008described in the remainder of this section.
10572009
10582010=end original
10592011
10602012ある正規表現のあるポイントでどの修飾子が有効かは、かなり複雑な相互作用に
10612013依存します。
10622014これらは、基本的にはあなたがこれらについて心配しなくて良いように
10632015設計されています。
10642016しかし、この節は詳細を記述します。
10652017L</Extended Patterns> で後述するとおり、正規表現の一部にだけ
10662018適用する修飾子を明示的に指定することが可能です。
10672019一番内側のものは常により外側のものより優先され、式全体に適用されるものは
10682020この節の残りで記述されるデフォルト設定より優先されます。
10692021
10702022=begin original
10712023
10722024The C<L<use re 'E<sol>foo'|re/"'/flags' mode">> pragma can be used to set
10732025default modifiers (including these) for regular expressions compiled
10742026within its scope. This pragma has precedence over the other pragmas
10752027listed below that also change the defaults.
10762028
10772029=end original
10782030
10792031C<L<use re 'E<sol>foo'|re/"'/flags' mode">> プラグマは、
10802032このスコープ内でコンパイルされる正規表現に対して(これらを含む)
10812033デフォルトの修飾子を設定するのに使えます。
10822034このプラグマは、デフォルトを変更する後述するその他のプラグマに優先します。
10832035
10842036=begin original
10852037
10862038Otherwise, C<L<use locale|perllocale>> sets the default modifier to C</l>;
10872039and C<L<use feature 'unicode_strings|feature>>, or
10882040C<L<use 5.012|perlfunc/use VERSION>> (or higher) set the default to
10892041C</u> when not in the same scope as either C<L<use locale|perllocale>>
10902042or C<L<use bytes|bytes>>.
10912043(C<L<use locale ':not_characters'|perllocale/Unicode and UTF-8>> also
10922044sets the default to C</u>, overriding any plain C<use locale>.)
10932045Unlike the mechanisms mentioned above, these
10942046affect operations besides regular expressions pattern matching, and so
10952047give more consistent results with other operators, including using
1096C<\U>, C<\l>, etc. in substitution replacements.
2048C<\U>, C<\l>, I<etc>. in substitution replacements.
10972049
10982050=end original
10992051
11002052さもなければ、C<L<use locale|perllocale>> はデフォルト修飾子を C</l> に
11012053設定します; そして、C<L<use feature 'unicode_strings|feature>> か
11022054C<L<use 5.012|perlfunc/use VERSION>> (またはそれ以上) は、
11032055同じスコープに C<L<use locale|perllocale>> や C<L<use bytes|bytes>> が
11042056なければ、デフォルトを C</u> に設定します。
11052057(C<L<use locale ':not_characters'|perllocale/Unicode and UTF-8>> はまた
11062058デフォルトを C</u> に設定し、普通の C<use locale> を上書きします。)
11072059前述した機構と異なり、これらは正規表現パターンマッチング以外の操作に
11082060影響するので、置換での C<\U>, C<\l> を使うことを含むその他の操作と
11092061より一貫性のある結果になります。
11102062
11112063=begin original
11122064
11132065If none of the above apply, for backwards compatibility reasons, the
11142066C</d> modifier is the one in effect by default. As this can lead to
11152067unexpected results, it is best to specify which other rule set should be
11162068used.
11172069
11182070=end original
11192071
11202072前述のどれも適用されない場合、後方互換性のために、
11212073C</d> 修飾子がデフォルトで有効になります。
11222074これは想定外の結果になることがあるので、
11232075その他の規則集合が使われるように指定するのが最良です。
11242076
11252077=head4 Character set modifier behavior prior to Perl 5.14
11262078
11272079(Perl 5.14 より前の文字集合修飾子の振る舞い)
11282080
11292081=begin original
11302082
11312083Prior to 5.14, there were no explicit modifiers, but C</l> was implied
11322084for regexes compiled within the scope of C<use locale>, and C</d> was
11332085implied otherwise. However, interpolating a regex into a larger regex
11342086would ignore the original compilation in favor of whatever was in effect
11352087at the time of the second compilation. There were a number of
11362088inconsistencies (bugs) with the C</d> modifier, where Unicode rules
11372089would be used when inappropriate, and vice versa. C<\p{}> did not imply
11382090Unicode rules, and neither did all occurrences of C<\N{}>, until 5.12.
11392091
11402092=end original
11412093
114220945.14 より前では、明示的な修飾子はありませんが、
11432095C<use locale> のスコープ内でコンパイルされた正規表現に関しては
11442096C</l> が仮定され、さもなければ C</d> が仮定されます。
11452097しかし、ある正規表現をより大きな正規表現に展開した場合、元のコンパイル時の
11462098状況は、2 回目のコンパイル時点で有効なもので上書きされます。
11472099C</d> 演算子には、不適切なときに Unicode 規則が使われる、あるいはその逆の
11482100多くの非一貫性(バグ)があります。
11492101C<\p{}> および C<\N{}> は 5.12 まで Unicode 規則を仮定していません。
11502102
11512103=head2 Regular Expressions
11522104
11532105(正規表現)
11542106
1155=head3 Metacharacters
1156
1157(メタ文字)
1158
1159=begin original
1160
1161The patterns used in Perl pattern matching evolved from those supplied in
1162the Version 8 regex routines. (The routines are derived
1163(distantly) from Henry Spencer's freely redistributable reimplementation
1164of the V8 routines.) See L<Version 8 Regular Expressions> for
1165details.
1166
1167=end original
1168
1169Perl のパターンマッチングで使われるパターンは Version 8 正規表現ルーチンで
1170提供されているものからの派生です。
1171(このルーチンは Henry Spencer の自由に再配布可能な V8 ルーチンの再実装から
1172(遠く)派生しています)。
1173詳細は L<Version 8 Regular Expressions> を参照してください。
1174
1175=begin original
1176
1177In particular the following metacharacters have their standard I<egrep>-ish
1178meanings:
1179X<metacharacter>
1180X<\> X<^> X<.> X<$> X<|> X<(> X<()> X<[> X<[]>
1181
1182=end original
1183
1184特に以下のメタ文字は標準の I<egrep> 風の意味を持っています:
1185X<metacharacter>
1186X<\> X<^> X<.> X<$> X<|> X<(> X<()> X<[> X<[]>
1187
1188=begin original
1189
1190 \ Quote the next metacharacter
1191 ^ Match the beginning of the line
1192 . Match any character (except newline)
1193 $ Match the end of the line (or before newline at the end)
1194 | Alternation
1195 () Grouping
1196 [] Bracketed Character class
1197
1198=end original
1199
1200 \ 次のメタ文字をエスケープ
1201 ^ 行の先頭にマッチング
1202 . 任意の文字にマッチング(但し改行は除く)
1203 $ 行の終端にマッチング(または終端の改行の前)
1204 | 代替
1205 () グループ化
1206 [] 文字クラス
1207
1208=begin original
1209
1210By default, the "^" character is guaranteed to match only the
1211beginning of the string, the "$" character only the end (or before the
1212newline at the end), and Perl does certain optimizations with the
1213assumption that the string contains only one line. Embedded newlines
1214will not be matched by "^" or "$". You may, however, wish to treat a
1215string as a multi-line buffer, such that the "^" will match after any
1216newline within the string (except if the newline is the last character in
1217the string), and "$" will match before any newline. At the
1218cost of a little more overhead, you can do this by using the /m modifier
1219on the pattern match operator. (Older programs did this by setting C<$*>,
1220but this option was removed in perl 5.9.)
1221X<^> X<$> X</m>
1222
1223=end original
1224
1225デフォルトでは、文字 "^" は文字列の先頭にのみ、そして文字 "$" は
1226末尾(または末尾の改行の前)にのみマッチングすることを保証し、そして Perl は
1227文字列が 1 行のみを含んでいるという仮定でいくつかの最適化を行います。
1228埋め込まれている改行文字は "^" や "$" とはマッチングしません。
1229しかし文字列には複数行が格納されていて、"^" は任意の改行の後(但し
1230改行文字が文字列の最後の文字だった場合は除く)、そして "$" は任意の改行の前で
1231マッチングさせたいこともあるでしょう。
1232小さなオーバーヘッドはありますが、これはパターンマッチングで /m 修飾子を
1233使うことで行うことができます。
1234(古いプログラムでは C<$*> を設定することでこれを行っていましたが
1235これは perl 5.9 では削除されています。)
1236X<^> X<$> X</m>
1237
1238=begin original
1239
1240To simplify multi-line substitutions, the "." character never matches a
1241newline unless you use the C</s> modifier, which in effect tells Perl to pretend
1242the string is a single line--even if it isn't.
1243X<.> X</s>
1244
1245=end original
1246
1247複数行での利用を簡単にするために、文字 "." は C</s> 修飾子を
1248使って Perl に文字列を 1 行として処理すると伝えない限り
1249改行にはマッチングしません。
1250X<.> X</s>
1251
12522107=head3 Quantifiers
12532108
12542109(量指定子)
12552110
12562111=begin original
12572112
1258The following standard quantifiers are recognized:
2113Quantifiers are used when a particular portion of a pattern needs to
2114match a certain number (or numbers) of times. If there isn't a
2115quantifier the number of times to match is exactly one. The following
2116standard quantifiers are recognized:
12592117X<metacharacter> X<quantifier> X<*> X<+> X<?> X<{n}> X<{n,}> X<{n,m}>
12602118
12612119=end original
12622120
2121Quantifiers are used when a particular portion of a pattern needs to
2122match a certain number (or numbers) of times. If there isn't a
2123quantifier the number of times to match is exactly one.
12632124以下の標準的な量指定子を使えます:
12642125X<metacharacter> X<quantifier> X<*> X<+> X<?> X<{n}> X<{n,}> X<{n,m}>
12652126
12662127=begin original
12672128
12682129 * Match 0 or more times
12692130 + Match 1 or more times
12702131 ? Match 1 or 0 times
12712132 {n} Match exactly n times
12722133 {n,} Match at least n times
12732134 {n,m} Match at least n but not more than m times
12742135
12752136=end original
12762137
12772138 * 0 回以上マッチング
12782139 + 1 回以上マッチング
12792140 ? 0 回または 1 回マッチング
12802141 {n} 正確に n 回マッチング
12812142 {n,} 最低 n 回マッチング
12822143 {n,m} n 回以上 m 回以下マッチング
12832144
12842145=begin original
12852146
1286(If a curly bracket occurs in any other context and does not form part of
2147(If a non-escaped curly bracket occurs in a context other than one of
1287a backslashed sequence like C<\x{...}>, it is treated
2148the quantifiers listed above, where it does not form part of a
1288as a regular character. In particular, the lower quantifier bound
2149backslashed sequence like C<\x{...}>, it is either a fatal syntax error,
1289is not optional. However, in Perl v5.18, it is planned to issue a
2150or treated as a regular character, generally with a deprecation warning
1290deprecation warning for all such occurrences, and in Perl v5.20 to
2151raised. To escape it, you can precede it with a backslash (C<"\{">) or
1291require literal uses of a curly bracket to be escaped, say by preceding
2152enclose it within square brackets (C<"[{]">).
1292them with a backslash or enclosing them within square brackets, (C<"\{">
2153This change will allow for future syntax extensions (like making the
1293or C<"[{]">). This change will allow for future syntax extensions (like
2154lower bound of a quantifier optional), and better error checking of
1294making the lower bound of a quantifier optional), and better error
2155quantifiers).
1295checking of quantifiers. Now, a typo in a quantifier silently causes
1296it to be treated as the literal characters. For example,
12972156
1298
12992157=end original
13002158
1301(これ以外のコンテキストで中かっこが使われて、C<\x{...}> のような
2159(前述した量指定子の一つ以外のコンテキストでエスケープされない中かっこが
1302バックスラッシュ付き並びの一部ではないときには、普通の文字として
2160使われて、C<\x{...}> のような逆スラッシュ付き並びの一部ではないときには、
1303使われます。
2161普通の文字として扱われるか、致命的エラーになり、どちらでも
1304また、下限の量指定子省略可能ではありせん
2162一般的に廃止予定警告が発生し
1305しかし、Perl v5.18 では、のようなもの全て廃し予定警告が発生し
2163れをエスケープするには、逆スラッシュを前置したり (C<"\{">)
1306Perl v.5.20 では、中かっこをリテラル使うには、逆スラッシュを前置した
2164かっこで囲んだ (C<"[{]">) できます。
1307大かっこで囲む (C<"\{"> または C<"[{]">) ことでエスケープすることが
1308要求されるようになる予定です。
13092165この変更により、(量指定子の加減をオプションにするような) 将来の
1310文法の拡張ができるようになり、量指定子に関するより良いエラーチェックが
2166文法の拡張ができるようになり、量指定子より良いエラーチェックが
13112167できるようになります。
1312現在のところ、量指定子のタイプミスは警告なしにリテラルな文字の並びとして
1313扱われます。
1314例えば:
13152168
1316 /o{4,3}/
1317
13182169=begin original
13192170
1320looks like a quantifier that matches 0 times, since 4 is greater than 3,
2171The C<"*"> quantifier is equivalent to C<{0,}>, the C<"+">
1321but it really means to match the sequence of six characters
2172quantifier to C<{1,}>, and the C<"?"> quantifier to C<{0,1}>. I<n> and I<m> are limited
1322S<C<"o { 4 , 3 }">>.)
1323
1324=end original
1325
1326これは、4 は 3 より大きいので、0 回マッチングする量指定子に見えますが、
1327これは実際には 6 文字並び
1328S<C<"o { 4 , 3 }">> にマッチングするという意味になります。)
1329
1330=begin original
1331
1332The "*" quantifier is equivalent to C<{0,}>, the "+"
1333quantifier to C<{1,}>, and the "?" quantifier to C<{0,1}>. n and m are limited
13342173to non-negative integral values less than a preset limit defined when perl is built.
13352174This is usually 32766 on the most common platforms. The actual limit can
13362175be seen in the error message generated by code such as this:
13372176
13382177=end original
13392178
1340"*" 量指定子は C<{0,}> と、"+" 量指定子は C<{1,}> と、
2179C<"*"> 量指定子は C<{0,}> と、C<"+"> 量指定子は C<{1,}> と、
1341"?" 量指定子は C<{0,1}> と等価です。
2180C<"?"> 量指定子は C<{0,1}> と等価です。
1342n 及び m は perl をビルドしたときに定義した既定の制限より小さな非負整数回に
2181I<n> I<m> は perl をビルドしたときに定義した既定の制限より小さな
1343制限されます。
2182非負整数回に制限されます。
13442183これは大抵のプラットフォームでは 32766 回になっています。
13452184実際の制限は次のようなコードを実行すると生成されるエラーメッセージで
13462185見ることができます:
13472186
13482187 $_ **= $_ , / {$_} / for 2 .. 42;
13492188
13502189=begin original
13512190
13522191By default, a quantified subpattern is "greedy", that is, it will match as
13532192many times as possible (given a particular starting location) while still
13542193allowing the rest of the pattern to match. If you want it to match the
1355minimum number of times possible, follow the quantifier with a "?". Note
2194minimum number of times possible, follow the quantifier with a C<"?">. Note
13562195that the meanings don't change, just the "greediness":
13572196X<metacharacter> X<greedy> X<greediness>
13582197X<?> X<*?> X<+?> X<??> X<{n}?> X<{n,}?> X<{n,m}?>
13592198
13602199=end original
13612200
13622201デフォルトでは、パターンで行われる量指定は「貪欲」です;
13632202つまりそれはパターンの残りの部分が可能な範囲で、
13642203(始めた地点から)可能な限り多くを先にあるパターンでマッチングさせます。
13652204もし最小回数でのマッチングを行いたいのであれば、量指定子の後ろに
1366"?" を続けます。
2205C<"?"> を続けます。
13672206意味は変更されずに「貪欲さ」だけを変更できます:
13682207X<metacharacter> X<greedy> X<greediness>
13692208X<?> X<*?> X<+?> X<??> X<{n}?> X<{n,}?> X<{n,m}?>
13702209
13712210=begin original
13722211
13732212 *? Match 0 or more times, not greedily
13742213 +? Match 1 or more times, not greedily
13752214 ?? Match 0 or 1 time, not greedily
13762215 {n}? Match exactly n times, not greedily (redundant)
13772216 {n,}? Match at least n times, not greedily
13782217 {n,m}? Match at least n but not more than m times, not greedily
13792218
13802219=end original
13812220
13822221 *? 0 回以上の貪欲でないマッチング
13832222 +? 1 回以上の貪欲でないマッチング
13842223 ?? 0 回または 1 回の貪欲でないマッチング
13852224 {n}? ちょうど n 回の貪欲でないマッチング (冗長)
13862225 {n,}? n 回以上の貪欲でないマッチング
13872226 {n,m}? n 回以上 m 回以下の貪欲でないマッチング
13882227
13892228=begin original
13902229
1391By default, when a quantified subpattern does not allow the rest of the
2230Normally when a quantified subpattern does not allow the rest of the
13922231overall pattern to match, Perl will backtrack. However, this behaviour is
13932232sometimes undesirable. Thus Perl provides the "possessive" quantifier form
13942233as well.
13952234
13962235=end original
13972236
1398デフォルトでは、パターンのうちの量指定された一部によってパターン全体が
2237通常、パターンのうちの量指定された一部によってパターン全体が
13992238マッチングに失敗したとき、Perl はバックトラックを行います。
14002239しかしこの振る舞いは望まれないこともあります。
14012240そのため、Perl は「絶対最大量(possessive)」量指定形式も提供しています。
14022241
14032242=begin original
14042243
14052244 *+ Match 0 or more times and give nothing back
14062245 ++ Match 1 or more times and give nothing back
14072246 ?+ Match 0 or 1 time and give nothing back
14082247 {n}+ Match exactly n times and give nothing back (redundant)
14092248 {n,}+ Match at least n times and give nothing back
14102249 {n,m}+ Match at least n but not more than m times and give nothing back
14112250
14122251=end original
14132252
14142253 *+ 0 回以上マッチングして何も返さない
14152254 ++ 1 回以上マッチングして何も返さない
14162255 ?+ 0 回または 1 回マッチングして何も返さない
14172256 {n}+ ちょうど n 回のマッチングして何も返さない (冗長)
14182257 {n,}+ n 回以上のマッチングして何も返さない
14192258 {n,m}+ n 回以上 m 回以下マッチングして何も返さない
14202259
14212260=begin original
14222261
14232262For instance,
14242263
14252264=end original
14262265
14272266例えば、
14282267
14292268 'aaaa' =~ /a++a/
14302269
14312270=begin original
14322271
1433will never match, as the C<a++> will gobble up all the C<a>'s in the
2272will never match, as the C<a++> will gobble up all the C<"a">'s in the
14342273string and won't leave any for the remaining part of the pattern. This
14352274feature can be extremely useful to give perl hints about where it
14362275shouldn't backtrack. For instance, the typical "match a double-quoted
14372276string" problem can be most efficiently performed when written as:
14382277
14392278=end original
14402279
1441は、C<a++> が文字列中の全ての C<a> を飲み込んでしまい、
2280は、C<a++> が文字列中の全ての C<"a"> を飲み込んでしまい、
14422281後に何も残さないためマッチングしません。
14432282この機能はバックトラックするべきでない場所のヒントを perl に
14442283与えるのに非常に便利です。
14452284例えば、典型的な「ダブルクォート文字列のマッチング」問題で次のように
14462285書くととても効率的になります:
14472286
14482287 /"(?:[^"\\]++|\\.)*+"/
14492288
14502289=begin original
14512290
14522291as we know that if the final quote does not match, backtracking will not
14532292help. See the independent subexpression
1454L</C<< (?>pattern) >>> for more details;
2293L</C<< (?>I<pattern>) >>> for more details;
14552294possessive quantifiers are just syntactic sugar for that construct. For
14562295instance the above example could also be written as follows:
14572296
14582297=end original
14592298
14602299見ての通り最後のクォートがマッチングしなかったとき、バックトラックは
14612300役に立ちません。
1462詳細は独立したサブパターン L</C<< (?>pattern) >>> を参照してください;
2301詳細は独立したサブパターン L</C<< (?>I<pattern>) >>> を参照してください;
14632302絶対最大量指定子はまさにその構文糖です。
14642303例えばこの例は次のようにも書けます:
14652304
14662305 /"(?>(?:(?>[^"\\]+)|\\.)*)"/
14672306
2307=begin original
2308
2309Note that the possessive quantifier modifier can not be combined
2310with the non-greedy modifier. This is because it would make no sense.
2311Consider the follow equivalency table:
2312
2313=end original
2314
2315絶対最大量指定修飾子は非貪欲修飾子と結合できないことに注意してください。
2316これは無意味だからです。
2317次の等価性表を考慮してください:
2318
2319 Illegal Legal
2320 ------------ ------
2321 X??+ X{0}
2322 X+?+ X{1}
2323 X{min,max}?+ X{min}
2324
14682325=head3 Escape sequences
14692326
14702327(エスケープシーケンス)
14712328
14722329=begin original
14732330
14742331Because patterns are processed as double-quoted strings, the following
14752332also work:
14762333
14772334=end original
14782335
14792336パターンはダブルクォート文字列として処理されるため、
14802337以下のエスケープ文字も動作します:
14812338X<\t> X<\n> X<\r> X<\f> X<\e> X<\a> X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q>
14822339X<\0> X<\c> X<\N{}> X<\x>
14832340
14842341=begin original
14852342
14862343 \t tab (HT, TAB)
14872344 \n newline (LF, NL)
14882345 \r return (CR)
14892346 \f form feed (FF)
14902347 \a alarm (bell) (BEL)
14912348 \e escape (think troff) (ESC)
14922349 \cK control char (example: VT)
14932350 \x{}, \x00 character whose ordinal is the given hexadecimal number
14942351 \N{name} named Unicode character or character sequence
14952352 \N{U+263D} Unicode character (example: FIRST QUARTER MOON)
14962353 \o{}, \000 character whose ordinal is the given octal number
14972354 \l lowercase next char (think vi)
14982355 \u uppercase next char (think vi)
1499 \L lowercase till \E (think vi)
2356 \L lowercase until \E (think vi)
1500 \U uppercase till \E (think vi)
2357 \U uppercase until \E (think vi)
1501 \Q quote (disable) pattern metacharacters till \E
2358 \Q quote (disable) pattern metacharacters until \E
15022359 \E end either case modification or quoted section, think vi
15032360
15042361=end original
15052362
15062363 \t タブ (水平タブ;HT、TAB)
15072364 \n 改行 (LF、NL)
15082365 \r 復帰 (CR)
15092366 \f フォームフィード (FF)
15102367 \a アラーム (ベル) (BEL)
15112368 \e エスケープ (troff 的) (ESC)
15122369 \cK 制御文字 (例: VT)
15132370 \x{}, \x00 16 進数で番号指定された文字
15142371 \N{name} 名前付きユニコード文字または文字並び
15152372 \N{U+263D} Unicode 文字 (例: FIRST QUARTER MOON)
15162373 \o{}, \000 8 進数で番号指定された文字
15172374 \l 次の文字を小文字に (vi 的)
15182375 \u 次の文字を大文字に (vi 的)
15192376 \L \E まで小文字に (vi 的)
15202377 \U \E まで大文字に (vi 的)
15212378 \Q \E までパターンメタ文字の無効化(Quote)
15222379 \E 大文字小文字変更またはクォートの終端 (vi 的)
15232380
15242381=begin original
15252382
15262383Details are in L<perlop/Quote and Quote-like Operators>.
15272384
15282385=end original
15292386
15302387詳細は L<perlop/Quote and Quote-like Operators> にあります。
15312388
15322389=head3 Character Classes and other Special Escapes
15332390
15342391(文字クラス及び他の特殊なエスケープ)
15352392
15362393=begin original
15372394
15382395In addition, Perl defines the following:
15392396X<\g> X<\k> X<\K> X<backreference>
15402397
15412398=end original
15422399
15432400さらに、Perl は以下のものを定義します:
15442401X<\g> X<\k> X<\K> X<backreference>
15452402
15462403=begin original
15472404
15482405 Sequence Note Description
15492406 [...] [1] Match a character according to the rules of the
15502407 bracketed character class defined by the "...".
15512408 Example: [a-z] matches "a" or "b" or "c" ... or "z"
15522409 [[:...:]] [2] Match a character according to the rules of the POSIX
15532410 character class "..." within the outer bracketed
15542411 character class. Example: [[:upper:]] matches any
15552412 uppercase character.
2413 (?[...]) [8] Extended bracketed character class
15562414 \w [3] Match a "word" character (alphanumeric plus "_", plus
15572415 other connector punctuation chars plus Unicode
15582416 marks)
15592417 \W [3] Match a non-"word" character
15602418 \s [3] Match a whitespace character
15612419 \S [3] Match a non-whitespace character
15622420 \d [3] Match a decimal digit character
15632421 \D [3] Match a non-digit character
15642422 \pP [3] Match P, named property. Use \p{Prop} for longer names
15652423 \PP [3] Match non-P
15662424 \X [4] Match Unicode "eXtended grapheme cluster"
1567 \C Match a single C-language char (octet) even if that is
1568 part of a larger UTF-8 character. Thus it breaks up
1569 characters into their UTF-8 bytes, so you may end up
1570 with malformed pieces of UTF-8. Unsupported in
1571 lookbehind.
15722425 \1 [5] Backreference to a specific capture group or buffer.
15732426 '1' may actually be any positive integer.
15742427 \g1 [5] Backreference to a specific or previous group,
15752428 \g{-1} [5] The number may be negative indicating a relative
15762429 previous group and may optionally be wrapped in
15772430 curly brackets for safer parsing.
15782431 \g{name} [5] Named backreference
15792432 \k<name> [5] Named backreference
15802433 \K [6] Keep the stuff left of the \K, don't include it in $&
1581 \N [7] Any character but \n (experimental). Not affected by
2434 \N [7] Any character but \n. Not affected by /s modifier
1582 /s modifier
15832435 \v [3] Vertical whitespace
15842436 \V [3] Not vertical whitespace
15852437 \h [3] Horizontal whitespace
15862438 \H [3] Not horizontal whitespace
15872439 \R [4] Linebreak
15882440
15892441=end original
15902442
15912443 Sequence Note Description
15922444 [...] [1] "..." で定義された大かっこ文字クラスのルールに従う文字に
15932445 マッチング。
15942446 例: [a-z] は "a", "b", "c", ... "z" にマッチング。
15952447 [[:...:]] [2] 外側の大かっこ文字クラスの内側の POSIX 文字クラスに
15962448 従う文字にマッチング。
15972449 例: [[:upper:]] は任意の大文字にマッチング。
2450 (?[...]) [8] 拡張大かっこ文字クラス
15982451 \w [3] "単語" 文字にマッチング (英数字及び "_" に加えて、
15992452 その他の接続句読点文字と Unicode マークにマッチング)
16002453 \W [3] 非"単語"文字にマッチング
16012454 \s [3] 空白文字にマッチング
16022455 \S [3] 非空白文字にマッチング
16032456 \d [3] 10 進数字にマッチング
16042457 \D [3] 非数字にマッチング
16052458 \pP [3] 名前属性 P にマッチング. 長い名前であれば \p{Prop}
16062459 \PP [3] P以外にマッチング
16072460 \X [4] Unicode 拡張書記素クラスタ("eXtended grapheme cluster")にマッチング
1608 \C より大きな UTF-8 文字の一部であっても、1つの C 言語の文字 (オクテット)にマッチング
1609 従って文字をUTF-8バイト列へと変換するので、壊れた
1610 UTF-8 片となるかもしれません; 後読みは対応していません
16112461 \1 [5] 指定した捕捉グループやバッファへの後方参照。
16122462 '1' には正の整数を指定できます。
16132463 \g1 [5] 指定したまたは前のグループへの後方参照
16142464 \g{-1} [5] 数値は相対的に前のグループを示す負の値にもできます、また
16152465 任意で安全にパースするために波かっこで括ることもできます
16162466 \g{name} [5] 名前指定の後方参照
16172467 \k<name> [5] 名前指定の後方参照
16182468 \K [6] \K の左にある物を保持、$& に含めない
1619 \N [7] \n 以外の任意の文字 (実験的) /s 修飾子の影響は受けない
2469 \N [7] \n 以外の任意の文字; /s 修飾子の影響は受けない
16202470 \v [3] 垂直空白
16212471 \V [3] 垂直空白以外
16222472 \h [3] 水平空白
16232473 \H [3] 水平空白以外
16242474 \R [4] 行区切り
16252475
16262476=over 4
16272477
16282478=item [1]
16292479
16302480=begin original
16312481
16322482See L<perlrecharclass/Bracketed Character Classes> for details.
16332483
16342484=end original
16352485
16362486詳しくは L<perlrecharclass/Bracketed Character Classes> を参照してください。
16372487
16382488=item [2]
16392489
16402490=begin original
16412491
16422492See L<perlrecharclass/POSIX Character Classes> for details.
16432493
16442494=end original
16452495
16462496詳しくは L<perlrecharclass/POSIX Character Classes> を参照してください。
16472497
16482498=item [3]
16492499
16502500=begin original
16512501
1652See L<perlrecharclass/Backslash sequences> for details.
2502See L<perlunicode/Unicode Character Properties> for details
16532503
16542504=end original
16552505
1656詳しくは L<perlrecharclass/Backslash sequences> を参照してください。
2506詳しくは L<perlunicode/Unicode Character Properties> を参照してください。
16572507
16582508=item [4]
16592509
16602510=begin original
16612511
16622512See L<perlrebackslash/Misc> for details.
16632513
16642514=end original
16652515
16662516詳しくは L<perlrebackslash/Misc> を参照してください。
16672517
16682518=item [5]
16692519
16702520=begin original
16712521
16722522See L</Capture groups> below for details.
16732523
16742524=end original
16752525
16762526詳しくは以下の L</Capture groups> を参照してください。
16772527
16782528=item [6]
16792529
16802530=begin original
16812531
16822532See L</Extended Patterns> below for details.
16832533
16842534=end original
16852535
16862536詳しくは以下のSee L</Extended Patterns> を参照してください。
16872537
16882538=item [7]
16892539
16902540=begin original
16912541
1692Note that C<\N> has two meanings. When of the form C<\N{NAME}>, it matches the
2542Note that C<\N> has two meanings. When of the form C<\N{I<NAME>}>, it
1693character or character sequence whose name is C<NAME>; and similarly
2543matches the character or character sequence whose name is I<NAME>; and
2544similarly
16942545when of the form C<\N{U+I<hex>}>, it matches the character whose Unicode
16952546code point is I<hex>. Otherwise it matches any character but C<\n>.
16962547
16972548=end original
16982549
16992550C<\N> には二つの意味があることに注意してください。
1700C<\N{NAME}> の形式では、これは名前が C<NAME> の文字または文字の並びに
2551C<\N{I<NAME>}> の形式では、これは名前が I<NAME> の文字または文字の並びに
17012552マッチングします;
17022553同様に、C<\N{U+I<wide hex char>}> の形式では、Unicode 符号位置が
17032554I<hex> の文字にマッチングします。
17042555そうでなければ、C<\n> 以外の任意の文字にマッチングします。
17052556
2557=item [8]
2558
2559=begin original
2560
2561See L<perlrecharclass/Extended Bracketed Character Classes> for details.
2562
2563=end original
2564
2565詳しくは L<perlrecharclass/Extended Bracketed Character Classes> を
2566参照してください。
2567
17062568=back
17072569
17082570=head3 Assertions
17092571
17102572(言明)
17112573
17122574=begin original
17132575
1714Perl defines the following zero-width assertions:
2576Besides L<C<"^"> and C<"$">|/Metacharacters>, Perl defines the following
2577zero-width assertions:
17152578X<zero-width assertion> X<assertion> X<regex, zero-width assertion>
17162579X<regexp, zero-width assertion>
17172580X<regular expression, zero-width assertion>
17182581X<\b> X<\B> X<\A> X<\Z> X<\z> X<\G>
17192582
17202583=end original
17212584
2585L<C<"^"> と C<"$">|/Metacharacters> の他に、
17222586Perl は以下のゼロ幅のアサーションを定義しています:
17232587X<zero-width assertion> X<assertion> X<regex, zero-width assertion>
17242588X<regexp, zero-width assertion>
17252589X<regular expression, zero-width assertion>
17262590X<\b> X<\B> X<\A> X<\Z> X<\z> X<\G>
17272591
1728 \b Match a word boundary
2592 \b{} Match at Unicode boundary of specified type
1729 \B Match except at a word boundary
2593 \B{} Match where corresponding \b{} doesn't match
1730 \A Match only at beginning of string
2594 \b Match a \w\W or \W\w boundary
1731 \Z Match only at end of string, or before newline at the end
2595 \B Match except at a \w\W or \W\w boundary
1732 \z Match only at end of string
2596 \A Match only at beginning of string
1733 \G Match only at pos() (e.g. at the end-of-match position
2597 \Z Match only at end of string, or before newline at the end
2598 \z Match only at end of string
2599 \G Match only at pos() (e.g. at the end-of-match position
17342600 of prior m//g)
17352601
17362602=begin original
17372603
2604A Unicode boundary (C<\b{}>), available starting in v5.22, is a spot
2605between two characters, or before the first character in the string, or
2606after the final character in the string where certain criteria defined
2607by Unicode are met. See L<perlrebackslash/\b{}, \b, \B{}, \B> for
2608details.
2609
2610=end original
2611
2612v5.22 から利用可能である Unicode 境界 (C<\b{}>) は、
2613Unicode で定義されたある種の基準に一致した、
2614二つの文字の間か、文字列の最初の文字の前か、
2615文字列の最後の文字の後の地点です。
2616詳しくは L<perlrebackslash/\b{}, \b, \B{}, \B> を参照してください。
2617
2618=begin original
2619
17382620A word boundary (C<\b>) is a spot between two characters
17392621that has a C<\w> on one side of it and a C<\W> on the other side
17402622of it (in either order), counting the imaginary characters off the
17412623beginning and end of the string as matching a C<\W>. (Within
17422624character classes C<\b> represents backspace rather than a word
17432625boundary, just as it normally does in any double-quoted string.)
1744The C<\A> and C<\Z> are just like "^" and "$", except that they
2626The C<\A> and C<\Z> are just like C<"^"> and C<"$">, except that they
17452627won't match multiple times when the C</m> modifier is used, while
1746"^" and "$" will match at every internal line boundary. To match
2628C<"^"> and C<"$"> will match at every internal line boundary. To match
17472629the actual end of the string and not ignore an optional trailing
17482630newline, use C<\z>.
17492631X<\b> X<\A> X<\Z> X<\z> X</m>
17502632
17512633=end original
17522634
17532635単語境界(C<\b>)はC<\W> にマッチングする文字列の始まりと終わりを
17542636連想するような、片側を C<\w>、もう片側を C<\W> で挟まれている点です。
17552637(文字クラスにおいては C<\b> は単語境界ではなくバックスペースを表します,
17562638ちょうどダブルクォート文字列と同じように。)
1757C<\A> 及び C<\Z> は "^" 及び "$" と同様ですが、C</m> 修飾子が
2639C<\A> 及び C<\Z> は C<"^"> 及び C<"$"> と同様ですが、C</m> 修飾子が
1758指定されているときに "^" 及び "$" は全ての内部的な行境界に
2640指定されているときに C<"^"> 及び C<"$"> は全ての内部的な行境界に
17592641マッチングするのに対して C<\A> 及び C<\Z> は複数回のマッチングには
17602642なりません。
17612643文字列の本当の末尾にマッチングさせ、省略可能である末尾の改行を
17622644無視しないようにする C<\z> を使います。
17632645X<\b> X<\A> X<\Z> X<\z> X</m>
17642646
17652647=begin original
17662648
17672649The C<\G> assertion can be used to chain global matches (using
17682650C<m//g>), as described in L<perlop/"Regexp Quote-Like Operators">.
17692651It is also useful when writing C<lex>-like scanners, when you have
17702652several patterns that you want to match against consequent substrings
17712653of your string; see the previous reference. The actual location
17722654where C<\G> will match can also be influenced by using C<pos()> as
17732655an lvalue: see L<perlfunc/pos>. Note that the rule for zero-length
17742656matches (see L</"Repeated Patterns Matching a Zero-length Substring">)
17752657is modified somewhat, in that contents to the left of C<\G> are
17762658not counted when determining the length of the match. Thus the following
17772659will not match forever:
17782660X<\G>
17792661
17802662=end original
17812663
17822664C<\G> アサーションはグローバルなマッチング(C<m//g>)を連結するために
17832665使います; これは L<perlop/"Regexp Quote-Like Operators"> にも説明されています。
17842666これは文字列に対していくつかのパターンを次々にマッチングさせたいといった、
17852667C<lex> 風のスキャナを書きたいときにも便利です; 以前のリファレンスを
17862668参照してください。
17872669C<\G> が実際にマッチングできる位置は C<pos()> を左辺値として
17882670使うことで変更できます: L<perlfunc/pos> を参照してください。
17892671ゼロ幅マッチング
17902672(L</"Repeated Patterns Matching a Zero-length Substring"> を参照してください)
17912673のルールは少し変化することに注意してください、
17922674C<\G> の左にある内容はマッチングの長さを決定するときに
17932675数えられません。
17942676従って次のコードは永遠にマッチングしません:
17952677X<\G>
17962678
17972679 my $string = 'ABC';
17982680 pos($string) = 1;
17992681 while ($string =~ /(.\G)/g) {
18002682 print $1;
18012683 }
18022684
18032685=begin original
18042686
18052687It will print 'A' and then terminate, as it considers the match to
18062688be zero-width, and thus will not match at the same position twice in a
18072689row.
18082690
18092691=end original
18102692
18112693これはゼロ幅へのマッチングと見なされ、'A' を出力し終了するので、
18122694行の中で同じ場所に二度はマッチングしません。
18132695
18142696=begin original
18152697
18162698It is worth noting that C<\G> improperly used can result in an infinite
18172699loop. Take care when using patterns that include C<\G> in an alternation.
18182700
18192701=end original
18202702
18212703適切に使われていない C<\G> は無限ループとなり何の価値もありません。
18222704代替(alternation; C<|>)の中に C<\G> を含んでいるパターンを使う際には
18232705十分注意してください。
18242706
2707=begin original
2708
2709Note also that C<s///> will refuse to overwrite part of a substitution
2710that has already been replaced; so for example this will stop after the
2711first iteration, rather than iterating its way backwards through the
2712string:
2713
2714=end original
2715
2716C<s///> は置換部の既に置き換えられた部分を上書きすることを拒否することにも
2717注意してください; 従って例えばこれは文字列の後ろ向きの反復中ではなく、
2718最初の反復の後に停止します:
2719
2720 $_ = "123456789";
2721 pos = 6;
2722 s/.(?=.\G)/X/g;
2723 print; # prints 1234X6789, not XXXXX6789
2724
18252725=head3 Capture groups
18262726
18272727(捕捉グループ)
18282728
18292729=begin original
18302730
1831The bracketing construct C<( ... )> creates capture groups (also referred to as
2731The grouping construct C<( ... )> creates capture groups (also referred to as
18322732capture buffers). To refer to the current contents of a group later on, within
18332733the same pattern, use C<\g1> (or C<\g{1}>) for the first, C<\g2> (or C<\g{2}>)
18342734for the second, and so on.
18352735This is called a I<backreference>.
18362736X<regex, capture buffer> X<regexp, capture buffer>
18372737X<regex, capture group> X<regexp, capture group>
18382738X<regular expression, capture buffer> X<backreference>
18392739X<regular expression, capture group> X<backreference>
18402740X<\g{1}> X<\g{-1}> X<\g{name}> X<relative backreference> X<named backreference>
18412741X<named capture buffer> X<regular expression, named capture buffer>
18422742X<named capture group> X<regular expression, named capture group>
18432743X<%+> X<$+{name}> X<< \k<name> >>
18442744There is no limit to the number of captured substrings that you may use.
1845Groups are numbered with the leftmost open parenthesis being number 1, etc. If
2745Groups are numbered with the leftmost open parenthesis being number 1, I<etc>. If
18462746a group did not match, the associated backreference won't match either. (This
18472747can happen if the group is optional, or in a different branch of an
18482748alternation.)
1849You can omit the C<"g">, and write C<"\1">, etc, but there are some issues with
2749You can omit the C<"g">, and write C<"\1">, I<etc>, but there are some issues with
18502750this form, described below.
18512751
18522752=end original
18532753
1854かっこ構文 C<( ... )> は捕捉グループを作成します (そして捕捉バッファとして
2754グループ化構文 C<( ... )> は捕捉グループを作成します (そして捕捉バッファとして
18552755参照します)。
18562756同じパターンの中で、あるグループの現在の内容を後で参照するには、
18572757最初のものには C<\g1> (または C<\g{1}>) を、2 番目には C<\g2> (または
18582758C<\g{2}>) を、以下同様のものを使います。
18592759これを I<後方参照> (backreference) と呼びます。
18602760X<regex, capture buffer> X<regexp, capture buffer>
18612761X<regex, capture group> X<regexp, capture group>
18622762X<regular expression, capture buffer> X<backreference>
18632763X<regular expression, capture group> X<backreference>
18642764X<\g{1}> X<\g{-1}> X<\g{name}> X<relative backreference> X<named backreference>
18652765X<named capture buffer> X<regular expression, named capture buffer>
18662766X<named capture group> X<regular expression, named capture group>
18672767X<%+> X<$+{name}> X<< \k<name> >>
18682768使う捕捉部分文字列の数に制限はありません。
18692769グループはいちばん左の開きかっこを 1 番として番号付けされます。
18702770グループがマッチングしなかった場合、対応する後方参照もマッチングしません。
18712771(これはグループがオプションか、選択の異なる枝の場合に怒ることがあります。)
18722772C<"g"> を省略して C<"\1"> などと書くこともできますが、後で述べるように、
18732773この形式にはいくらかの問題があります。
18742774
18752775=begin original
18762776
18772777You can also refer to capture groups relatively, by using a negative number, so
18782778that C<\g-1> and C<\g{-1}> both refer to the immediately preceding capture
18792779group, and C<\g-2> and C<\g{-2}> both refer to the group before it. For
18802780example:
18812781
18822782=end original
18832783
18842784負数を使うことで捕捉グループを相対的に参照することもできます; C<\g-1> と
18852785C<\g{-1}> は両方とも直前の捕捉グループを参照し、C<\g-2> と C<\g{-2}> は
18862786両方ともその前のグループを参照します。
18872787例えば:
18882788
18892789 /
18902790 (Y) # group 1
18912791 ( # group 2
18922792 (X) # group 3
18932793 \g{-1} # backref to group 3
18942794 \g{-3} # backref to group 1
18952795 )
18962796 /x
18972797
18982798=begin original
18992799
19002800would match the same as C</(Y) ( (X) \g3 \g1 )/x>. This allows you to
19012801interpolate regexes into larger regexes and not have to worry about the
19022802capture groups being renumbered.
19032803
19042804=end original
19052805
19062806は C</(Y) ( (X) \g3 \g1 )/x> と同じマッチングとなります。
19072807これにより、正規表現をより大きな正規表現に挿入したときに、捕捉グループの
19082808番号を振り直す心配をする必要がなくなります。
19092809
19102810=begin original
19112811
19122812You can dispense with numbers altogether and create named capture groups.
19132813The notation is C<(?E<lt>I<name>E<gt>...)> to declare and C<\g{I<name>}> to
19142814reference. (To be compatible with .Net regular expressions, C<\g{I<name>}> may
19152815also be written as C<\k{I<name>}>, C<\kE<lt>I<name>E<gt>> or C<\k'I<name>'>.)
19162816I<name> must not begin with a number, nor contain hyphens.
19172817When different groups within the same pattern have the same name, any reference
19182818to that name assumes the leftmost defined group. Named groups count in
19192819absolute and relative numbering, and so can also be referred to by those
19202820numbers.
19212821(It's possible to do things with named capture groups that would otherwise
19222822require C<(??{})>.)
19232823
19242824=end original
19252825
19262826数値を全く使わずに、名前付き捕捉グループを作ることが出来ます。
19272827記法は、宣言が C<(?E<lt>I<name>E<gt>...)>、参照が C<\g{I<name>}> です。
19282828(.Net 正規表現との互換性のために、C<\g{I<name>}> は C<\k{I<name>}>,
19292829C<\kE<lt>I<name>E<gt>>, C<\k'I<name>'> とも書けます。)
19302830I<name> は数字で始まってはならず、ハイフンを含んではなりません。
19312831同じパターンの中に同じ名前の違うグループがある場合、
19322832この名前での参照は一番左で定義されたものを仮定します。
19332833名前付きグループも絶対や相対番号付けに含まれるので、
19342834番号で参照することも出来ます。
19352835(C<(??{})> が必要な場合でも名前付き捕捉グループを使うことが出来ます。)
19362836
19372837=begin original
19382838
19392839Capture group contents are dynamically scoped and available to you outside the
19402840pattern until the end of the enclosing block or until the next successful
19412841match, whichever comes first. (See L<perlsyn/"Compound Statements">.)
19422842You can refer to them by absolute number (using C<"$1"> instead of C<"\g1">,
1943etc); or by name via the C<%+> hash, using C<"$+{I<name>}">.
2843I<etc>); or by name via the C<%+> hash, using C<"$+{I<name>}">.
19442844
19452845=end original
19462846
19472847捕捉グループの内容は動的スコープを持ち、パターンの外側でも現在のブロックの
19482848末尾か次のマッチングの成功のどちらか早いほうまで利用可能です。
19492849(L<perlsyn/"Compound Statements"> を参照してください。)
19502850これらに対して (C<"\g1"> などの代わりに C<"$1"> を使って) 絶対値で
19512851参照するか、C<"$+{I<name>}"> を使って C<%+> 経由で名前で参照できます。
19522852
19532853=begin original
19542854
19552855Braces are required in referring to named capture groups, but are optional for
19562856absolute or relative numbered ones. Braces are safer when creating a regex by
19572857concatenating smaller strings. For example if you have C<qr/$a$b/>, and C<$a>
19582858contained C<"\g1">, and C<$b> contained C<"37">, you would get C</\g137/> which
19592859is probably not what you intended.
19602860
19612861=end original
19622862
19632863名前付き捕捉グループを参照するには中かっこが必要です;
19642864しかし、絶対数値や相対数値の場合はオプションです。
19652865より小さい文字列を結合して正規表現を作る場合は中かっこを使う方が安全です。
19662866例えば C<qr/$a$b/> で C<$a> に C<"\g1"> を含み、
19672867C<$b> に C<"37"> を含んでいるとき、
19682868結果は C</\g137/> となりますが、おそらく望んでいたものではないでしょう。
19692869
19702870=begin original
19712871
19722872The C<\g> and C<\k> notations were introduced in Perl 5.10.0. Prior to that
19732873there were no named nor relative numbered capture groups. Absolute numbered
19742874groups were referred to using C<\1>,
1975C<\2>, etc., and this notation is still
2875C<\2>, I<etc>., and this notation is still
19762876accepted (and likely always will be). But it leads to some ambiguities if
19772877there are more than 9 capture groups, as C<\10> could mean either the tenth
19782878capture group, or the character whose ordinal in octal is 010 (a backspace in
19792879ASCII). Perl resolves this ambiguity by interpreting C<\10> as a backreference
19802880only if at least 10 left parentheses have opened before it. Likewise C<\11> is
19812881a backreference only if at least 11 left parentheses have opened before it.
19822882And so on. C<\1> through C<\9> are always interpreted as backreferences.
19832883There are several examples below that illustrate these perils. You can avoid
19842884the ambiguity by always using C<\g{}> or C<\g> if you mean capturing groups;
19852885and for octal constants always using C<\o{}>, or for C<\077> and below, using 3
19862886digits padded with leading zeros, since a leading zero implies an octal
19872887constant.
19882888
19892889=end original
19902890
19912891C<\g> と C<\k> の記法は Perl 5.10.0 で導入されました。
19922892それより前には名前付きや相対数値指定の捕捉グループはありませんでした。
19932893絶対数値指定のグループは C<\1>, C<\2> などとして参照でき、この記法はまだ
19942894受け付けられます (そしておそらくいつも受け付けられます)。
19952895しかし、これは 9 を越える捕捉グループがあるとあいまいさがあります;
19962896C<\10> は 10 番目の捕捉グループとも、8 進数で 010 の文字(ASCII で
19972897バックスペース)とも解釈できます。
19982898Perl はこのあいまいさを以下のように解決します;
19992899C<\10> の場合、これの前に少なくとも 10 の左かっこがある場合にのみ
20002900これを後方参照として解釈します。
20012901同様に、C<\11> はその前に少なくとも 11 の左かっこがある場合にのみ
20022902これを後方参照として解釈します。
20032903以下同様です。
20042904C<\1> から C<\9> は常に後方参照として解釈されます。
20052905これを図示するいくつかの例が後にあります。
20062906捕捉グループを意味する場合は常に C<\g{}> や C<\g> を使うことで
20072907あいまいさを避けられます;
20082908そして 8 進定数については常に C<\o{}> を使うか、C<\077> 以下の場合は、
20092909先頭に 0 を付けて 3 桁にします; なぜなら先頭に 0 が付くと
201029108 進定数を仮定するからです。
20112911
20122912=begin original
20132913
20142914The C<\I<digit>> notation also works in certain circumstances outside
20152915the pattern. See L</Warning on \1 Instead of $1> below for details.
20162916
20172917=end original
20182918
20192919C<\I<digit>> 記法は、ある種の状況ではパターンの外側でも動作します。
20202920詳しくは後述する L</Warning on \1 Instead of $1> を参照して下さい。
20212921
20222922=begin original
20232923
20242924Examples:
20252925
20262926=end original
20272927
20282928例:
20292929
20302930 s/^([^ ]*) *([^ ]*)/$2 $1/; # swap first two words
20312931
20322932 /(.)\g1/ # find first doubled char
20332933 and print "'$1' is the first doubled character\n";
20342934
20352935 /(?<char>.)\k<char>/ # ... a different way
20362936 and print "'$+{char}' is the first doubled character\n";
20372937
20382938 /(?'char'.)\g1/ # ... mix and match
20392939 and print "'$1' is the first doubled character\n";
20402940
20412941 if (/Time: (..):(..):(..)/) { # parse out values
20422942 $hours = $1;
20432943 $minutes = $2;
20442944 $seconds = $3;
20452945 }
20462946
20472947 /(.)(.)(.)(.)(.)(.)(.)(.)(.)\g10/ # \g10 is a backreference
20482948 /(.)(.)(.)(.)(.)(.)(.)(.)(.)\10/ # \10 is octal
20492949 /((.)(.)(.)(.)(.)(.)(.)(.)(.))\10/ # \10 is a backreference
20502950 /((.)(.)(.)(.)(.)(.)(.)(.)(.))\010/ # \010 is octal
20512951
20522952 $a = '(.)\1'; # Creates problems when concatenated.
20532953 $b = '(.)\g{1}'; # Avoids the problems.
20542954 "aa" =~ /${a}/; # True
20552955 "aa" =~ /${b}/; # True
20562956 "aa0" =~ /${a}0/; # False!
20572957 "aa0" =~ /${b}0/; # True
20582958 "aa\x08" =~ /${a}0/; # True!
20592959 "aa\x08" =~ /${b}0/; # False
20602960
20612961=begin original
20622962
20632963Several special variables also refer back to portions of the previous
20642964match. C<$+> returns whatever the last bracket match matched.
20652965C<$&> returns the entire matched string. (At one point C<$0> did
20662966also, but now it returns the name of the program.) C<$`> returns
20672967everything before the matched string. C<$'> returns everything
20682968after the matched string. And C<$^N> contains whatever was matched by
20692969the most-recently closed group (submatch). C<$^N> can be used in
20702970extended patterns (see below), for example to assign a submatch to a
20712971variable.
20722972X<$+> X<$^N> X<$&> X<$`> X<$'>
20732973
20742974=end original
20752975
20762976いくつかの特殊変数もまた以前のマッチングの一部を参照しています。
20772977C<$+> は最後のマッチングしたブラケットマッチングを返します。
20782978C<$&> はマッチングした文字列全体を返します。
20792979(一頃は C<$0> もそうでしたが、現在ではこれはプログラム名を返します。)
20802980C<$`> はマッチングした文字列の前の全てを返します。
20812981C<$'> はマッチングした文字列の後の全てを返します。
20822982そして C<$^N> には一番最後に閉じたグループ(サブマッチング)に
20832983マッチングしたものを含んでいます。
20842984C<$^N> は例えばサブマッチングを変数に格納するため等に拡張パターンの中でも
20852985利用できます(後述)。
20862986X<$+> X<$^N> X<$&> X<$`> X<$'>
20872987
20882988=begin original
20892989
20902990These special variables, like the C<%+> hash and the numbered match variables
2091(C<$1>, C<$2>, C<$3>, etc.) are dynamically scoped
2991(C<$1>, C<$2>, C<$3>, I<etc>.) are dynamically scoped
20922992until the end of the enclosing block or until the next successful
20932993match, whichever comes first. (See L<perlsyn/"Compound Statements">.)
20942994X<$+> X<$^N> X<$&> X<$`> X<$'>
20952995X<$1> X<$2> X<$3> X<$4> X<$5> X<$6> X<$7> X<$8> X<$9>
20962996
20972997=end original
20982998
20992999C<%+> ハッシュのような特殊変数と、数値によるマッチング変数
21003000(C<$1>, C<$2>, C<$3> など)はブロックの終端または次のマッチング
21013001成功までのどちらか先に満たした方の、動的なスコープを持ちます。
21023002(L<perlsyn/"Compound Statements"> を参照してください。)
21033003X<$+> X<$^N> X<$&> X<$`> X<$'>
21043004X<$1> X<$2> X<$3> X<$4> X<$5> X<$6> X<$7> X<$8> X<$9>
21053005
21063006=begin original
21073007
21083008B<NOTE>: Failed matches in Perl do not reset the match variables,
21093009which makes it easier to write code that tests for a series of more
21103010specific cases and remembers the best match.
21113011
21123012=end original
21133013
21143014B<補足>: Perl において失敗したマッチングはマッチング変数をリセットしません;
21153015これはより特殊化させる一連のテストを書くことや、
21163016最善のマッチングを書くことを容易にします。
21173017
21183018=begin original
21193019
2120B<WARNING>: Once Perl sees that you need one of C<$&>, C<$`>, or
3020B<WARNING>: If your code is to run on Perl 5.16 or earlier,
3021beware that once Perl sees that you need one of C<$&>, C<$`>, or
21213022C<$'> anywhere in the program, it has to provide them for every
2122pattern match. This may substantially slow your program. Perl
3023pattern match. This may substantially slow your program.
2123uses the same mechanism to produce C<$1>, C<$2>, etc, so you also pay a
2124price for each pattern that contains capturing parentheses. (To
3025=end original
2125avoid this cost while retaining the grouping behaviour, use the
3027B<警告>: あなたのコードが Perl 5.16 以前で実行されるものの場合、
3028Perl は、一旦プログラム中のどこかで C<$&>, C<$`>, C<$'> の
3029いずれかを必要としていることを見つけると、全てのパターンマッチングで
3030それらを提供しなければなあらないことに注意してください。
3031これはあなたのプログラムを大幅に遅くさせるでしょう。
3032
3033=begin original
3034
3035Perl uses the same mechanism to produce C<$1>, C<$2>, I<etc>, so you also
3036pay a price for each pattern that contains capturing parentheses.
3037(To avoid this cost while retaining the grouping behaviour, use the
21263038extended regular expression C<(?: ... )> instead.) But if you never
21273039use C<$&>, C<$`> or C<$'>, then patterns I<without> capturing
21283040parentheses will not be penalized. So avoid C<$&>, C<$'>, and C<$`>
21293041if you can, but if you can't (and some algorithms really appreciate
21303042them), once you've used them once, use them at will, because you've
2131already paid the price. As of 5.005, C<$&> is not so costly as the
3043already paid the price.
2132other two.
21333044X<$&> X<$`> X<$'>
21343045
21353046=end original
21363047
2137B<警告>: Perl は、一旦プログラム中のどこかで C<$&>, C<$`>, C<$'> の
2138いずれかを必要としていることを見つけると、全てのパターンマッチングで
2139それらを提供しなければなりません。
2140これはあなたのプログラムを大幅に遅くさせるでしょう。
21413048Perl は C<$1>, C<$2> 等の生成にも同じメカニズムを使っているので、
21423049キャプチャのかっこに含まれるそれぞれのパターンにも
21433050同じ料金を払っています。
21443051(グループ化の振る舞いを維持しつつこのコストを削減するには
21453052拡張正規表現 C<(?: ... )> を代わりに使います
21463053(訳注:Perl拡張というだけで C</x> 修飾子は不要)。)
21473054ですが C<$&>, C<$`> または C<$'> を一度も使わなければ、
21483055キャプチャのかっこをI<もたない>パターンではこの不利益はなくなります。
21493056この為、可能であれば C<$&>, C<$'>, 及び C<$`> を削除しましょう:
21503057しかしそれができなかった(そしてそれらを
21513058本当に理解しているアルゴリズムがあるのであれば)、一旦
21523059それらを使った時点でそれ以降は自由にそれらを使うことができます;
21533060なぜならあなたは(一度使った時点で)既に代価を払っているので。
21545.005 であれば C<$&> は他の2つほど高価ではありません。
21553061X<$&> X<$`> X<$'>
21563062
21573063=begin original
21583064
2159As a workaround for this problem, Perl 5.10.0 introduces C<${^PREMATCH}>,
3065Perl 5.16 introduced a slightly more efficient mechanism that notes
3066separately whether each of C<$`>, C<$&>, and C<$'> have been seen, and
3067thus may only need to copy part of the string. Perl 5.20 introduced a
3068much more efficient copy-on-write mechanism which eliminates any slowdown.
3069
3070=end original
3071
3072Perl 5.16 では、C<$`>, C<$&>, C<$'> のそれぞれが現れるかどうかを
3073個別に記録するという少し効率的な機構が導入され、
3074従って文字列の一部分だけコピーするようになりました。
3075Perl 5.20 では、全く遅くならない遙かに効率的なコピーオンライト機構を
3076導入しました。
3077
3078=begin original
3079
3080As another workaround for this problem, Perl 5.10.0 introduced C<${^PREMATCH}>,
21603081C<${^MATCH}> and C<${^POSTMATCH}>, which are equivalent to C<$`>, C<$&>
21613082and C<$'>, B<except> that they are only guaranteed to be defined after a
21623083successful match that was executed with the C</p> (preserve) modifier.
21633084The use of these variables incurs no global performance penalty, unlike
2164their punctuation char equivalents, however at the trade-off that you
3085their punctuation character equivalents, however at the trade-off that you
2165have to tell perl when you want to use them.
3086have to tell perl when you want to use them. As of Perl 5.20, these three
3087variables are equivalent to C<$`>, C<$&> and C<$'>, and C</p> is ignored.
21663088X</p> X<p modifier>
21673089
21683090=end original
21693091
2170この問題に対する解決策として、Perl 5.10.0 からは C<$`>, C<$&>, C<$'> と
3092この問題に対するもう一つの解決策として、Perl 5.10.0 からは
3093C<$`>, C<$&>, C<$'> と
21713094等価だけれども C</p> (preseve) 修飾子を伴って実行されたマッチングが
21723095成功した後でのみ定義されることが保証される C<${^PREMATCH}>、
21733096C<${^MATCH}> 及び C<${^POSTMATCH}> を導入しました。
21743097これらの変数の使用は利用したいときに perl に伝える必要がある代わりに、
21753098等価な記号変数とは違い全体的なパフォーマンスの低下を引き起こしません。
3099Perl 5.20 からこれら三つの変数は C<$`>, C<$&>, C<$'> と等価となり、
3100C</p> は無視されます。
21763101X</p> X<p modifier>
21773102
21783103=head2 Quoting metacharacters
21793104
21803105(メタ文字のクォート)
21813106
21823107=begin original
21833108
21843109Backslashed metacharacters in Perl are alphanumeric, such as C<\b>,
21853110C<\w>, C<\n>. Unlike some other regular expression languages, there
21863111are no backslashed symbols that aren't alphanumeric. So anything
2187that looks like \\, \(, \), \<, \>, \{, or \} is always
3112that looks like C<\\>, C<\(>, C<\)>, C<\[>, C<\]>, C<\{>, or C<\}> is
3113always
21883114interpreted as a literal character, not a metacharacter. This was
21893115once used in a common idiom to disable or quote the special meanings
21903116of regular expression metacharacters in a string that you want to
21913117use for a pattern. Simply quote all non-"word" characters:
21923118
21933119=end original
21943120
2195Perl においてバックスラッシュで表現されるメタ文字は C<\b>, C<\w>,
3121Perl においてスラッシュで表現されるメタ文字は C<\b>, C<\w>,
21963122C<\n> のように英数字です。
2197他の正規表現言語とは異なり、英数字でないシンボルのバックスラッシュは
3123他の正規表現言語とは異なり、英数字でないスラッシュ付きシンボル
21983124ありません。
2199なので \\, \(, \), \<, \>, \{, \} といったものは全てメタ文字ではなく
3125なので C<\\>, C<\(>, C<\)>, C<\[>, C<\]>, C<\{>, or C<\}> といったものは
2200リテラル文字です。
3126全てメタ文字ではなくリテラル文字です。
22013127これはパターンで使いたい文字列の中で正規表現のメタ文字としての特殊な意味を
22023128無効化またはクォートするための一般的な指標として使われてきました。
22033129「単語」でない全ての文字は単にクォートします:
22043130
22053131 $pattern =~ s/(\W)/\\$1/g;
22063132
22073133=begin original
22083134
22093135(If C<use locale> is set, then this depends on the current locale.)
2210Today it is more common to use the quotemeta() function or the C<\Q>
3136Today it is more common to use the C<L<quotemeta()|perlfunc/quotemeta>>
2211metaquoting escape sequence to disable all metacharacters' special
3137function or the C<\Q> metaquoting escape sequence to disable all
2212meanings like this:
3138metacharacters' special meanings like this:
22133139
22143140=end original
22153141
22163142(もし C<use locale> が有効であれば、これは現在のロケールに依存します。)
22173143今日では特殊な意味を持つメタ文字を全て無効にするためには次のように
2218quotemeta() 関数か C<\Q> メタクォートエスケープシーケンスを使うのが
3144C<L<quotemeta()|perlfunc/quotemeta>> 関数か C<\Q> メタクォート
2219より一般的です:
3145エスケープシーケンスを使うのがより一般的です:
22203146
22213147 /$unquoted\Q$quoted\E$unquoted/
22223148
22233149=begin original
22243150
22253151Beware that if you put literal backslashes (those not inside
22263152interpolated variables) between C<\Q> and C<\E>, double-quotish
22273153backslash interpolation may lead to confusing results. If you
22283154I<need> to use literal backslashes within C<\Q...\E>,
22293155consult L<perlop/"Gory details of parsing quoted constructs">.
22303156
22313157=end original
22323158
2233C<\Q> 及び C<\E> の間でリテラルとしてバックスラッシュをおくとき
3159C<\Q> 及び C<\E> の間でリテラルとしてスラッシュをおくとき
2234(埋め込んだ変数の中でではない)には、二重にクォートしたバックスラッシュの
3160(埋め込んだ変数の中でではない)には、二重にクォートしたスラッシュの
22353161埋め込みは困惑した結果となるでしょう。
2236もし C<\Q...\E> でリテラルとしてのバックスラッシュを使う
3162もし C<\Q...\E> でリテラルとしてのスラッシュを使う
22373163I<必要がある> のなら、
22383164L<perlop/"Gory details of parsing quoted constructs"> を参照してください。
22393165
22403166=begin original
22413167
22423168C<quotemeta()> and C<\Q> are fully described in L<perlfunc/quotemeta>.
22433169
22443170=end original
22453171
22463172C<quotemeta()> と C<\Q> は L<perlfunc/quotemeta> に完全に記述されています。
22473173
22483174=head2 Extended Patterns
22493175
22503176(拡張パターン)
22513177
22523178=begin original
22533179
22543180Perl also defines a consistent extension syntax for features not
22553181found in standard tools like B<awk> and
22563182B<lex>. The syntax for most of these is a
22573183pair of parentheses with a question mark as the first thing within
22583184the parentheses. The character after the question mark indicates
22593185the extension.
22603186
22613187=end original
22623188
22633189Perl は B<awk> や B<lex> といった標準的なツールでは見られない機能のための
22643190拡張構文も定義しています。
22653191これらのほとんどの構文は対のかっことかっこ内の最初に疑問符の形をとります。
22663192疑問符の後の文字で拡張を区別します。
22673193
22683194=begin original
22693195
2270The stability of these extensions varies widely. Some have been
2271part of the core language for many years. Others are experimental
2272and may change without warning or be completely removed. Check
2273the documentation on an individual feature to verify its current
2274status.
2275
2276=end original
2277
2278拡張構文の安定度は様々です。
2279中には長年言語コアの一部となっている物もあります。
2280そうでなく実験的に追加され警告なしに変更されたり削除されるものも
2281中にはあります。
2282それぞれのステータスに関しては個々の機能のドキュメントを確認してください。
2283
2284=begin original
2285
22863196A question mark was chosen for this and for the minimal-matching
22873197construct because 1) question marks are rare in older regular
22883198expressions, and 2) whenever you see one, you should stop and
22893199"question" exactly what is going on. That's psychology....
22903200
22913201=end original
22923202
22933203疑問符は 1) それが古い正規表現で使われることは稀であること、そして
229432042) それを見かけると何が行われるのか本当に「疑問に」思って止まることから、
22953205これのためと最小マッチング構成子のために選ばれました。
22963206これが心理学です…。
22973207
22983208=over 4
22993209
2300=item C<(?#text)>
3210=item C<(?<#ins>I<text>)>
23013211X<(?#)>
23023212
23033213=begin original
23043214
2305A comment. The text is ignored. If the C</x> modifier enables
3215A comment. The I<text> is ignored.
2306whitespace formatting, a simple C<#> will suffice. Note that Perl closes
3216Note that Perl closes
2307the comment as soon as it sees a C<)>, so there is no way to put a literal
3217the comment as soon as it sees a C<")">, so there is no way to put a literal
2308C<)> in the comment.
3218C<")"> in the comment. The pattern's closing delimiter must be escaped by
3219a backslash if it appears in the comment.
23093220
23103221=end original
23113222
23123223コメント。
2313テキストは無視されます。
3224I<text> は無視されます。
2314C</x> 修飾子よって空白の整形が有効されていれば単なる C<#> でも十分です。
3225Perl は C<")"> を見つけると直ぐコメントを閉じる点注意しくださ;
2315Perl C<)> を見つけると直ぐにコメントを閉じる点注意してださい;
3226この為リテラル C<")"> をコメントことはできません。
2316為リテラル C<)> をコメントおくことはできません。
3227パターン閉じ区切り文字がコメントに見えるようなものなら、
3228逆スラッシュでエスケープしなければなりません。
23173229
2318=item C<(?adlupimsx-imsx)>
3230=begin original
23193231
2320=item C<(?^alupimsx)>
3232See L</E<sol>x> for another way to have comments in patterns.
3233
3234=end original
3235
3236パターンの中にコメントを入れるもう一つの方法については
3237L</E<sol>x> を参照してください。
3238
3239=begin original
3240
3241Note that a comment can go just about anywhere, except in the middle of
3242an escape sequence. Examples:
3243
3244=end original
3245
3246コメントは、エスケープシーケンスの途中を除いて、
3247どこにでも入れることができることに注意してください。
3248例:
3249
3250 qr/foo(?#comment)bar/' # Matches 'foobar'
3251
3252 # The pattern below matches 'abcd', 'abccd', or 'abcccd'
3253 qr/abc(?#comment between literal and its quantifier){1,3}d/
3254
3255 # The pattern below generates a syntax error, because the '\p' must
3256 # be followed immediately by a '{'.
3257 qr/\p(?#comment between \p and its property name){Any}/
3258
3259 # The pattern below generates a syntax error, because the initial
3260 # '\(' is a literal opening parenthesis, and so there is nothing
3261 # for the closing ')' to match
3262 qr/\(?#the backslash means this isn't a comment)p{Any}/
3263
3264 # Comments can be used to fold long patterns into multiple lines
3265 qr/First part of a long regex(?#
3266 )remaining part/
3267
3268=item C<(?adlupimnsx-imnsx)>
3269
3270=item C<(?^alupimnsx)>
23213271X<(?)> X<(?^)>
23223272
23233273=begin original
23243274
2325One or more embedded pattern-match modifiers, to be turned on (or
3275Zero or more embedded pattern-match modifiers, to be turned on (or
2326turned off, if preceded by C<->) for the remainder of the pattern or
3276turned off if preceded by C<"-">) for the remainder of the pattern or
23273277the remainder of the enclosing pattern group (if any).
23283278
23293279=end original
23303280
2331一つもしくは複数のパターンマッチング修飾子;
3281ゼロ以上のパターンマッチング修飾子;
23323282パターンの残りまたは(もしあれば)包含しているパターングループの残りで
2333有効にする(または C<-> が前置されていれば解除する)。
3283有効にする(または C<"-"> が前置されていれば解除する)。
23343284
23353285=begin original
23363286
2337This is particularly useful for dynamic patterns, such as those read in from a
3287This is particularly useful for dynamically-generated patterns,
3288such as those read in from a
23383289configuration file, taken from an argument, or specified in a table
23393290somewhere. Consider the case where some patterns want to be
23403291case-sensitive and some do not: The case-insensitive ones merely need to
23413292include C<(?i)> at the front of the pattern. For example:
23423293
23433294=end original
23443295
23453296これは設定ファイルから読む、引数から取る、どこかのテーブルで
2346指定されている箇所からダイナミックなパターンを使うときに特に便利です。
3297指定されている箇所から動的生成されたパターンを使うときに特に便利です。
23473298パターンの一部では大文字小文字を区別したいけれども別の箇所では
23483299区別しないといったケースを考えてみます: 区別をしない場所では
23493300単にパターンの先頭に C<(?i)> を含めるだけです。
23503301例えば:
23513302
23523303 $pattern = "foobar";
23533304 if ( /$pattern/i ) { }
23543305
23553306 # more flexible:
23563307
23573308 $pattern = "(?i)foobar";
23583309 if ( /$pattern/ ) { }
23593310
23603311=begin original
23613312
23623313These modifiers are restored at the end of the enclosing group. For example,
23633314
23643315=end original
23653316
23663317これらの修飾子は包含しているグループの最後で復元(restore)されます。
23673318例えば、
23683319
23693320 ( (?i) blah ) \s+ \g1
23703321
23713322=begin original
23723323
23733324will match C<blah> in any case, some spaces, and an exact (I<including the case>!)
23743325repetition of the previous word, assuming the C</x> modifier, and no C</i>
23753326modifier outside this group.
23763327
23773328=end original
23783329
23793330は C<blah> に大文字小文字の区別なくマッチングし、
23803331いくつかの空白、そして前の単語その物(I<大文字小文字の区別まで含めて>!)に
23813332再度マッチングします; ここではこのグループの外側で C</x> 修飾子を持ち、
23823333C</i> 修飾子を持たないものとします。
23833334
23843335=begin original
23853336
23863337These modifiers do not carry over into named subpatterns called in the
2387enclosing group. In other words, a pattern such as C<((?i)(?&NAME))> does not
3338enclosing group. In other words, a pattern such as C<((?i)(?&I<NAME>))> does not
2388change the case-sensitivity of the "NAME" pattern.
3339change the case-sensitivity of the I<NAME> pattern.
23893340
23903341=end original
23913342
23923343これらの修飾子は囲まれたグループで呼び出された名前付き部分パターンには
23933344持ち越されません。
2394言い換えると、C<((?i)(?&NAME))> のようなパターンは、"NAME" パターンが
3345言い換えると、C<((?i)(?&I<NAME>))> のようなパターンは、
2395大文字小文字を認識するのを変更しません。
3346I<NAME> パターンが大文字小文字を認識するのを変更しません。
23963347
23973348=begin original
23983349
3350A modifier is overridden by later occurrences of this construct in the
3351same scope containing the same modifier, so that
3352
3353=end original
3354
3355修飾子は、同じスコープ内で同じ修飾子を含む
3356この構文が後に出現すると上書きされるので:
3357
3358 /((?im)foo(?-m)bar)/
3359
3360=begin original
3361
3362matches all of C<foobar> case insensitively, but uses C</m> rules for
3363only the C<foo> portion. The C<"a"> flag overrides C<aa> as well;
3364likewise C<aa> overrides C<"a">. The same goes for C<"x"> and C<xx>.
3365Hence, in
3366
3367=end original
3368
3369は、C<foobar> のすべてに大文字小文字を区別せずにマッチングしますが、
3370C<foo> 部分のみに C</m> の規則を使います。
3371C<"a"> フラグは C<aa> も上書きします;
3372同様に、C<aa> も C<"a">を上書きします。
3373同じことが C<"x"> と C<xx> についても当てはまります。
3374したがって:
3375
3376 /(?-x)foo/xx
3377
3378=begin original
3379
3380both C</x> and C</xx> are turned off during matching C<foo>. And in
3381
3382=end original
3383
3384C</x> と C</xx> は両方とも、C<foo> のマッチング中はオフになります。
3385そして:
3386
3387 /(?x)foo/x
3388
3389=begin original
3390
3391C</x> but NOT C</xx> is turned on for matching C<foo>. (One might
3392mistakenly think that since the inner C<(?x)> is already in the scope of
3393C</x>, that the result would effectively be the sum of them, yielding
3394C</xx>. It doesn't work that way.) Similarly, doing something like
3395C<(?xx-x)foo> turns off all C<"x"> behavior for matching C<foo>, it is not
3396that you subtract 1 C<"x"> from 2 to get 1 C<"x"> remaining.
3397
3398=end original
3399
3400C<foo> にマッチングする間、C</xx> ではなく C</x> がオンになります。
3401(内部 C<(?x)> はすでに C</x> の範囲内にあるので、結果は実質的に
3402それらの和になり、C</xx> が生成されると誤って考えるかもしれません。
3403そうはなりません。)
3404同様に、C<(?xx-x)foo> のようなことをすると、C<foo> とマッチングする間
3405C<"x"> の動作がすべてオフになります;
3406二つの C<"x"> から一つ引いて一つの C<"x"> が残るということにはなりません。
3407
3408=begin original
3409
23993410Any of these modifiers can be set to apply globally to all regular
24003411expressions compiled within the scope of a C<use re>. See
24013412L<re/"'/flags' mode">.
24023413
24033414=end original
24043415
24053416それらの変更のどれもセットでき、C<use re>のスコープ内でグローバルに
24063417全てのコンパイルされた正規表現に適用されます。L<re/"'/flags' mode">
24073418を見てください。
24083419
24093420=begin original
24103421
24113422Starting in Perl 5.14, a C<"^"> (caret or circumflex accent) immediately
2412after the C<"?"> is a shorthand equivalent to C<d-imsx>. Flags (except
3423after the C<"?"> is a shorthand equivalent to C<d-imnsx>. Flags (except
24133424C<"d">) may follow the caret to override it.
24143425But a minus sign is not legal with it.
24153426
24163427=end original
24173428
24183429Perl 5.14から、C<"^">(キャレットか曲折アクセント)がC<"?">のすぐ
2419後ろにつくと、C<d-imsx>と同じになります。フラグ(C<"d">以外の)
3430後ろにつくと、C<d-imnsx> と同じになります。フラグ(C<"d">以外の)
24203431をキャレットに続けることで、上書きできます。
24213432ですが、マイナス記号は一緒に使えません。
24223433
24233434=begin original
24243435
2425Note that the C<a>, C<d>, C<l>, C<p>, and C<u> modifiers are special in
3436Note that the C<"a">, C<"d">, C<"l">, C<"p">, and C<"u"> modifiers are special in
2426that they can only be enabled, not disabled, and the C<a>, C<d>, C<l>, and
3437that they can only be enabled, not disabled, and the C<"a">, C<"d">, C<"l">, and
2427C<u> modifiers are mutually exclusive: specifying one de-specifies the
3438C<"u"> modifiers are mutually exclusive: specifying one de-specifies the
2428others, and a maximum of one (or two C<a>'s) may appear in the
3439others, and a maximum of one (or two C<"a">'s) may appear in the
24293440construct. Thus, for
24303441example, C<(?-p)> will warn when compiled under C<use warnings>;
24313442C<(?-d:...)> and C<(?dl:...)> are fatal errors.
24323443
24333444=end original
24343445
2435C<a>, C<d>, C<l>, C<p>, C<u> 修飾子は有効にできるのみで、無効にはできない点
3446C<"a">, C<"d">, C<"l">, C<"p">, C<"u"> 修飾子は有効にできるのみで、
2436そして C<a>, C<d>, C<l>, C<u> 修飾子は互いに排他であるという点で特別です:
3447無効にはできない点、 そして C<"a">, C<"d">, C<"l">, C<"u"> 修飾子は
3448互いに排他であるという点で特別です:
24373449一つを指定すると他のものの指定を解除し、構文中に最大で一つ (または二つの
2438C<a>) だけが現れます。
3450C<"a">) だけが現れます。
24393451従って 例えば C<(?-p)> は C<use warnings> の下でコンパイルされると
24403452警告を発します; C<(?-d:...)> と C<(?dl:...)> は致命的エラーです。
24413453
24423454=begin original
24433455
2444Note also that the C<p> modifier is special in that its presence
3456Note also that the C<"p"> modifier is special in that its presence
24453457anywhere in a pattern has a global effect.
24463458
24473459=end original
24483460
24493461パターン中のどこにあってもグローバルな影響があるという意味で
2450C<p> 修飾子が特別であることにも注意してください。
3462C<"p"> 修飾子が特別であることにも注意してください。
24513463
2452=item C<(?:pattern)>
3464=begin original
3465
3466Having zero modifiers makes this a no-op (so why did you specify it,
3467unless it's generated code), and starting in v5.30, warns under L<C<use
3468re 'strict'>|re/'strict' mode>.
3469
3470=end original
3471
3472修飾子がない場合、これは何もせず (なので、生成されたコードでない場合、
3473なぜこれを指定したのでしょう?)、v5.30 から、
3474L<C<use re 'strict'>|re/'strict' mode> の警告が出ます。
3475
3476=item C<(?:I<pattern>)>
24533477X<(?:)>
24543478
2455=item C<(?adluimsx-imsx:pattern)>
3479=item C<(?adluimnsx-imnsx:I<pattern>)>
24563480
2457=item C<(?^aluimsx:pattern)>
3481=item C<(?^aluimnsx:I<pattern>)>
24583482X<(?^:)>
24593483
24603484=begin original
24613485
24623486This is for clustering, not capturing; it groups subexpressions like
2463"()", but doesn't make backreferences as "()" does. So
3487C<"()">, but doesn't make backreferences as C<"()"> does. So
24643488
24653489=end original
24663490
2467これはキャプチャではなくクラスタです; これは "()" のように部分式を
3491これはキャプチャではなくクラスタです; これは C<"()"> のように部分式を
2468グループ化しますが "()" が行うような後方参照は行いません。
3492グループ化しますが C<"()"> が行うような後方参照は行いません。
24693493つまり、
24703494
24713495 @fields = split(/\b(?:a|b|c)\b/)
24723496
24733497=begin original
24743498
2475is like
3499matches the same field delimiters as
24763500
24773501=end original
24783502
2479次と同様ですが
3503次と同じフィールド区切り文字にマッチングしますが:
24803504
24813505 @fields = split(/\b(a|b|c)\b/)
24823506
24833507=begin original
24843508
2485but doesn't spit out extra fields. It's also cheaper not to capture
3509but doesn't spit out the delimiters themselves as extra fields (even though
3510that's the behaviour of L<perlfunc/split> when its pattern contains capturing
3511groups). It's also cheaper not to capture
24863512characters if you don't need to.
24873513
24883514=end original
24893515
2490余計なフィー出しません。
3516(例えそれが捕捉グープ含むとの L<perlfunc/split> の振る舞いで
3517あったとしても) 区切り文字自身を余計なフィールドとして引き出しません。
24913518また不要であれば文字のキャプチャを行わないため低コストです。
24923519
24933520=begin original
24943521
2495Any letters between C<?> and C<:> act as flags modifiers as with
3522Any letters between C<"?"> and C<":"> act as flags modifiers as with
2496C<(?adluimsx-imsx)>. For example,
3523C<(?adluimnsx-imnsx)>. For example,
24973524
24983525=end original
24993526
2500C<?> 及び C<:> の間の文字は C<(?adluimsx-imsx)> のようなフラグ修飾子として
3527C<"?"> 及び C<":"> の間の文字は C<(?adluimnsx-imnsx)> のような
2501動作します。
3528フラグ修飾子として動作します。
25023529例えば、
25033530
25043531 /(?s-i:more.*than).*million/i
25053532
25063533=begin original
25073534
25083535is equivalent to the more verbose
25093536
25103537=end original
25113538
25123539はより冗長に書けば以下と等価です
25133540
25143541 /(?:(?s-i)more.*than).*million/i
25153542
25163543=begin original
25173544
3545Note that any C<()> constructs enclosed within this one will still
3546capture unless the C</n> modifier is in effect.
3547
3548=end original
3549
3550これの中の C<()> 構文は、C</n> が有効でない限りまだ捕捉することに
3551注意してください。
3552
3553=begin original
3554
3555Like the L</(?adlupimnsx-imnsx)> construct, C<aa> and C<"a"> override each
3556other, as do C<xx> and C<"x">. They are not additive. So, doing
3557something like C<(?xx-x:foo)> turns off all C<"x"> behavior for matching
3558C<foo>.
3559
3560=end original
3561
3562L</(?adlupimnsx-imnsx)> 構文と同様、C<xx> と
3563C<"x">と同様に、C<aa> と C<"a"> は互いに上書きします。
3564これらは加法的ではありません。
3565したがって、C<(?xx-x:foo)> のようなことを行うと、
3566C<foo> にマッチングする間、C<"x"> の動作がすべてオフになります。
3567
3568=begin original
3569
25183570Starting in Perl 5.14, a C<"^"> (caret or circumflex accent) immediately
2519after the C<"?"> is a shorthand equivalent to C<d-imsx>. Any positive
3571after the C<"?"> is a shorthand equivalent to C<d-imnsx>. Any positive
25203572flags (except C<"d">) may follow the caret, so
25213573
25223574=end original
25233575
2524Perl 5.14から、C<"^">(キャレット曲折アクセント)がC<"?">のすぐ
3576Perl 5.14 から、C<"^">(キャレットあるいは曲折アクセント)が C<"?"> のすぐ
2525後ろにつくと、C<d-imsx>と同じになります。
3577後ろにつくと、C<d-imnsx> と同じになります。
2526どのような肯定のフラグ(C<"d">以外の)もキャレットに続けることができます;
3578どのような肯定のフラグ(C<"d"> 以外の)もキャレットに続けることができます;
25273579そのため、
25283580
25293581 (?^x:foo)
25303582
25313583=begin original
25323584
25333585is equivalent to
25343586
25353587=end original
25363588
25373589は、以下と同じになります。
25383590
2539 (?x-ims:foo)
3591 (?x-imns:foo)
25403592
25413593=begin original
25423594
25433595The caret tells Perl that this cluster doesn't inherit the flags of any
2544surrounding pattern, but uses the system defaults (C<d-imsx>),
3596surrounding pattern, but uses the system defaults (C<d-imnsx>),
25453597modified by any flags specified.
25463598
25473599=end original
25483600
25493601キャレットは、Perlにこのクラスターはパターンの周りのどのフラグも
2550引き継がずに、代わりに、システムのデフォルトのフラグ(C<d-imsx>)
3602引き継がずに、代わりに、システムのデフォルトのフラグ (C<d-imnsx>)
25513603を使うことを教えます; 指定されている他のフラグによって変更されます。
25523604
25533605=begin original
25543606
25553607The caret allows for simpler stringification of compiled regular
25563608expressions. These look like
25573609
25583610=end original
25593611
25603612キャレットはより簡単なコンパイルされた正規表現の文字列化をすることが
25613613できます。次のものは
25623614
25633615 (?^:pattern)
25643616
25653617=begin original
25663618
25673619with any non-default flags appearing between the caret and the colon.
25683620A test that looks at such stringification thus doesn't need to have the
25693621system default flags hard-coded in it, just the caret. If new flags are
25703622added to Perl, the meaning of the caret's expansion will change to include
25713623the default for those flags, so the test will still work, unchanged.
25723624
25733625=end original
25743626
25753627キャレットとコロンの間には、デフォルトでないフラグがありません。
25763628このような文字列化を見るテストには、したがって、システムのデフォルトのフラグを
25773629その中にハードコードする必要はなく、ただキャレットを使います。新しいフラグが
25783630Perlに追加されたら、キャレットを展開した意味はそれらのフラグのためのデフォルトを
25793631含むように変更されます; そのために、このテストは、それでも何も変えずに
25803632動くでしょう。
25813633
25823634=begin original
25833635
25843636Specifying a negative flag after the caret is an error, as the flag is
25853637redundant.
25863638
25873639=end original
25883640
25893641キャレットの後ろに否定のフラグを付けると、余計なフラグであるとして、
25903642エラーになります。
25913643
25923644=begin original
25933645
25943646Mnemonic for C<(?^...)>: A fresh beginning since the usual use of a caret is
25953647to match at the beginning.
25963648
25973649=end original
25983650
25993651C<(?^...)>の覚え方: 新鮮な開始; 通常のキャレットの使い方は文字列の開始の
26003652マッチなので。
26013653
2602=item C<(?|pattern)>
3654=item C<(?|I<pattern>)>
26033655X<(?|)> X<Branch reset>
26043656
26053657=begin original
26063658
26073659This is the "branch reset" pattern, which has the special property
26083660that the capture groups are numbered from the same starting point
26093661in each alternation branch. It is available starting from perl 5.10.0.
26103662
26113663=end original
26123664
26133665これは各代替分岐において捕捉グループを同じ番号から始める特殊な
26143666属性を持っている、「ブランチリセット(branch reset)」パターンです。
26153667これは perl 5.10.0 から提供されています。
26163668
26173669=begin original
26183670
26193671Capture groups are numbered from left to right, but inside this
26203672construct the numbering is restarted for each branch.
26213673
26223674=end original
26233675
26243676捕捉グループは左から右へと番号が振られますが、この構成子の内側では
26253677各分岐毎に番号はリセットされます。
26263678
26273679=begin original
26283680
26293681The numbering within each branch will be as normal, and any groups
26303682following this construct will be numbered as though the construct
26313683contained only one branch, that being the one with the most capture
26323684groups in it.
26333685
26343686=end original
26353687
26363688各分岐内での番号付けは通常通りに行われ、この構成子の後に続くグループは
26373689その中で捕捉グループが一番多かった分岐のみが
26383690格納されていたかのように番号付けされていきます。
26393691
26403692=begin original
26413693
26423694This construct is useful when you want to capture one of a
26433695number of alternative matches.
26443696
26453697=end original
26463698
26473699この構成子はいくつかの代替マッチングの1つを捕捉したいときに便利です。
26483700
26493701=begin original
26503702
26513703Consider the following pattern. The numbers underneath show in
26523704which group the captured content will be stored.
26533705
26543706=end original
26553707
26563708以下のパターンを想像してみてください。
26573709下側の番号は内容の格納されるグループを示します。
26583710
2659 # before ---------------branch-reset----------- after
3711 # before ---------------branch-reset----------- after
26603712 / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x
2661 # 1 2 2 3 2 3 4
3713 # 1 2 2 3 2 3 4
26623714
26633715=begin original
26643716
2665Be careful when using the branch reset pattern in combination with
3717Be careful when using the branch reset pattern in combination with
2666named captures. Named captures are implemented as being aliases to
3718named captures. Named captures are implemented as being aliases to
26673719numbered groups holding the captures, and that interferes with the
26683720implementation of the branch reset pattern. If you are using named
26693721captures in a branch reset pattern, it's best to use the same names,
26703722in the same order, in each of the alternations:
26713723
26723724=end original
26733725
26743726名前付き捕捉と枝リセットパターンを組み合わせて使うときには注意してください。
26753727名前付き捕捉は捕捉を保持している番号付きグループへの別名として
26763728実装されていて、枝リセットパターンの実装を妨害します。
26773729枝リセットパターンで名前付き捕捉を使うときは、それぞれの代替で
26783730同じ名前を同じ順番で使うのが最良です:
26793731
26803732 /(?| (?<a> x ) (?<b> y )
26813733 | (?<a> z ) (?<b> w )) /x
26823734
26833735=begin original
26843736
26853737Not doing so may lead to surprises:
26863738
26873739=end original
26883740
26893741そうしないと驚くことになります:
26903742
26913743 "12" =~ /(?| (?<a> \d+ ) | (?<b> \D+))/x;
2692 say $+ {a}; # Prints '12'
3744 say $+{a}; # Prints '12'
2693 say $+ {b}; # *Also* prints '12'.
3745 say $+{b}; # *Also* prints '12'.
26943746
26953747=begin original
26963748
26973749The problem here is that both the group named C<< a >> and the group
26983750named C<< b >> are aliases for the group belonging to C<< $1 >>.
26993751
27003752=end original
27013753
27023754ここでの問題は、C<< a >> という名前のグループと C<< b >> という名前の
27033755が両方ともグループ C<< $1 >> への別名であることです。
27043756
2705=item Look-Around Assertions
3757=item Lookaround Assertions
27063758X<look-around assertion> X<lookaround assertion> X<look-around> X<lookaround>
27073759
27083760=begin original
27093761
2710Look-around assertions are zero-width patterns which match a specific
3762Lookaround assertions are zero-width patterns which match a specific
27113763pattern without including it in C<$&>. Positive assertions match when
27123764their subpattern matches, negative assertions match when their subpattern
2713fails. Look-behind matches text up to the current match position,
3765fails. Lookbehind matches text up to the current match position,
2714look-ahead matches text following the current match position.
3766lookahead matches text following the current match position.
27153767
27163768=end original
27173769
27183770先読み及び後読みの言明(assertion)は C<$&> の中に
27193771含めない特定のパターンにマッチングするゼロ幅のパターンです。
27203772正の言明はその部分パターンがマッチングしたときにマッチングし、
27213773負の言明はその部分パターンが失敗したときにマッチングします。
27223774後読みのマッチングは今のマッチング位置までのテキストにマッチングし、
27233775先読みの言明は今のマッチング位置の先にあるテキストにマッチングします。
27243776
27253777=over 4
27263778
2727=item C<(?=pattern)>
3779=item C<(?=I<pattern>)>
2728X<(?=)> X<look-ahead, positive> X<lookahead, positive>
27293780
3781=item C<(*pla:I<pattern>)>
3782
3783=item C<(*positive_lookahead:I<pattern>)>
3784X<(?=)>
3785X<(*pla>
3786X<(*positive_lookahead>
3787X<look-ahead, positive> X<lookahead, positive>
3788
27303789=begin original
27313790
2732A zero-width positive look-ahead assertion. For example, C</\w+(?=\t)/>
3791A zero-width positive lookahead assertion. For example, C</\w+(?=\t)/>
27333792matches a word followed by a tab, without including the tab in C<$&>.
27343793
27353794=end original
27363795
27373796ゼロ幅の正の先読み言明。
27383797例えば、C</\w+(?=\t)/> はタブが続く単語にマッチングしますが、タブは
27393798C<$&> に含まれません。
27403799
2741=item C<(?!pattern)>
3800=begin original
2742X<(?!)> X<look-ahead, negative> X<lookahead, negative>
27433801
3802The alphabetic forms are experimental; using them yields a warning in the
3803C<experimental::alpha_assertions> category.
3804
3805=end original
3806
3807英字形式は実験的です; これらを使うと
3808C<experimental::alpha_assertions> カテゴリの警告が出ます。
3809
3810=item C<(?!I<pattern>)>
3811
3812=item C<(*nla:I<pattern>)>
3813
3814=item C<(*negative_lookahead:I<pattern>)>
3815X<(?!)>
3816X<(*nla>
3817X<(*negative_lookahead>
3818X<look-ahead, negative> X<lookahead, negative>
3819
27443820=begin original
27453821
2746A zero-width negative look-ahead assertion. For example C</foo(?!bar)/>
3822A zero-width negative lookahead assertion. For example C</foo(?!bar)/>
27473823matches any occurrence of "foo" that isn't followed by "bar". Note
2748however that look-ahead and look-behind are NOT the same thing. You cannot
3824however that lookahead and lookbehind are NOT the same thing. You cannot
2749use this for look-behind.
3825use this for lookbehind.
27503826
27513827=end original
27523828
27533829ゼロ幅の負の先読み言明。
27543830例えば C</foo(?!bar)/> は "bar" が続かない全ての "foo" にマッチングします。
27553831しかしながら先読みと後読みは同じ物では ない点に注意してください。
27563832これを後読みに使うことはできません。
27573833
27583834=begin original
27593835
27603836If you are looking for a "bar" that isn't preceded by a "foo", C</(?!foo)bar/>
27613837will not do what you want. That's because the C<(?!foo)> is just saying that
27623838the next thing cannot be "foo"--and it's not, it's a "bar", so "foobar" will
2763match. Use look-behind instead (see below).
3839match. Use lookbehind instead (see below).
27643840
27653841=end original
27663842
27673843もし "foo" が前にない "bar" を探しているのなら、
27683844C</(?!foo)bar/> では欲しい物にはなりません。
27693845なぜなら C<(?!foo)> は次のものが "foo" ではないとだけいっているのです --
27703846そしてそうではなく、そこには "bar" があるので、"foobar" はマッチングします。
27713847(後述の) 後方参照を使ってください。
27723848
2773=item C<(?<=pattern)> C<\K>
3849=begin original
2774X<(?<=)> X<look-behind, positive> X<lookbehind, positive> X<\K>
27753850
3851The alphabetic forms are experimental; using them yields a warning in the
3852C<experimental::alpha_assertions> category.
3853
3854=end original
3855
3856英字形式は実験的です; これらを使うと
3857C<experimental::alpha_assertions> カテゴリの警告が出ます。
3858
3859=item C<(?<=I<pattern>)>
3860
3861=item C<\K>
3862
3863=item C<(*plb:I<pattern>)>
3864
3865=item C<(*positive_lookbehind:I<pattern>)>
3866X<(?<=)>
3867X<(*plb>
3868X<(*positive_lookbehind>
3869X<look-behind, positive> X<lookbehind, positive> X<\K>
3870
27763871=begin original
27773872
2778A zero-width positive look-behind assertion. For example, C</(?<=\t)\w+/>
3873A zero-width positive lookbehind assertion. For example, C</(?<=\t)\w+/>
27793874matches a word that follows a tab, without including the tab in C<$&>.
2780Works only for fixed-width look-behind.
27813875
27823876=end original
27833877
27843878ゼロ幅の正の後読みの言明。
27853879例えば、C</(?<=\t)\w+/> は
27863880タブに続く単語にマッチングしますが、タブは $& に 含まれません。
2787固定幅の後読みのみが動作します。
27883881
27893882=begin original
27903883
2791There is a special form of this construct, called C<\K>, which causes the
3884Prior to Perl 5.30, it worked only for fixed-width lookbehind, but
3885starting in that release, it can handle variable lengths from 1 to 255
3886characters as an experimental feature. The feature is enabled
3887automatically if you use a variable length lookbehind assertion, but
3888will raise a warning at pattern compilation time, unless turned off, in
3889the C<experimental::vlb> category. This is to warn you that the exact
3890behavior is subject to change should feedback from actual use in the
3891field indicate to do so; or even complete removal if the problems found
3892are not practically surmountable. You can achieve close to pre-5.30
3893behavior by fatalizing warnings in this category.
3894
3895=end original
3896
3897Perl 5.30 より前では、固定幅の後読みのみが動作しますが、このリリースからは、
3898実験的な機能として 1 から 255 文字の可変長を処理できます。
3899可変長の後読みの言明を使用すると、この機能は自動的に有効になりますが、
3900C<experimental::vlb> カテゴリでオフにしない限り、パターンのコンパイル時に
3901警告が表示されます。
3902これは、フィールドでの実際の使用からのフィードバックが変更することを
3903示していた場合、正確な動作が変更される可能性があることを警告するためです;
3904または、検出された問題が実際に解決できない場合は完全に
3905削除されることもあります。
3906このカテゴリの警告を致命的エラーにすることによって、5.30 以前に
3907近い振る舞いを実現できます。
3908
3909=begin original
3910
3911There is a special form of this construct, called C<\K>
3912(available since Perl 5.10.0), which causes the
27923913regex engine to "keep" everything it had matched prior to the C<\K> and
2793not include it in C<$&>. This effectively provides variable-length
3914not include it in C<$&>. This effectively provides non-experimental
2794look-behind. The use of C<\K> inside of another look-around assertion
3915variable-length lookbehind of any length.
2795is allowed, but the behaviour is currently not well defined.
27963916
27973917=end original
27983918
2799C<\K> というこの構成子の特殊な形式もあります
3919(Perl 5.10.0 から利用可能な) C<\K> というこの構成子の特殊な形式もあります;
28003920これは正規表現エンジンに対してそれが C<\K> までにマッチングした
28013921すべてのものを"取っておいて"、C<$&> には含めないようにさせます。
2802これは事実上可変長の後読みを提供します。
3922これは事実上実験的でない任意の長さの可変長の後読みを提供します。
3923
3924=begin original
3925
3926And, there is a technique that can be used to handle variable length
3927lookbehinds on earlier releases, and longer than 255 characters. It is
3928described in
3929L<http://www.drregex.com/2019/02/variable-length-lookbehinds-actually.html>.
3930
3931=end original
3932
3933そして、以前のリリースや、255 文字より長い可変長後ろ読みを扱うのに使える
3934テクニックがあります。
3935これは
3936L<http://www.drregex.com/2019/02/variable-length-lookbehinds-actually.html> に
3937記述されています。
3938
3939=begin original
3940
3941Note that under C</i>, a few single characters match two or three other
3942characters. This makes them variable length, and the 255 length applies
3943to the maximum number of characters in the match. For
3944example C<qr/\N{LATIN SMALL LETTER SHARP S}/i> matches the sequence
3945C<"ss">. Your lookbehind assertion could contain 127 Sharp S
3946characters under C</i>, but adding a 128th would generate a compilation
3947error, as that could match 256 C<"s"> characters in a row.
3948
3949=end original
3950
3951C</i> の下では、いくつかの単一の文字が 2 または 3 の他の文字に
3952マッチングすることに注意してください。
3953これは後読みを可変長にし、長さ 255 はマッチングの中の文字の最大数に
3954適用されます。
3955例えば、C<qr/\N{LATIN SMALL LETTER SHARP S}/i> は並び C<"ss"> に
3956マッチングします。
3957あなたの後読み言明は C</i> の下で 127 個の Sharp S 文字を含むことは
3958できますが、128 個目を追加するとコンパイルエラーが生成されます;
3959これは 256 個連続した C<"s"> 文字にマッチングすることがあるからです。
3960
3961=begin original
3962
3963The use of C<\K> inside of another lookaround assertion
3964is allowed, but the behaviour is currently not well defined.
3965
3966=end original
3967
28033968他の先読み及び後読みの言明の中での利用も可能ですが、その振る舞いは
28043969今のところあまり定義されていません。
28053970
28063971=begin original
28073972
28083973For various reasons C<\K> may be significantly more efficient than the
28093974equivalent C<< (?<=...) >> construct, and it is especially useful in
28103975situations where you want to efficiently remove something following
28113976something else in a string. For instance
28123977
28133978=end original
28143979
28153980いくつかの理由から、C<\K> は等価な C<< (?<=...) >>
28163981構成子より非常に効率的で、文字列の中で何かに続いている何かを効率的に
28173982取り除きたいようなシチュエーションで効果的に役立ちます。
28183983例えば
28193984
28203985 s/(foo)bar/$1/g;
28213986
28223987=begin original
28233988
28243989can be rewritten as the much more efficient
28253990
28263991=end original
28273992
28283993次のようにより効率的に書き直せます
28293994
28303995 s/foo\Kbar//g;
28313996
2832=item C<(?<!pattern)>
3997=begin original
2833X<(?<!)> X<look-behind, negative> X<lookbehind, negative>
28343998
3999Use of the non-greedy modifier C<"?"> may not give you the expected
4000results if it is within a capturing group within the construct.
4001
4002=end original
4003
4004非貪欲修飾子 C<"?"> の使用は、この構文の中の捕捉グループの中では
4005想定される結果にならないかもしれません。
4006
28354007=begin original
28364008
2837A zero-width negative look-behind assertion. For example C</(?<!bar)foo/>
4009The alphabetic forms (not including C<\K> are experimental; using them
2838matches any occurrence of "foo" that does not follow "bar". Works
4010yields a warning in the C<experimental::alpha_assertions> category.
2839only for fixed-width look-behind.
28404011
28414012=end original
28424013
4014(C<\K> を除く) 英字形式は実験的です; これらを使うと
4015C<experimental::alpha_assertions> カテゴリの警告が出ます。
4016
4017=item C<(?<!I<pattern>)>
4018
4019=item C<(*nlb:I<pattern>)>
4020
4021=item C<(*negative_lookbehind:I<pattern>)>
4022X<(?<!)>
4023X<(*nlb>
4024X<(*negative_lookbehind>
4025X<look-behind, negative> X<lookbehind, negative>
4026
4027=begin original
4028
4029A zero-width negative lookbehind assertion. For example C</(?<!bar)foo/>
4030matches any occurrence of "foo" that does not follow "bar".
4031
4032=end original
4033
28434034ゼロ幅の負の後読みの言明。
28444035例えば C</(?<!bar)foo/> は "bar" に続いていない任意の "foo" に
28454036マッチングします。
2846固定幅の後読みのみが動作します。
28474037
4038=begin original
4039
4040Prior to Perl 5.30, it worked only for fixed-width lookbehind, but
4041starting in that release, it can handle variable lengths from 1 to 255
4042characters as an experimental feature. The feature is enabled
4043automatically if you use a variable length lookbehind assertion, but
4044will raise a warning at pattern compilation time, unless turned off, in
4045the C<experimental::vlb> category. This is to warn you that the exact
4046behavior is subject to change should feedback from actual use in the
4047field indicate to do so; or even complete removal if the problems found
4048are not practically surmountable. You can achieve close to pre-5.30
4049behavior by fatalizing warnings in this category.
4050
4051=end original
4052
4053Perl 5.30 より前では、固定幅の後読みのみが動作しますが、このリリースからは、
4054実験的な機能として 1 から 255 文字の可変長を処理できます。
4055可変長の後読みの言明を使用すると、この機能は自動的に有効になりますが、
4056C<experimental::vlb> カテゴリでオフにしない限り、パターンのコンパイル時に
4057警告が表示されます。
4058これは、フィールドでの実際の使用からのフィードバックが変更することを
4059示していた場合、正確な動作が変更される可能性があることを警告するためです;
4060または、検出された問題が実際に解決できない場合は完全に
4061削除されることもあります。
4062このカテゴリの警告を致命的エラーにすることによって、5.30 以前に
4063近い振る舞いを実現できます。
4064
4065=begin original
4066
4067There is a technique that can be used to handle variable length
4068lookbehinds on earlier releases, and longer than 255 characters. It is
4069described in
4070L<http://www.drregex.com/2019/02/variable-length-lookbehinds-actually.html>.
4071
4072=end original
4073
4074以前のリリースや、255 文字より長い可変長後ろ読みを扱うのに使える
4075テクニックがあります。
4076これは
4077L<http://www.drregex.com/2019/02/variable-length-lookbehinds-actually.html> に
4078記述されています。
4079
4080=begin original
4081
4082Note that under C</i>, a few single characters match two or three other
4083characters. This makes them variable length, and the 255 length applies
4084to the maximum number of characters in the match. For
4085example C<qr/\N{LATIN SMALL LETTER SHARP S}/i> matches the sequence
4086C<"ss">. Your lookbehind assertion could contain 127 Sharp S
4087characters under C</i>, but adding a 128th would generate a compilation
4088error, as that could match 256 C<"s"> characters in a row.
4089
4090=end original
4091
4092C</i> の下では、いくつかの単一の文字が 2 または 3 の他の文字に
4093マッチングすることに注意してください。
4094これは後読みを可変長にし、長さ 255 はマッチングの中の文字の最大数に
4095適用されます。
4096例えば、C<qr/\N{LATIN SMALL LETTER SHARP S}/i> は並び C<"ss"> に
4097マッチングします。
4098あなたの後読み言明は C</i> の下で 127 個の Sharp S 文字を含むことは
4099できますが、128 個目を追加するとコンパイルエラーが生成されます;
4100これは 256 個連続した C<"s"> 文字にマッチングすることがあるからです。
4101
4102=begin original
4103
4104Use of the non-greedy modifier C<"?"> may not give you the expected
4105results if it is within a capturing group within the construct.
4106
4107=end original
4108
4109非貪欲修飾子 C<"?"> の使用は、この構文の中の捕捉グループの中では
4110想定される結果にならないかもしれません。
4111
4112=begin original
4113
4114The alphabetic forms are experimental; using them yields a warning in the
4115C<experimental::alpha_assertions> category.
4116
4117=end original
4118
4119英字形式は実験的です; これらを使うと
4120C<experimental::alpha_assertions> カテゴリの警告が出ます。
4121
28484122=back
28494123
2850=item C<(?'NAME'pattern)>
4124=item C<< (?<I<NAME>>I<pattern>) >>
28514125
2852=item C<< (?<NAME>pattern) >>
4126=item C<(?'I<NAME>'I<pattern>)>
28534127X<< (?<NAME>) >> X<(?'NAME')> X<named capture> X<capture>
28544128
28554129=begin original
28564130
28574131A named capture group. Identical in every respect to normal capturing
28584132parentheses C<()> but for the additional fact that the group
28594133can be referred to by name in various regular expression
2860constructs (like C<\g{NAME}>) and can be accessed by name
4134constructs (like C<\g{I<NAME>}>) and can be accessed by name
28614135after a successful match via C<%+> or C<%->. See L<perlvar>
28624136for more details on the C<%+> and C<%-> hashes.
28634137
28644138=end original
28654139
28664140名前付の捕捉グループ。
28674141通常のキャプチャかっこ C<()> と同様ですがそれに加えて、
2868グループは(C<\g{NAME}> のように) 様々な正規表現構文で名前で参照でき、
4142グループは(C<\g{I<NAME>}> のように) 様々な正規表現構文で名前で参照でき、
28694143マッチングに成功したあと C<%+> を C<%-> を使って名前によって
28704144アクセスできます。
28714145C<%+> 及び C<%-> ハッシュに関する詳細は L<perlvar> を
28724146参照してください。
28734147
28744148=begin original
28754149
2876If multiple distinct capture groups have the same name then the
4150If multiple distinct capture groups have the same name, then
2877$+{NAME} will refer to the leftmost defined group in the match.
4151C<$+{I<NAME>}> will refer to the leftmost defined group in the match.
28784152
28794153=end original
28804154
28814155複数の異なる捕捉グループが同じ名前を持っていたときには
2882$+{NAME} はマッチングの中で一番左で定義されたグループを参照します。
4156C<$+{I<NAME>}> はマッチングの中で一番左で定義されたグループを参照します。
28834157
28844158=begin original
28854159
2886The forms C<(?'NAME'pattern)> and C<< (?<NAME>pattern) >> are equivalent.
4160The forms C<(?'I<NAME>'I<pattern>)> and C<< (?<I<NAME>>I<pattern>) >>
4161are equivalent.
28874162
28884163=end original
28894164
2890二つの形式 C<(?'NAME'pattern)> 及び C<< (?<NAME>pattern) >> は等価です。
4165二つの形式 C<(?'I<NAME>'I<pattern>)> C<< (?<I<NAME>>I<pattern>) >> は
4166等価です。
28914167
28924168=begin original
28934169
28944170B<NOTE:> While the notation of this construct is the same as the similar
28954171function in .NET regexes, the behavior is not. In Perl the groups are
28964172numbered sequentially regardless of being named or not. Thus in the
28974173pattern
28984174
28994175=end original
29004176
29014177B<補足:> これを構成する記法は 類似していている .NET での正規表現と
29024178同じですが、振る舞いは異なります。
29034179Perl ではグループは名前がついているかどうかにかかわらず順番に番号が
29044180振られます。
29054181従って次のパターンにおいて
29064182
29074183 /(x)(?<foo>y)(z)/
29084184
29094185=begin original
29104186
2911$+{foo} will be the same as $2, and $3 will contain 'z' instead of
4187C<$+{foo}> will be the same as C<$2>, and C<$3> will contain 'z' instead of
29124188the opposite which is what a .NET regex hacker might expect.
29134189
29144190=end original
29154191
2916$+{foo} は $2 と同じであり、$3 には .NET 正規表現に
4192C<$+{foo}>C<$2> と同じであり、C<$3> には .NET 正規表現に
29174193慣れた人が予測するのとは異なり 'z' が含まれます。
29184194
29194195=begin original
29204196
2921Currently NAME is restricted to simple identifiers only.
4197Currently I<NAME> is restricted to simple identifiers only.
29224198In other words, it must match C</^[_A-Za-z][_A-Za-z0-9]*\z/> or
29234199its Unicode extension (see L<utf8>),
29244200though it isn't extended by the locale (see L<perllocale>).
29254201
29264202=end original
29274203
2928現在のところ NAME はシンプルな識別子のみに制限されています。
4204現在のところ I<NAME> はシンプルな識別子のみに制限されています。
29294205言い換えると、C</^[_A-Za-z][_A-Za-z0-9]*\z/> または
29304206その Unicode 拡張にマッチングしなければなりません
29314207(L<utf8> も参照); しかしロケールでは拡張されません
29324208(L<perllocale> 参照)。
29334209
29344210=begin original
29354211
29364212B<NOTE:> In order to make things easier for programmers with experience
2937with the Python or PCRE regex engines, the pattern C<< (?PE<lt>NAMEE<gt>pattern) >>
4213with the Python or PCRE regex engines, the pattern C<<
2938may be used instead of C<< (?<NAME>pattern) >>; however this form does not
4214(?PE<lt>I<NAME>E<gt>I<pattern>) >>
4215may be used instead of C<< (?<I<NAME>>I<pattern>) >>; however this form does not
29394216support the use of single quotes as a delimiter for the name.
29404217
29414218=end original
29424219
29434220B<補足:> Python や PCRE 正規表現エンジンになれたプログラマが
2944楽になるように、C<< (?<NAME>pattern) >> の代わりに
4221楽になるように、C<< (?<I<NAME>>I<pattern>) >> の代わりに
2945C<< (?PE<lt>NAMEE<gt>pattern) >> のパターンを使うことも
4222C<< (?PE<lt>I<NAME>E<gt>I<pattern>) >> のパターンを使うことも
29464223できます; しかしこの形式は名前のデリミタとして
29474224シングルクォートの使用はサポートされていません。
29484225
2949=item C<< \k<NAME> >>
4226=item C<< \k<I<NAME>> >>
29504227
2951=item C<< \k'NAME' >>
4228=item C<< \k'I<NAME>' >>
29524229
29534230=begin original
29544231
29554232Named backreference. Similar to numeric backreferences, except that
29564233the group is designated by name and not number. If multiple groups
29574234have the same name then it refers to the leftmost defined group in
29584235the current match.
29594236
29604237=end original
29614238
29624239名前による後方参照。
29634240数値によってではなく名前によってグループを指定する点を除いて、名前による
29644241後方参照と似ています。
29654242もし同じ名前の複数のグループがあったときには現在のマッチングで
29664243一番左に定義されているグループを参照します。
29674244
29684245=begin original
29694246
2970It is an error to refer to a name not defined by a C<< (?<NAME>) >>
4247It is an error to refer to a name not defined by a C<< (?<I<NAME>>) >>
29714248earlier in the pattern.
29724249
29734250=end original
29744251
2975パターン内で C<< (?<NAME>) >> によって定義されていない名前を
4252パターン内で C<< (?<I<NAME>>) >> によって定義されていない名前を
29764253参照するとエラーになります。
29774254
29784255=begin original
29794256
29804257Both forms are equivalent.
29814258
29824259=end original
29834260
29844261両方の形式とも等価です。
29854262
29864263=begin original
29874264
29884265B<NOTE:> In order to make things easier for programmers with experience
2989with the Python or PCRE regex engines, the pattern C<< (?P=NAME) >>
4266with the Python or PCRE regex engines, the pattern C<< (?P=I<NAME>) >>
2990may be used instead of C<< \k<NAME> >>.
4267may be used instead of C<< \k<I<NAME>> >>.
29914268
29924269=end original
29934270
29944271B<補足:> Python や PCRE 正規表現エンジンになれたプログラマが楽に
2995なるように、C<< \k<NAME> >> の代わりに
4272なるように、C<< \k<I<NAME>> >> の代わりに
2996C<< (?P=NAME) >> のパターンを使うこともできます。
4273C<< (?P=I<NAME>) >> のパターンを使うこともできます。
29974274
2998=item C<(?{ code })>
4275=item C<(?{ I<code> })>
29994276X<(?{})> X<regex, code in> X<regexp, code in> X<regular expression, code in>
30004277
30014278=begin original
30024279
3003B<WARNING>: This extended regular expression feature is considered
4280B<WARNING>: Using this feature safely requires that you understand its
3004experimental, and may be changed without notice. Code executed that
4281limitations. Code executed that has side effects may not perform identically
3005has side effects may not perform identically from version to version
4282from version to version due to the effect of future optimisations in the regex
3006due to the effect of future optimisations in the regex engine.
4283engine. For more information on this, see L</Embedded Code Execution
4284Frequency>.
30074285
30084286=end original
30094287
3010B<警告>: この拡張正規表現の機能は実験的なもと考えられおり、
4288B<警告>: この機能を安全に使うに、そ制限につい理解することが必要です。
3011また通知なしに変更されるかもしれません。
30124289副作用を持つコードの実行は今後の正規表現エンジンの最適化の影響で
30134290バージョン間で必ずしも同じになるとは限らないでしょう。
4291これに関するさらなる情報については、L</Embedded Code Execution Frequency> を
4292参照してください。
30144293
30154294=begin original
30164295
3017This zero-width assertion evaluates any embedded Perl code. It
4296This zero-width assertion executes any embedded Perl code. It always
3018always succeeds, and its C<code> is not interpolated. Currently,
4297succeeds, and its return value is set as C<$^R>.
3019the rules to determine where the C<code> ends are somewhat convoluted.
30204298
30214299=end original
30224300
3023このゼロ幅アサーションは埋め込まれた任意の Perl コードを評価します。
4301このゼロ幅アサーションは埋め込まれた任意の Perl コードを実行します。
3024これは常に(正規表現として)成功し、その C<code> は埋め込まれません
4302これは常に(正規表現として)成功し、返り値は C<$^R> に設定されま
3025今のところ、C<code> が終わる場所を認識するルールは少々複雑です。
30264303
30274304=begin original
30284305
3029This feature can be used together with the special variable C<$^N> to
4306In literal patterns, the code is parsed at the same time as the
3030capture the results of submatches in variables without having to keep
4307surrounding code. While within the pattern, control is passed temporarily
3031track of the number of nested parentheses. For example:
4308back to the perl parser, until the logically-balancing closing brace is
4309encountered. This is similar to the way that an array index expression in
4310a literal string is handled, for example
30324311
30334312=end original
30344313
3035この機能では一緒にネストしたかっこの数を数えなくとも1つ前
4314リテラルなパターンではこのコードは周りコードと同時にパースされます。
3036マッチング結果をキャプチャ特殊変数 C<$^N> を使うとがきます。
4315のパターンの中は、
3037例えば:
4316論理的にバランスが取れる閉じかっこが現れるまで、
4317制御文字は一時的に perl パーサーに渡されます。
4318これはリテラル文字列の中の配列添字表現の扱われ方と似ています; 例えば:
30384319
3039 $_ = "The brown fox jumps over the lazy dog";
4320 "abc$array[ 1 + f('[') + g()]def"
3040 /the (\S+)(?{ $color = $^N }) (\S+)(?{ $animal = $^N })/i;
3041 print "color = $color, animal = $animal\n";
30424321
30434322=begin original
30444323
3045Inside the C<(?{...})> block, C<$_> refers to the string the regular
4324In particular, braces do not need to be balanced:
4325
4326=end original
4327
4328特に、中かっこはバランスが取れている必要はありません:
4329
4330 s/abc(?{ f('{'); })/def/
4331
4332=begin original
4333
4334Even in a pattern that is interpolated and compiled at run-time, literal
4335code blocks will be compiled once, at perl compile time; the following
4336prints "ABCD":
4337
4338=end original
4339
4340実行時に展開されてコンパイルされるパターンの中であっても、
4341リテラルなコードブロックは perl のコンパイル時に一度だけコンパイルされます;
4342次のものは "ABCD" と表示されます:
4343
4344 print "D";
4345 my $qr = qr/(?{ BEGIN { print "A" } })/;
4346 my $foo = "foo";
4347 /$foo$qr(?{ BEGIN { print "B" } })/;
4348 BEGIN { print "C" }
4349
4350=begin original
4351
4352In patterns where the text of the code is derived from run-time
4353information rather than appearing literally in a source code /pattern/,
4354the code is compiled at the same time that the pattern is compiled, and
4355for reasons of security, C<use re 'eval'> must be in scope. This is to
4356stop user-supplied patterns containing code snippets from being
4357executable.
4358
4359=end original
4360
4361コードのテキストがソースコードの /pattern/ としてリテラルに現れるのではなく、
4362実行時の情報から派生したものであるパターンでは、
4363コードはパターンがコンパイルされるのと同時にコンパイルされ、
4364セキュリティー上の理由により、C<use re 'eval'> が
4365スコープ内になければなりません。
4366これは、実行可能なコード片を含む、ユーザーが提供したパターンを止めるためです。
4367
4368=begin original
4369
4370In situations where you need to enable this with C<use re 'eval'>, you should
4371also have taint checking enabled. Better yet, use the carefully
4372constrained evaluation within a Safe compartment. See L<perlsec> for
4373details about both these mechanisms.
4374
4375=end original
4376
4377C<use re 'eval'> でこれを有効にする必要がある場合、
4378汚染チェックも有効にするべきです。
4379出来れば、Safe 区画の中で注意深く制限された評価を使ってください。
4380これら両方の機構に関する詳細については L<perlsec> を参照してください。
4381
4382=begin original
4383
4384From the viewpoint of parsing, lexical variable scope and closures,
4385
4386=end original
4387
4388パース、レキシカル変数スコープ、クロージャの観点から、
4389
4390 /AAA(?{ BBB })CCC/
4391
4392=begin original
4393
4394behaves approximately like
4395
4396=end original
4397
4398はおよそ次のように振る舞います:
4399
4400 /AAA/ && do { BBB } && /CCC/
4401
4402=begin original
4403
4404Similarly,
4405
4406=end original
4407
4408同様に、
4409
4410 qr/AAA(?{ BBB })CCC/
4411
4412=begin original
4413
4414behaves approximately like
4415
4416=end original
4417
4418はおよそ次のように振る舞います:
4419
4420 sub { /AAA/ && do { BBB } && /CCC/ }
4421
4422=begin original
4423
4424In particular:
4425
4426=end original
4427
4428特に:
4429
4430 { my $i = 1; $r = qr/(?{ print $i })/ }
4431 my $i = 2;
4432 /$r/; # prints "1"
4433
4434=begin original
4435
4436Inside a C<(?{...})> block, C<$_> refers to the string the regular
30464437expression is matching against. You can also use C<pos()> to know what is
30474438the current position of matching within this string.
30484439
30494440=end original
30504441
30514442C<(?{...})> ブロックの中では C<$_> は正規表現をマッチングさせている文字列を
30524443参照します。
30534444C<pos()> を使ってこの文字列で現在のマッチング位置を知ることもできます。
30544445
30554446=begin original
30564447
3057The C<code> is properly scoped in the following sense: If the assertion
4448The code block introduces a new scope from the perspective of lexical
3058is backtracked (compare L<"Backtracking">), all changes introduced after
4449variable declarations, but B<not> from the perspective of C<local> and
3059C<local>ization are undone, so that
4450similar localizing behaviours. So later code blocks within the same
4451pattern will still see the values which were localized in earlier blocks.
4452These accumulated localizations are undone either at the end of a
4453successful match, or if the assertion is backtracked (compare
4454L</"Backtracking">). For example,
30604455
30614456=end original
30624457
3063C<code> は次感じで適切にスコプを持ちます: もしアサーションが
4458コードブロックは、C<local> や同様カル化の振る舞いの観点 B<ではなく>
3064バックトラックされている(L<"Backtracking"> 参照)なら、
4459レキシカル変数宣言観点での新しいスコープを導入します。
3065C<local> されなかた後の全の変更つまり
4460って、同じパターンで後に出てくるコードブロックは
4461前に出てきたローカル化された値が見えるままです。
4462これらの蓄積されたローカル化は、マッチングが成功するか、
4463アサートがバックトラックした時点で巻き戻されます
4464(L</"Backtracking"> と比較してください)。
4465例えば:
30664466
30674467 $_ = 'a' x 8;
30684468 m<
30694469 (?{ $cnt = 0 }) # Initialize $cnt.
30704470 (
30714471 a
30724472 (?{
30734473 local $cnt = $cnt + 1; # Update $cnt,
30744474 # backtracking-safe.
30754475 })
30764476 )*
30774477 aaaa
30784478 (?{ $res = $cnt }) # On success copy to
30794479 # non-localized location.
30804480 >x;
30814481
30824482=begin original
30834483
3084will set C<$res = 4>. Note that after the match, C<$cnt> returns to the globally
4484will initially increment C<$cnt> up to 8; then during backtracking, its
3085introduced value, because the scopes that restrict C<local> operators
4485value will be unwound back to 4, which is the value assigned to C<$res>.
3086are unwound.
4486At the end of the regex execution, C<$cnt> will be wound back to its initial
4487value of 0.
30874488
30884489=end original
30894490
3090は C<$res = 4> を設定します
4491これまず C<$cnt> 8でインクリメントされま;
3091チング後で C<$cnt> はグローバルに設定された値を返します; なぜなら
4492それからバクトラック間に、この値は 4 まで巻き戻され
3092C<local> 演算子で制限されたスコープは巻き戻されるためです。
4493その値が C<$res> に代入されす。
4494正規表現実行の最後では、$cnt は初期値である 0 に巻き戻ります。
30934495
30944496=begin original
30954497
3096This assertion may be used as a C<(?(condition)yes-pattern|no-pattern)>
4498This assertion may be used as the condition in a
3097switch. If I<not> used in this way, the result of evaluation of
3098C<code> is put into the special variable C<$^R>. This happens
3099immediately, so C<$^R> can be used from other C<(?{ code })> assertions
3100inside the same regular expression.
31014499
31024500=end original
31034501
3104このアサーションは C<(?(condition)yes-pattern|no-pattern)> スイッチとして
4502このアサーションは条件として:
3105使われるかもしれません。
3106この方法で使われI<なかった>のなら、C<code> の評価結果は特殊変数 C<$^R> に
4504 (?(condition)yes-pattern|no-pattern)
4505
4506=begin original
4507
4508switch. If I<not> used in this way, the result of evaluation of I<code>
4509is put into the special variable C<$^R>. This happens immediately, so
4510C<$^R> can be used from other C<(?{ I<code> })> assertions inside the same
4511regular expression.
4512
4513=end original
4514
4515スイッチとして使われるかもしれません。
4516この方法で使われI<なかった>のなら、I<code> の評価結果は特殊変数 C<$^R> に
31074517おかれます。
3108これはすぐに行われるので C<$^R> は同じ正規表現内の他の C<?{ code })>
4518これはすぐに行われるので C<$^R> は同じ正規表現内の他の C<?{ I<code> })>
31094519アサーションで使うことができます。
31104520
31114521=begin original
31124522
31134523The assignment to C<$^R> above is properly localized, so the old
31144524value of C<$^R> is restored if the assertion is backtracked; compare
3115L<"Backtracking">.
4525L</"Backtracking">.
31164526
31174527=end original
31184528
31194529この C<$^R> への設定は適切にlocal化されるため、C<$^R> の古い値は
3120バックトラックしたときには復元されます; L<"Backtracking"> を
4530バックトラックしたときには復元されます; L</"Backtracking"> を
31214531見てください。
31224532
31234533=begin original
31244534
3125For reasons of security, this construct is forbidden if the regular
4535Note that the special variable C<$^N> is particularly useful with code
3126expression involves run-time interpolation of variables, unless the
4536blocks to capture the results of submatches in variables without having to
3127perilous C<use re 'eval'> pragma has been used (see L<re>), or the
4537keep track of the number of nested parentheses. For example:
3128variables contain results of the C<qr//> operator (see
3129L<perlop/"qr/STRINGE<sol>msixpodual">).
31304538
31314539=end original
31324540
3133セキュリティ的な理由により、正規表現を実行時に変数から構築することは、
4541特殊変数 C<$^N> は、一緒にネストしたかっこの数を数えずに一つ前の
3134危険な C<use re 'eval'> プラグが使われてい(L<re> 参照)か
4542ッチング結果を捕捉すコードブロックで特に有用です。
3135変数が C<qr//> 演算子(L<perlop/"qr/STRINGE<sol>msixpodual"> 参照)の結果を
3136含んでいる時以外は拒否されます。
3137
3138=begin original
3139
3140This restriction is due to the wide-spread and remarkably convenient
3141custom of using run-time determined strings as patterns. For example:
3142
3143=end original
3144
3145この制限は、実行時に決まる文字列をパターンとして使う、とても広まっていて
3146とても便利な風習のためのものです。
31474543例えば:
31484544
3149 $re = <>;
4545 $_ = "The brown fox jumps over the lazy dog";
3150 chomp $re;
4546 /the (\S+)(?{ $color = $^N }) (\S+)(?{ $animal = $^N })/i;
3151 $string =~ /$re/;
4547 print "color = $color, animal = $animal\n";
31524548
4549=item C<(??{ I<code> })>
4550X<(??{})>
4551X<regex, postponed> X<regexp, postponed> X<regular expression, postponed>
4552
31534553=begin original
31544554
3155Before Perl knew how to execute interpolated code within a pattern,
4555B<WARNING>: Using this feature safely requires that you understand its
3156this operation was completely safe from a security point of view,
4556limitations. Code executed that has side effects may not perform
3157although it could raise an exception from an illegal pattern. If
4557identically from version to version due to the effect of future
3158you turn on the C<use re 'eval'>, though, it is no longer secure,
4558optimisations in the regex engine. For more information on this, see
3159so you should only do so if you are also using taint checking.
4559L</Embedded Code Execution Frequency>.
3160Better yet, use the carefully constrained evaluation within a Safe
3161compartment. See L<perlsec> for details about both these mechanisms.
31624560
31634561=end original
31644562
3165Perl がパターン中にあるコード実行する方法を知る前はの操作は不正な
4563B<警告>: 機能安全に使うには、その制限について理解することが必要です。
3166パターンで例外を発生させはしますがセキュリティ的な視点で完全に安全でした。
4564副作用を持つコドの実行は今後の正規表現エジンの最適化の影響
3167 C<use re 'eval'> を有効しているのら、これもはやセキュア
4565バージョン間で必ずも同じになると限らないしょう。
3168ありません; そして汚染チェックを使っているときだけ行うべきで
4566これるさらなる情報については、L</Embedded Code Execution Frequency> を
3169より良い方法としては、Safe の区画内で注意深制限れた評価を
4567参照してくい。
3170使うべきでしょう。
3171この双方のメカニズムについての詳細は L<perlsec> を参照してください。
31724568
31734569=begin original
31744570
3175B<WARNING>: Use of lexical (C<my>) variables in these blocks is
4571This is a "postponed" regular subexpression. It behaves in I<exactly> the
3176broken. The result is unpredictable and will make perl unstable. The
4572same way as a C<(?{ I<code> })> code block as described above, except that
3177workaround is to use global (C<our>) variables.
4573its return value, rather than being assigned to C<$^R>, is treated as a
4574pattern, compiled if it's a string (or used as-is if its a qr// object),
4575then matched as if it were inserted instead of this construct.
31784576
31794577=end original
31804578
3181B<警告>: これらのブロックでのレキシカル (C<my>) 変数の使用ています。
4579これは「先送りさた」正規部分表現です。
3182結果不確定で、perl を不安定します。
4580これ上述の C<(?{ I<code> })> コードブロックと I<正確> 同じように
3183回避方法グローバル (C<our>) 変数を使うことす。
4581振る舞いますが、その返り値C<$^R> に代入されるのはなく、
4582パターンとして扱われ、
4583それが文字列の場合はコンパイルされ(あるいは qr// オブジェクトの場合は
4584そのまま使われ)、それからこの構文の代わりに挿入されていたかのように
4585マッチングします。
31844586
31854587=begin original
31864588
3187B<WARNING>: In perl 5.12.x and earlier, the regex engine
4589During the matching of this sub-pattern, it has its own set of
3188was not re-entrant, so interpolated code could not
4590captures which are valid during the sub-match, but are discarded once
3189safely invoke the regex engine either directly with
4591control returns to the main pattern. For example, the following matches,
3190C<m//> or C<s///>), or indirectly with functions such as
4592with the inner pattern capturing "B" and matching "BB", while the outer
3191C<split>. Invoking the regex engine in these blocks would make perl
4593pattern captures "A";
3192unstable.
31934594
31944595=end original
31954596
3196B<警告>: Perl 5.12.x 以前では、正規表現エは再入可能ではない
4597副パターのマッチ副マッチングの間有効な独自の捕捉グループを
3197埋め込れたコドかは C<m//> たは C<s///> を使って直接的にでも
4598持ちすが、一旦制御が主パタンに戻ると捨てす。
3198C<split> ような関数を使って間接的にも安全は呼び出せません。
4599例えば、次マッチングは、内側のパターン "B" と "BB" マッチングし、
3199これらブロックで正規表現エジンを起動すると perl が不安定になります
4600一方外側パター "A" を捕捉します;
32004601
3201=item C<(??{ code })>
4602 my $inner = '(.)\1';
3202X<(??{})>
4603 "ABBA" =~ /^(.)(??{ $inner })\1/;
3203X<regex, postponed> X<regexp, postponed> X<regular expression, postponed>
4604 print $1; # prints "A";
32044605
32054606=begin original
32064607
3207B<WARNING>: This extended regular expression feature is considered
4608Note that this means that there is no way for the inner pattern to refer
3208experimental, and may be changed without notice. Code executed that
4609to a capture group defined outside. (The code block itself can use C<$1>,
3209has side effects may not perform identically from version to version
4610I<etc>., to refer to the enclosing pattern's capture groups.) Thus, although
3210due to the effect of future optimisations in the regex engine.
32114611
32124612=end original
32134613
3214B<警告>: こ拡張正規表現の機能は実験的なものと考えらており、
4614内側パターンが外側で定義さた捕捉グループを参照する方法は
3215また通知変更されるかもれません
4615いこと注意てください
3216副作用を持つコードの実行は今後の正規表現エンジンの最適化の影響で
3217バージョン間で必ずしも同じになるとは限らないでしょう。
3218
3219=begin original
3220
3221This is a "postponed" regular subexpression. The C<code> is evaluated
3222at run time, at the moment this subexpression may match. The result
3223of evaluation is considered a regular expression and matched as
3224if it were inserted instead of this construct. Note that this means
3225that the contents of capture groups defined inside an eval'ed pattern
3226are not available outside of the pattern, and vice versa, there is no
3227way for the inner pattern returned from the code block to refer to a
3228capture group defined outside. (The code block itself can use C<$1>, etc.,
3229to refer to the enclosing pattern's capture groups.) Thus,
3230
3231=end original
3232
3233これは「先送りされた」正規部分表現です。
3234C<code> は実行時に評価され、そのときにこの部分表現にマッチングさせます。
3235評価の結果は正規表現として受け取られ、この構成子の代わりに
3236入れられていたかのようにマッチングされます。
3237これは eval されたパターン内部で定義された捕捉グループの内容はパターンの
3238外側では提供されず、そしてその逆も同様になる点に注意してください;
3239コードブロックから返された内側のパターンが
3240外側で定義された捕捉グループを参照する方法はありません。
32414616(コードブロック自体は、内側のパターンの捕捉グループを参照するために
32424617C<$1> などを使えます。)
32434618従って:
32444619
32454620 ('a' x 100)=~/(??{'(.)' x 100})/
32464621
32474622=begin original
32484623
3249B<will> match, it will B<not> set $1.
4624I<will> match, it will I<not> set C<$1> on exit.
32504625
32514626=end original
32524627
3253これはマッチング B<します> が、$1 は設定 B<されません>。
4628これはマッチング I<します> が、終了時に C<$1> は設定 I<されません>。
32544629
32554630=begin original
32564631
3257The C<code> is not interpolated. As before, the rules to determine
3258where the C<code> ends are currently somewhat convoluted.
3259
3260=end original
3261
3262C<code> は埋め込まれません。
3263先の時と同様に C<code> が終了していると決定するルールは少々複雑です。
3264
3265=begin original
3266
32674632The following pattern matches a parenthesized group:
32684633
32694634=end original
32704635
32714636次のパターンはかっこで囲まれたグループにマッチングします:
32724637
32734638 $re = qr{
32744639 \(
32754640 (?:
32764641 (?> [^()]+ ) # Non-parens without backtracking
32774642 |
32784643 (??{ $re }) # Group with matching parens
32794644 )*
32804645 \)
32814646 }x;
32824647
32834648=begin original
32844649
3285See also C<(?PARNO)> for a different, more efficient way to accomplish
4650See also
4651L<C<(?I<PARNO>)>|/(?I<PARNO>) (?-I<PARNO>) (?+I<PARNO>) (?R) (?0)>
4652for a different, more efficient way to accomplish
32864653the same task.
32874654
32884655=end original
32894656
3290同じタスクを行う別の、より効率的な方法として C<(?PARNO)> も
4657同じタスクを行う別の、より効率的な方法として
4658L<C<(?I<PARNO>)>|/(?I<PARNO>) (?-I<PARNO>) (?+I<PARNO>) (?R) (?0)> も
32914659参照してください。
32924660
32934661=begin original
32944662
3295For reasons of security, this construct is forbidden if the regular
4663Executing a postponed regular expression too many times without
3296expression involves run-time interpolation of variables, unless the
4664consuming any input string will also result in a fatal error. The depth
3297perilous C<use re 'eval'> pragma has been used (see L<re>), or the
4665at which that happens is compiled into perl, so it can be changed with a
3298variables contain results of the C<qr//> operator (see
4666custom build.
3299L<perlop/"qrE<sol>STRINGE<sol>msixpodual">).
33004667
33014668=end original
33024669
3303セキュリティ的理由によ正規表現を実行時に変数から構築することは、
4670入力を消費しい多すぎる先送された正規表現を実行するのも
3304危険 C<use re 'eval'> プグマが使われている(L<re> 参照)か
4671致命的ーとなります。
3305変数C<qr//> 演算子(L<perlop/"qr/STRING/imosx"> 参照)結果を
4672これ起きる深度は perl にコンパイルされているで、カスタムビルドで
3306含んでいる時以外は拒否されます。
4673を変更できます。
33074674
3308=begin original
4675=item C<(?I<PARNO>)> C<(?-I<PARNO>)> C<(?+I<PARNO>)> C<(?R)> C<(?0)>
4676X<(?PARNO)> X<(?1)> X<(?R)> X<(?0)> X<(?-1)> X<(?+1)> X<(?-PARNO)> X<(?+PARNO)>
4677X<regex, recursive> X<regexp, recursive> X<regular expression, recursive>
4678X<regex, relative recursion> X<GOSUB> X<GOSTART>
33094679
3310In perl 5.12.x and earlier, because the regex engine was not re-entrant,
3311delayed code could not safely invoke the regex engine either directly with
3312C<m//> or C<s///>), or indirectly with functions such as C<split>.
3313
3314=end original
3315
3316perl 5.12.x 以前では、Perl の正規表現エンジンは再入可能ではないので、
3317遅延されたコードからは C<m//> または C<s///> を使って直接的にでも
3318C<split> のような関数を使って間接的にでも安全には呼び出せません。
3319
33204680=begin original
33214681
3322Recursing deeper than 50 times without consuming any input string will
4682Recursive subpattern. Treat the contents of a given capture buffer in the
3323result in a fatal error. The maximum depth is compiled into perl, so
4683current pattern as an independent subpattern and attempt to match it at
3324changing it requires a custom build.
4684the current position in the string. Information about capture state from
4685the caller for things like backreferences is available to the subpattern,
4686but capture buffers set by the subpattern are not visible to the caller.
33254687
33264688=end original
33274689
3328入力を消費しない 50 回を超える深い再帰は致命的なエラとなります
4690再帰部分パタ
3329最大深度は perl にコンイルされているで、これを変更するには特別に
4691現在のターン与えらた捕捉バッファの内容独立した
3330ビルドする必要があります。
4692部分パターンとして扱って、
4693文字列の現在の位置でマッチングしようとします。
4694後方参照のような呼び出し元からの捕捉状態に関する情報は
4695部分パターンで利用可能ですが、
4696部分パターンで設定された捕捉バッファは呼び出し元には見えません。
33314697
3332=item C<(?PARNO)> C<(?-PARNO)> C<(?+PARNO)> C<(?R)> C<(?0)>
3333X<(?PARNO)> X<(?1)> X<(?R)> X<(?0)> X<(?-1)> X<(?+1)> X<(?-PARNO)> X<(?+PARNO)>
3334X<regex, recursive> X<regexp, recursive> X<regular expression, recursive>
3335X<regex, relative recursion>
3336
33374698=begin original
33384699
3339Similar to C<(??{ code })> except it does not involve compiling any code,
4700Similar to C<(??{ I<code> })> except that it does not involve executing any
3340instead it treats the contents of a capture group as an independent
4701code or potentially compiling a returned pattern string; instead it treats
3341pattern that must match at the current position. Capture groups
4702the part of the current pattern contained within a specified capture group
3342contained by the pattern will have the value as determined by the
4703as an independent pattern that must match at the current position. Also
3343outermost recursion.
4704different is the treatment of capture buffers, unlike C<(??{ I<code> })>
4705recursive patterns have access to their caller's match state, so one can
4706use backreferences safely.
33444707
33454708=end original
33464709
3347コードのコンパイルを伴わなずその代わりに捕捉グループの内容を現在の位置で
4710C<(??{ I<code> })> と似ていますが
3348マッチングすべき独立したパターンて扱う、C<(??{ code })> と似た機能です。
4711コードの実行を伴なわず、返されたパターン文字列をコンパイルもません;
3349パターン内包されている捕捉グループは一番外側再帰として
4712その代わり、指定され捕捉グループに含まれる現在パターンの一部を、
3350決定されるという価値があります。
4713現在の位置でマッチングすべき独立したパターンして扱います。
4714また、捕捉バッファの扱いも異なります;
4715C<(??{ I<code> })> と異なり、再帰パターンはその呼び出し元のマッチング
4716状態にアクセスすることが出来るので、安全に後方参照を使えます。
33514717
33524718=begin original
33534719
3354PARNO is a sequence of digits (not starting with 0) whose value reflects
4720I<PARNO> is a sequence of digits (not starting with 0) whose value reflects
33554721the paren-number of the capture group to recurse to. C<(?R)> recurses to
33564722the beginning of the whole pattern. C<(?0)> is an alternate syntax for
3357C<(?R)>. If PARNO is preceded by a plus or minus sign then it is assumed
4723C<(?R)>. If I<PARNO> is preceded by a plus or minus sign then it is assumed
33584724to be relative, with negative numbers indicating preceding capture groups
33594725and positive ones following. Thus C<(?-1)> refers to the most recently
33604726declared group, and C<(?+1)> indicates the next group to be declared.
33614727Note that the counting for relative recursion differs from that of
33624728relative backreferences, in that with recursion unclosed groups B<are>
33634729included.
33644730
33654731=end original
33664732
3367PARNO はその値が再帰させる捕捉グループのかっこ番号を反映する一連の
4733I<PARNO> はその値が再帰させる捕捉グループのかっこ番号を反映する一連の
33684734数字からなります(そして 0 からは始まりません)。
33694735C<(?R)> はパターン全体の最初から再帰します。
33704736C<(?0)> は C<(?R)> の別の構文です。
3371PARNO の前に正符号または負符号がついていた場合には相対的な位置として
4737I<PARNO> の前に正符号または負符号がついていた場合には相対的な位置として
33724738使われます; 負数であれば前の捕捉グループを、正数であれば続く
33734739捕捉グループを示します。
33744740従って C<(?-1)> は一番最近宣言されたグループを参照し、C<(?+1)> は次に
33754741宣言されるグループを参照します。
33764742相対再帰の数え方は相対後方参照とは違って、グループに閉じていない再帰は
33774743含まB<れる>ことに注意してください,
33784744
33794745=begin original
33804746
3381The following pattern matches a function foo() which may contain
4747The following pattern matches a function C<foo()> which may contain
33824748balanced parentheses as the argument.
33834749
33844750=end original
33854751
33864752以下のパターンは引数にバランスのとれたかっこを含んでいるかもしれない関数
3387foo() にマッチングします。
4753C<foo()> にマッチングします。
33884754
33894755 $re = qr{ ( # paren group 1 (full function)
33904756 foo
33914757 ( # paren group 2 (parens)
33924758 \(
33934759 ( # paren group 3 (contents of parens)
33944760 (?:
33954761 (?> [^()]+ ) # Non-parens without backtracking
33964762 |
33974763 (?2) # Recurse to start of paren group 2
33984764 )*
33994765 )
34004766 \)
34014767 )
34024768 )
34034769 }x;
34044770
34054771=begin original
34064772
34074773If the pattern was used as follows
34084774
34094775=end original
34104776
34114777このパターンを以下のように使うと,
34124778
34134779 'foo(bar(baz)+baz(bop))'=~/$re/
34144780 and print "\$1 = $1\n",
34154781 "\$2 = $2\n",
34164782 "\$3 = $3\n";
34174783
34184784=begin original
34194785
34204786the output produced should be the following:
34214787
34224788=end original
34234789
34244790次のように出力されます:
34254791
34264792 $1 = foo(bar(baz)+baz(bop))
34274793 $2 = (bar(baz)+baz(bop))
34284794 $3 = bar(baz)+baz(bop)
34294795
34304796=begin original
34314797
34324798If there is no corresponding capture group defined, then it is a
3433fatal error. Recursing deeper than 50 times without consuming any input
4799fatal error. Recursing deeply without consuming any input string will
3434string will also result in a fatal error. The maximum depth is compiled
4800also result in a fatal error. The depth at which that happens is
3435into perl, so changing it requires a custom build.
4801compiled into perl, so it can be changed with a custom build.
34364802
34374803=end original
34384804
34394805もし対応する捕捉グループが定義されていなかったときには致命的な
34404806エラーとなります。
3441入力を消費しない 50 回を超える深い再帰も致命的なエラーとなります。
4807入力を消費しない深い再帰も致命的なエラーとなります。
3442最大深度は perl にコンパイルされているので、これを変更するには特別に
4808これが起きる深度は perl にコンパイルされているので、カスタムビルドで
3443ビルドする必要があります。
4809これを変更できます。
34444810
34454811=begin original
34464812
34474813The following shows how using negative indexing can make it
34484814easier to embed recursive patterns inside of a C<qr//> construct
34494815for later use:
34504816
34514817=end original
34524818
34534819以下に後で使うパターンのために、C<qr//> 構成子内で再帰を埋め込むのに
34544820負数の参照を使うとどのように容易になるかを示します:
34554821
34564822 my $parens = qr/(\((?:[^()]++|(?-1))*+\))/;
3457 if (/foo $parens \s+ + \s+ bar $parens/x) {
4823 if (/foo $parens \s+ \+ \s+ bar $parens/x) {
34584824 # do something here...
34594825 }
34604826
34614827=begin original
34624828
34634829B<Note> that this pattern does not behave the same way as the equivalent
34644830PCRE or Python construct of the same form. In Perl you can backtrack into
34654831a recursed group, in PCRE and Python the recursed into group is treated
34664832as atomic. Also, modifiers are resolved at compile time, so constructs
3467like (?i:(?1)) or (?:(?i)(?1)) do not affect how the sub-pattern will
4833like C<(?i:(?1))> or C<(?:(?i)(?1))> do not affect how the sub-pattern will
34684834be processed.
34694835
34704836=end original
34714837
34724838B<補足> このパターンは PCRE や Python での等価な形式の構成子と同じように
34734839振る舞うわけではありません。
34744840Perl においては再帰グループの中にバックトラックできますが、PCRE や
34754841Python ではグループへの再帰はアトミックに扱われます。
3476また、修飾子はコンパイル時に解決されるので、(?i:(?1)) や
4842また、修飾子はコンパイル時に解決されるので、C<(?i:(?1))>
3477(?:(?i)(?1)) といった構成子はサブパターンがどのように処理されたかに
4843C<(?:(?i)(?1))> といった構成子はサブパターンがどのように処理されたかに
34784844影響されません。
34794845
3480=item C<(?&NAME)>
4846=item C<(?&I<NAME>)>
34814847X<(?&NAME)>
34824848
34834849=begin original
34844850
3485Recurse to a named subpattern. Identical to C<(?PARNO)> except that the
4851Recurse to a named subpattern. Identical to C<(?I<PARNO>)> except that the
34864852parenthesis to recurse to is determined by name. If multiple parentheses have
34874853the same name, then it recurses to the leftmost.
34884854
34894855=end original
34904856
34914857名前付きサブパターンへの再帰。
3492再帰するかっこが名前によって決定される点以外は C<(?PARNO)> と等価です。
4858再帰するかっこが名前によって決定される点以外は C<(?I<PARNO>)> と等価です。
34934859もし複数のかっこで同じ名前を持っていた場合には一番左のものに再帰します。
34944860
34954861=begin original
34964862
34974863It is an error to refer to a name that is not declared somewhere in the
34984864pattern.
34994865
35004866=end original
35014867
35024868パターンのどこでも宣言されていない名前の参照はエラーになります。
35034869
35044870=begin original
35054871
35064872B<NOTE:> In order to make things easier for programmers with experience
3507with the Python or PCRE regex engines the pattern C<< (?P>NAME) >>
4873with the Python or PCRE regex engines the pattern C<< (?P>I<NAME>) >>
3508may be used instead of C<< (?&NAME) >>.
4874may be used instead of C<< (?&I<NAME>) >>.
35094875
35104876=end original
35114877
35124878B<補足:> Python または PCRE 正規表現エンジンに慣れているプログラマが
3513簡単になるように C<< (?&NAME) >> の代わりに C<< (?P>NANE) >> を使うことも
4879簡単になるように C<< (?&I<NAME>) >> の代わりに C<< (?P>I<NAME>) >> を
3514できます。
4880使うこともできます。
35154881
3516=item C<(?(condition)yes-pattern|no-pattern)>
4882=item C<(?(I<condition>)I<yes-pattern>|I<no-pattern>)>
35174883X<(?()>
35184884
3519=item C<(?(condition)yes-pattern)>
4885=item C<(?(I<condition>)I<yes-pattern>)>
35204886
35214887=begin original
35224888
3523Conditional expression. Matches C<yes-pattern> if C<condition> yields
4889Conditional expression. Matches I<yes-pattern> if I<condition> yields
3524a true value, matches C<no-pattern> otherwise. A missing pattern always
4890a true value, matches I<no-pattern> otherwise. A missing pattern always
35254891matches.
35264892
35274893=end original
35284894
35294895条件付き式。
3530C<condition> が真なら C<yes-pattern> にマッチングし、さもなければ
4896I<condition> が真なら I<yes-pattern> にマッチングし、さもなければ
3531C<no-pattern> にマッチングします。
4897I<no-pattern> にマッチングします。
35324898パターンがなければ常にマッチングします。
35334899
35344900=begin original
35354901
3536C<(condition)> should be either an integer in
4902C<(I<condition>)> should be one of:
3537parentheses (which is valid if the corresponding pair of parentheses
3538matched), a look-ahead/look-behind/evaluate zero-width assertion, a
4904=end original
3539name in angle brackets or single quotes (which is valid if a group
3540with the given name matched), or the special symbol (R) (true when
4906C<(I<condition>)> は次のいずれかです:
3541evaluated inside of recursion or eval). Additionally the R may be
4908=over 4
4909
4910=item an integer in parentheses
4911
4912(かっこでくるまれた数値)
4913
4914=begin original
4915
4916(which is valid if the corresponding pair of parentheses
4917matched);
4918
4919=end original
4920
4921(対応するかっこ対がマッチングしたときに有効);
4922
4923=item a lookahead/lookbehind/evaluate zero-width assertion;
4924
4925(先読み/後読み/ゼロ幅で評価される言明)
4926
4927=item a name in angle brackets or single quotes
4928
4929(角かっこもしくはシングルクォートでくるまれた名前)
4930
4931=begin original
4932
4933(which is valid if a group with the given name matched);
4934
4935=end original
4936
4937(その名前のグループがマッチングしたときに有効);
4938
4939=item the special symbol C<(R)>
4940
4941(特殊なシンボル C<(R)>)
4942
4943=begin original
4944
4945(true when evaluated inside of recursion or eval). Additionally the
4946C<"R"> may be
35424947followed by a number, (which will be true when evaluated when recursing
3543inside of the appropriate group), or by C<&NAME>, in which case it will
4948inside of the appropriate group), or by C<&I<NAME>>, in which case it will
35444949be true only when evaluated during recursion in the named group.
35454950
35464951=end original
35474952
3548C<(condition)> かっこくるまた数値(対応すかっこ対が
4953(再帰また eval 内評価さていときに真)。
3549マッチングしときに有効)、先読み/後読み/ゼロ幅で評価される言明、角かっこ
4954加えて C<"R"> には数字(対応するループ内で再帰ているときに)、もしくは
3550もしくシングルクォートでくるまれた名前(その名前のグループ
4955C<&I<NAME>>、こちらの時はその名前のグループで再帰している時にのみ真、を
3551マッチングしたときに有効)、特殊なシンボル (R) (再帰または eval 内で
3552評価されているときに真)のいずれかです。
3553加えて R には数字(対応するグループ内で再帰しているときに真)、もしくは
3554C<&NAME>、こちらの時はその名前のグループで再帰している時にのみ真、を
35554956続けることもできます。
35564957
4958=back
4959
35574960=begin original
35584961
35594962Here's a summary of the possible predicates:
35604963
35614964=end original
35624965
35634966可能な述語の要約を次に示します:
35644967
35654968=over 4
35664969
3567=item (1) (2) ...
4970=item C<(1)> C<(2)> ...
35684971
35694972=begin original
35704973
35714974Checks if the numbered capturing group has matched something.
4975Full syntax: C<< (?(1)then|else) >>
35724976
35734977=end original
35744978
35754979その番号の捕捉グループが何かにマッチングしたかどうかを調べます。
4980完全な文法: C<< (?(1)then|else) >>
35764981
3577=item (<NAME>) ('NAME')
4982=item C<(E<lt>I<NAME>E<gt>)> C<('I<NAME>')>
35784983
35794984=begin original
35804985
35814986Checks if a group with the given name has matched something.
4987Full syntax: C<< (?(<name>)then|else) >>
35824988
35834989=end original
35844990
35854991その名前のグループが何かにマッチングしたかどうかを調べます。
4992完全な文法: C<< (?(<name>)then|else) >>
35864993
3587=item (?=...) (?!...) (?<=...) (?<!...)
4994=item C<(?=...)> C<(?!...)> C<(?<=...)> C<(?<!...)>
35884995
35894996=begin original
35904997
3591Checks whether the pattern matches (or does not match, for the '!'
4998Checks whether the pattern matches (or does not match, for the C<"!">
35924999variants).
5000Full syntax: C<< (?(?=I<lookahead>)I<then>|I<else>) >>
35935001
35945002=end original
35955003
3596パターンがマッチングするか (あるいは '!' 版はマッチングしないか) を
5004パターンがマッチングするか (あるいは C<"!"> 版はマッチングしないか) を
35975005チェックします。
5006完全な文法: C<< (?(?=lookahead)then|else) >>
35985007
3599=item (?{ CODE })
5008=item C<(?{ I<CODE> })>
36005009
36015010=begin original
36025011
36035012Treats the return value of the code block as the condition.
5013Full syntax: C<< (?(?{ I<code> })I<then>|I<else>) >>
36045014
36055015=end original
36065016
36075017コードブロックの返り値を条件として扱います。
5018完全な文法: C<< (?(?{ I<code> })I<then>|I<else>) >>
36085019
3609=item (R)
5020=item C<(R)>
36105021
36115022=begin original
36125023
36135024Checks if the expression has been evaluated inside of recursion.
5025Full syntax: C<< (?(R)I<then>|I<else>) >>
36145026
36155027=end original
36165028
36175029式が再帰の中で評価されているかどうかを調べます。
5030完全な文法: C<< (?(R)I<then>|I<else>) >>
36185031
3619=item (R1) (R2) ...
5032=item C<(R1)> C<(R2)> ...
36205033
36215034=begin original
36225035
36235036Checks if the expression has been evaluated while executing directly
36245037inside of the n-th capture group. This check is the regex equivalent of
36255038
36265039=end original
36275040
36285041式がその n 番目の捕捉グループのすぐ内側で実行されているかどうかを調べます。
36295042これは次のものと等価な正規表現です
36305043
36315044 if ((caller(0))[3] eq 'subname') { ... }
36325045
36335046=begin original
36345047
36355048In other words, it does not check the full recursion stack.
36365049
36375050=end original
36385051
36395052言い換えると、これは完全な再帰スタックを調べるわけではありません。
36405053
3641=item (R&NAME)
5054=begin original
36425055
5056Full syntax: C<< (?(R1)I<then>|I<else>) >>
5057
5058=end original
5059
5060完全な文法: C<< (?(R1)I<then>|I<else>) >>
5061
5062=item C<(R&I<NAME>)>
5063
36435064=begin original
36445065
36455066Similar to C<(R1)>, this predicate checks to see if we're executing
36465067directly inside of the leftmost group with a given name (this is the same
3647logic used by C<(?&NAME)> to disambiguate). It does not check the full
5068logic used by C<(?&I<NAME>)> to disambiguate). It does not check the full
36485069stack, but only the name of the innermost active recursion.
5070Full syntax: C<< (?(R&I<name>)I<then>|I<else>) >>
36495071
36505072=end original
36515073
36525074C<(R1)> と似ていて、この述語はその名前のつけられている一番左のグループの
3653すぐ内側で実行されているかどうかをしらべます(一番左は C<(?NAME)> と
5075すぐ内側で実行されているかどうかをしらべます(一番左は C<(?&I<NAME>)> と
36545076同じロジックです)。
36555077これは完全なスタックを調べずに、一番内部のアクティブな再帰の名前だけを
36565078調べます。
5079完全な文法: C<< (?(R&I<name>)I<then>|I<else>) >>
36575080
3658=item (DEFINE)
5081=item C<(DEFINE)>
36595082
36605083=begin original
36615084
36625085In this case, the yes-pattern is never directly executed, and no
36635086no-pattern is allowed. Similar in spirit to C<(?{0})> but more efficient.
36645087See below for details.
5088Full syntax: C<< (?(DEFINE)I<definitions>...) >>
36655089
36665090=end original
36675091
36685092この場合において、yes-pattern は直接は実行されず、no-pattern は
36695093許可されていません。
36705094C<(?{0})> と似ていますがより効率的です。
36715095詳細は次のようになります。
5096完全な文法: C<< (?(DEFINE)I<definitions>...) >>
36725097
36735098=back
36745099
36755100=begin original
36765101
36775102For example:
36785103
36795104=end original
36805105
36815106例:
36825107
36835108 m{ ( \( )?
36845109 [^()]+
36855110 (?(1) \) )
36865111 }x
36875112
36885113=begin original
36895114
36905115matches a chunk of non-parentheses, possibly included in parentheses
36915116themselves.
36925117
36935118=end original
36945119
36955120これはかっこ以外からなる固まりかかっこの中にあるそれらにマッチングします。
36965121
36975122=begin original
36985123
36995124A special form is the C<(DEFINE)> predicate, which never executes its
37005125yes-pattern directly, and does not allow a no-pattern. This allows one to
37015126define subpatterns which will be executed only by the recursion mechanism.
37025127This way, you can define a set of regular expression rules that can be
37035128bundled into any pattern you choose.
37045129
37055130=end original
37065131
37075132C<(DEFINE)> は特殊な形式で、これはその yes-pattern を直接は実行せず、
37085133no-pattern も許可していません。
37095134これは再帰メカニズムの中で利用することでのみ実行されるサブパターンの
37105135定義を許可します。
37115136これによって、選んだパターンと一緒に正規表現ルールを定義できます。
37125137
37135138=begin original
37145139
37155140It is recommended that for this usage you put the DEFINE block at the
37165141end of the pattern, and that you name any subpatterns defined within it.
37175142
37185143=end original
37195144
37205145この使い方において、DEFINE ブロックはパターンの最後におくこと、
37215146そしてそこで定義する全てのサブパターンに名前をつけることが
37225147推奨されています。
37235148
37245149=begin original
37255150
37265151Also, it's worth noting that patterns defined this way probably will
3727not be as efficient, as the optimiser is not very clever about
5152not be as efficient, as the optimizer is not very clever about
37285153handling them.
37295154
37305155=end original
37315156
37325157また、この方法によって定義されるパターンはその処理に関してそんなに
37335158賢い訳ではないので効率的でないことに価値は何もないでしょう。
37345159
37355160=begin original
37365161
37375162An example of how this might be used is as follows:
37385163
37395164=end original
37405165
37415166これをどのように使うかの例を次に示します:
37425167
37435168 /(?<NAME>(?&NAME_PAT))(?<ADDR>(?&ADDRESS_PAT))
37445169 (?(DEFINE)
37455170 (?<NAME_PAT>....)
3746 (?<ADRESS_PAT>....)
5171 (?<ADDRESS_PAT>....)
37475172 )/x
37485173
37495174=begin original
37505175
37515176Note that capture groups matched inside of recursion are not accessible
37525177after the recursion returns, so the extra layer of capturing groups is
37535178necessary. Thus C<$+{NAME_PAT}> would not be defined even though
37545179C<$+{NAME}> would be.
37555180
37565181=end original
37575182
37585183再帰の内側でマッチングした捕捉グループは再帰から戻った後には
37595184アクセスできないため、余分な捕捉グループの
37605185レイヤは必要な点に注意してください。
37615186従って C<$+{NAME}> が定義されていても C<$+{NAME_PAT}> は定義されません。
37625187
37635188=begin original
37645189
37655190Finally, keep in mind that subpatterns created inside a DEFINE block
37665191count towards the absolute and relative number of captures, so this:
37675192
37685193=end original
37695194
37705195最後に、DEFINE ブロックの内側で作られた副パターンは捕捉の絶対及び
37715196相対番号で数えることに注意してください; 従ってこうすると:
37725197
37735198 my @captures = "a" =~ /(.) # First capture
37745199 (?(DEFINE)
37755200 (?<EXAMPLE> 1 ) # Second capture
37765201 )/x;
37775202 say scalar @captures;
37785203
37795204=begin original
37805205
37815206Will output 2, not 1. This is particularly important if you intend to
37825207compile the definitions with the C<qr//> operator, and later
37835208interpolate them in another pattern.
37845209
37855210=end original
37865211
378752121 ではなく 2 を出力します。
37885213これは、C<qr//> 演算子で定義をコンパイルして、
37895214後で他のパターンの中で展開することを意図している場合に特に重要です。
37905215
3791=item C<< (?>pattern) >>
5216=item C<< (?>I<pattern>) >>
5217
5218=item C<< (*atomic:I<pattern>) >>
5219X<(?E<gt>pattern)>
5220X<(*atomic>
37925221X<backtrack> X<backtracking> X<atomic> X<possessive>
37935222
37945223=begin original
37955224
37965225An "independent" subexpression, one which matches the substring
3797that a I<standalone> C<pattern> would match if anchored at the given
5226that a standalone I<pattern> would match if anchored at the given
37985227position, and it matches I<nothing other than this substring>. This
37995228construct is useful for optimizations of what would otherwise be
3800"eternal" matches, because it will not backtrack (see L<"Backtracking">).
5229"eternal" matches, because it will not backtrack (see L</"Backtracking">).
38015230It may also be useful in places where the "grab all you can, and do not
38025231give anything back" semantic is desirable.
38035232
38045233=end original
38055234
3806「独立した」部分式、I<スタンドアロンの> C<pattern> がその場所に
5235「独立した」部分式、スタンドアロンの I<pattern> がその場所に
38075236固定されてマッチングする部分文字列にマッチングし、
38085237I<その文字列以外にはなにも>マッチングしません。
38095238この構成子は他の"外部"マッチングになる最適化に便利です;
3810なぜならこれはバックトラックしないためです(L<"Backtracking"> 参照)。
5239なぜならこれはバックトラックしないためです(L</"Backtracking"> 参照)。
38115240これは "できる限りを取り込んで、後は戻らない"セマンティクスが
38125241必要な場所でも便利です。
38135242
38145243=begin original
38155244
38165245For example: C<< ^(?>a*)ab >> will never match, since C<< (?>a*) >>
38175246(anchored at the beginning of string, as above) will match I<all>
3818characters C<a> at the beginning of string, leaving no C<a> for
5247characters C<"a"> at the beginning of string, leaving no C<"a"> for
38195248C<ab> to match. In contrast, C<a*ab> will match the same as C<a+b>,
38205249since the match of the subgroup C<a*> is influenced by the following
3821group C<ab> (see L<"Backtracking">). In particular, C<a*> inside
5250group C<ab> (see L</"Backtracking">). In particular, C<a*> inside
38225251C<a*ab> will match fewer characters than a standalone C<a*>, since
38235252this makes the tail match.
38245253
38255254=end original
38265255
38275256例: C<< ^(?>a*)ab >> は何もマッチングしません、
38285257なぜなら C<< (?>a*) >> (前述のように、文字列の開始で固定されます)は
3829文字列のはじめにある全ての文字 C<a> にマッチングし、
5258文字列のはじめにある全ての文字 C<"a"> にマッチングし、
3830C<ab> のマッチングのための C<a> を残さないためです。
5259C<ab> のマッチングのための C<"a"> を残さないためです。
38315260対照的に、C<a*ab> は C<a+b> と同じようにマッチングします、
38325261これはサブグループ C<a*> のマッチングは次のグループ C<ab> の影響を
3833受けるためです (L<"Backtracking"> 参照)。
5262受けるためです (L</"Backtracking"> 参照)。
38345263特に、C<a*ab> の中の C<a*> は単独の C<a*> より短い文字にマッチングします;
38355264これによって最後のマッチングが行えるようになります。
38365265
38375266=begin original
38385267
3839C<< (?>pattern) >> does not disable backtracking altogether once it has
5268C<< (?>I<pattern>) >> does not disable backtracking altogether once it has
38405269matched. It is still possible to backtrack past the construct, but not
38415270into it. So C<< ((?>a*)|(?>b*))ar >> will still match "bar".
38425271
38435272=end original
38445273
3845C<< (?>pattern) >> は、一旦マッチングしたら、全くバックトラックを
5274C<< (?>I<pattern>) >> は、一旦マッチングしたら、全くバックトラックを
38465275無効にしません。
38475276未だこの構文の前までバックトラックする可能性はありますが、構文の中に
38485277バックトラックすることはありません。
38495278従って C<< ((?>a*)|(?>b*))ar >> は "bar" にマッチングするままです。
38505279
38515280=begin original
38525281
3853An effect similar to C<< (?>pattern) >> may be achieved by writing
5282An effect similar to C<< (?>I<pattern>) >> may be achieved by writing
3854C<(?=(pattern))\g{-1}>. This matches the same substring as a standalone
5283C<(?=(I<pattern>))\g{-1}>. This matches the same substring as a standalone
38555284C<a+>, and the following C<\g{-1}> eats the matched string; it therefore
38565285makes a zero-length assertion into an analogue of C<< (?>...) >>.
38575286(The difference between these two constructs is that the second one
38585287uses a capturing group, thus shifting ordinals of backreferences
38595288in the rest of a regular expression.)
38605289
38615290=end original
38625291
3863C<< (?>pattern) >> と似た効果は C<(?=(pattern))\g{-1}> でも達成できます。
5292C<< (?>I<pattern>) >> と似た効果は C<(?=(I<pattern>))\g{-1}> でも達成できます。
38645293これは単独の C<a+> と同じ部分文字列にマッチングし、それに続く C<\g{-1}> が
38655294マッチングした文字列を消費します;
38665295これはゼロ幅の言明が C<< (?>...) >> の類似を作るためです。
38675296(この2つの構成子は後者はグループをキャプチャするため、
38685297それに続く正規表現の残りで後方参照の順序をずらす点で違いがあります。)
38695298
38705299=begin original
38715300
38725301Consider this pattern:
38735302
38745303=end original
38755304
38765305次のパターンを考えてみてください:
38775306
38785307 m{ \(
38795308 (
38805309 [^()]+ # x+
38815310 |
38825311 \( [^()]* \)
38835312 )+
38845313 \)
38855314 }x
38865315
38875316=begin original
38885317
38895318That will efficiently match a nonempty group with matching parentheses
38905319two levels deep or less. However, if there is no such group, it
38915320will take virtually forever on a long string. That's because there
38925321are so many different ways to split a long string into several
38935322substrings. This is what C<(.+)+> is doing, and C<(.+)+> is similar
38945323to a subpattern of the above pattern. Consider how the pattern
38955324above detects no-match on C<((()aaaaaaaaaaaaaaaaaa> in several
38965325seconds, but that each extra letter doubles this time. This
38975326exponential performance will make it appear that your program has
38985327hung. However, a tiny change to this pattern
38995328
39005329=end original
39015330
39025331これは 2 段階までのかっこでくるまれた空でないグループに効率的に
39035332マッチングします。
39045333しかしながら、これはマッチングするグループがなかったときに長い
39055334文字列においてはほとんど永遠に戻りません。
39065335これは長い文字列をいくつかの部分文字列に分解する方法がいくつもあるためです。
39075336これは C<(.+)+> が行うことでもあり、C<(.+)+> は このパターンの
39085337部分パターンと似ています。
39095338このパターンが C<((()aaaaaaaaaaaaaaaaaa> にはマッチングしないことを
39105339どうやって検出するかを少し考えてみましょう、
39115340しかしここでは余計な文字を2倍にしてみます。
39125341この指数的なパフォーマンスはプログラムのハングアップとして表面化します。
39135342しかしながら、このパターンに小さな変更をいれてみます,
39145343
39155344 m{ \(
39165345 (
39175346 (?> [^()]+ ) # change x+ above to (?> x+ )
39185347 |
39195348 \( [^()]* \)
39205349 )+
39215350 \)
39225351 }x
39235352
39245353=begin original
39255354
39265355which uses C<< (?>...) >> matches exactly when the one above does (verifying
39275356this yourself would be a productive exercise), but finishes in a fourth
3928the time when used on a similar string with 1000000 C<a>s. Be aware,
5357the time when used on a similar string with 1000000 C<"a">s. Be aware,
39295358however, that, when this construct is followed by a
39305359quantifier, it currently triggers a warning message under
39315360the C<use warnings> pragma or B<-w> switch saying it
39325361C<"matches null string many times in regex">.
39335362
39345363=end original
39355364
39365365これは上で行っているように C<< (?>...) >> マッチングを
39375366使っています(これは自身で確認してみるとよいでしょう)が、
3938しかし 1000000 個の C<a> からなる似た文字列を使ってみると、4 分の 1 の
5367しかし 1000000 個の C<"a"> からなる似た文字列を使ってみると、4 分の 1 の
39395368時間で完了します。
39405369しかしながら、この構文は量指定子が引き続くと現在のところ
39415370C<use warnings> プラグマまたは B<-w> スイッチの影響下では
39425371C<"matches null string many times in regex">
39435372(正規表現において空文字列に何回もマッチングしました) という警告を
39445373発するでしょう。
39455374
39465375=begin original
39475376
39485377On simple groups, such as the pattern C<< (?> [^()]+ ) >>, a comparable
3949effect may be achieved by negative look-ahead, as in C<[^()]+ (?! [^()] )>.
5378effect may be achieved by negative lookahead, as in C<[^()]+ (?! [^()] )>.
3950This was only 4 times slower on a string with 1000000 C<a>s.
5379This was only 4 times slower on a string with 1000000 C<"a">s.
39515380
39525381=end original
39535382
39545383パターン C<< (?> [^()]+ ) >> のような簡単なグループでは、
39555384比較できる影響は C<[^()]+ (?! [^()] )> のように負の先読みの
39565385言明で達することができます。
3957これは 1000000 個の C<a> からなる文字列において 4 倍だけ遅くなります。
5386これは 1000000 個の C<"a"> からなる文字列において 4 倍だけ遅くなります。
39585387
39595388=begin original
39605389
39615390The "grab all you can, and do not give anything back" semantic is desirable
39625391in many situations where on the first sight a simple C<()*> looks like
39635392the correct solution. Suppose we parse text with comments being delimited
3964by C<#> followed by some optional (horizontal) whitespace. Contrary to
5393by C<"<#ins>"> followed by some optional (horizontal) whitespace. Contrary to
39655394its appearance, C<#[ \t]*> I<is not> the correct subexpression to match
39665395the comment delimiter, because it may "give up" some whitespace if
39675396the remainder of the pattern can be made to match that way. The correct
39685397answer is either one of these:
39695398
39705399=end original
39715400
39725401最初の C<()*> のような正しい解法となる多くの状況において
39735402「できる限りを取り込んで、後は戻らない」セマンティクスが望まれるものです。
3974任意で(水平)空白の続く C<#> によって区切られるコメントのついたテキストの
5403任意で(水平)空白の続く C<"<#ins>"> によって区切られるコメントのついたテキストの
39755404パースを考えてみます。
39765405その出現と対比して、C<#[ \t]*> はコメント区切りにマッチングする
39775406正しい部分式ではありません; なぜならパターンの残りがそれのマッチングを
39785407作ることができるのならそれはいくつかの空白を「あきらめてしまう」ためです。
39795408正しい回答は以下のいずれかです:
39805409
39815410 (?>#[ \t]*)
39825411 #[ \t]*(?![ \t])
39835412
39845413=begin original
39855414
3986For example, to grab non-empty comments into $1, one should use either
5415For example, to grab non-empty comments into C<$1>, one should use either
39875416one of these:
39885417
39895418=end original
39905419
3991例えば空でないコメントを $1 に取り込むためには次のいずれかを使います:
5420例えば空でないコメントを C<$1> に取り込むためには次のいずれかを使います:
39925421
39935422 / (?> \# [ \t]* ) ( .+ ) /x;
39945423 / \# [ \t]* ( [^ \t] .* ) /x;
39955424
39965425=begin original
39975426
39985427Which one you pick depends on which of these expressions better reflects
39995428the above specification of comments.
40005429
40015430=end original
40025431
40035432選んだ方はコメントの仕様をより適切に反映した式に依存します。
40045433
40055434=begin original
40065435
40075436In some literature this construct is called "atomic matching" or
40085437"possessive matching".
40095438
40105439=end original
40115440
40125441いくつかの書籍においてこの構成子は「アトミックなマッチング」
40135442または「絶対最大量マッチング(possessive matching)」と呼ばれます。
40145443
40155444=begin original
40165445
40175446Possessive quantifiers are equivalent to putting the item they are applied
40185447to inside of one of these constructs. The following equivalences apply:
40195448
40205449=end original
40215450
40225451絶対最大量指定子はそれが適用されている項目をこれらの構成子の中に置くことと
40235452等価です。
40245453以下の等式が適用されます:
40255454
40265455 Quantifier Form Bracketing Form
40275456 --------------- ---------------
40285457 PAT*+ (?>PAT*)
40295458 PAT++ (?>PAT+)
40305459 PAT?+ (?>PAT?)
40315460 PAT{min,max}+ (?>PAT{min,max})
40325461
4033=back
4034
4035=head2 Special Backtracking Control Verbs
4036
4037(特殊なバックトラック制御記号)
4038
40395462=begin original
40405463
4041B<WARNING:> These patterns are experimental and subject to change or
5464Nested C<(?E<gt>...)> constructs are not no-ops, even if at first glance
4042removal in a future version of Perl. Their usage in production code should
5465they might seem to be. This is because the nested C<(?E<gt>...)> can
4043be noted to avoid problems during upgrades.
5466restrict internal backtracking that otherwise might occur. For example,
40445467
40455468=end original
40465469
4047B<警告:> これらのパターン実験的なものであり
5470ネストした C<(?E<gt>...)> 構文は、
4048Perl 今後のバージョン変更また削除される可能性がありま
5471たとえ一見何もしないように見えても、何もしないものではありません
4049製品コードでこれらを使う際にアップグレードによ問題を
5472これは、ネストした C<(?E<gt>...)> は、なければ起きかもしれない
4050避けるために明記するべきです。
5473内部バックトラックを制限するからです。
5474例えば:
40515475
4052=begin original
5476 "abc" =~ /(?>a[bc]*c)/
40535477
4054These special patterns are generally of the form C<(*VERB:ARG)>. Unless
4055otherwise stated the ARG argument is optional; in some cases, it is
4056forbidden.
4057
4058=end original
4059
4060これらの特殊なパターンは C<(*VERB:ARG)> という一般形式を持っています。
4061ARG が任意であると規定されていいないいくつかのケース以外では、それは
4062拒否されます。
4063
40645478=begin original
40655479
4066Any pattern containing a special backtracking verb that allows an argument
5480matches, but
4067has the special behaviour that when executed it sets the current package's
4068C<$REGERROR> and C<$REGMARK> variables. When doing so the following
4069rules apply:
40705481
40715482=end original
40725483
4073引数を許可する特殊バクトラック制御記号を含んでいる全てのパターは、
5484これはマグしますが:
4074それが実行されると現在のパッケージの C<$REGERROR> 及び C<$REGMARK> 変数を
4075設定する特殊な振る舞いを持っています。
4076これが行われる時以下の手順が適用されます。
40775485
4078=begin original
5486 "abc" =~ /(?>a(?>[bc]*)c)/
40795487
4080On failure, the C<$REGERROR> variable will be set to the ARG value of the
4081verb pattern, if the verb was involved in the failure of the match. If the
4082ARG part of the pattern was omitted, then C<$REGERROR> will be set to the
4083name of the last C<(*MARK:NAME)> pattern executed, or to TRUE if there was
4084none. Also, the C<$REGMARK> variable will be set to FALSE.
4085
4086=end original
4087
4088失敗時には C<$REGERROR> 変数には、記号がマッチングの失敗の中で
4089使われていたのならその記号パターンの ARG の値がセットされます。
4090もしパターンの ARG 部分が省略されていたときには、C<$REGERROR> には
4091最後に実行された C<(*MARK:NAME)> パターンの名前、またはそれもなければ
4092真に設定されます。
4093また、C<$REGMARK> 変数は偽に設定されます。
4094
40955488=begin original
40965489
4097On a successful match, the C<$REGERROR> variable will be set to FALSE, and
5490does not.
4098the C<$REGMARK> variable will be set to the name of the last
4099C<(*MARK:NAME)> pattern executed. See the explanation for the
4100C<(*MARK:NAME)> verb below for more details.
41015491
41025492=end original
41035493
4104マッチングの成功時には、C<$REGERROR> 変数は偽に設定され、C<$REGMARK> 変数には
5494これはマッチングしません。
4105最後に実行された C<(*MARK:NAME)> パターンの名前が設定されます。
4106詳細は C<(*MARK:NAME)> 記号の説明を参照してください。
41075495
41085496=begin original
41095497
4110B<NOTE:> C<$REGERROR> and C<$REGMARK> are not magic variables like C<$1>
5498The alphabetic form (C<(*atomic:...)>) is experimental; using it
4111and most other regex-related variables. They are not local to a scope, nor
5499yields a warning in the C<experimental::alpha_assertions> category.
4112readonly, but instead are volatile package variables similar to C<$AUTOLOAD>.
4113Use C<local> to localize changes to them to a specific scope if necessary.
41145500
41155501=end original
41165502
4117B<補足:> C<$REGERROR> 及び C<$REGMARK> C<$1> や他の多くの
5503英字形式 (C<(*atomic:...)>)実験的です; これを使うと
4118正規表現関連変数のようにマジック変数ではありせん
5504C<experimental::alpha_assertions> カテゴリ警告が出
4119それらはスコープ内にローカルにならず、読み込み専用でもありませんが、
4120C<$AUTOLOAD> と似た揮発するパッケージ変数です。
4121必要時に特定のスコープ内に変更を留めたいときには C<local> を使ってください。
41225505
4123=begin original
5506=item C<(?[ ])>
41245507
4125If a pattern does not contain a special backtracking verb that allows an
4126argument, then C<$REGERROR> and C<$REGMARK> are not touched at all.
4127
4128=end original
4129
4130もしパターンが引数を許可する特殊バックトラック記号を含んでなかった場合には、
4131C<$REGERROR> 及び C<$REGMARK> は全く触られません。
4132
4133=over 3
4134
4135=item Verbs that take an argument
4136
4137(引数を取る動詞)
4138
4139=over 4
4140
4141=item C<(*PRUNE)> C<(*PRUNE:NAME)>
4142X<(*PRUNE)> X<(*PRUNE:NAME)>
4143
41445508=begin original
41455509
4146This zero-width pattern prunes the backtracking tree at the current point
5510See L<perlrecharclass/Extended Bracketed Character Classes>.
4147when backtracked into on failure. Consider the pattern C<A (*PRUNE) B>,
4148where A and B are complex patterns. Until the C<(*PRUNE)> verb is reached,
4149A may backtrack as necessary to match. Once it is reached, matching
4150continues in B, which may also backtrack as necessary; however, should B
4151not match, then no further backtracking will take place, and the pattern
4152will fail outright at the current starting position.
41535511
41545512=end original
41555513
4156このゼロ幅のパターンは失敗でバックトラックしてきたときに現在の位置で
5514L<perlrecharclass/Extended Bracketed Character Classes> を参照してください。
4157バックトラックツリーを刈り取ります。
4158C<A (*PRUNE) B> というパターンで A も B も複雑なパターンである時を
4159考えてみます。
4160C<(*PRUNE)> に達するまでは、A はマッチングに必要であれば
4161バックトラックしていきます。
4162しかし一旦そこに達して B に続くと、そこでも必要に応じてバックトラックします;
4163しかしながら、B がマッチングしなかったときにはそれ以上のバックトラックは
4164行われず、現在の開始位置でのマッチングはすぐに失敗します。
41655515
41665516=begin original
41675517
4168The following example counts all the possible matching strings in a
5518Note that this feature is currently L<experimental|perlpolicy/experimental>;
4169pattern (without actually matching any of them).
5519using it yields a warning in the C<experimental::regex_sets> category.
41705520
41715521=end original
41725522
4173例でパターンに対してマッチングきるべての文字列を(実際には
5523機能現在 L<実験的|perlpolicy/experimental> です;
4174マッチングさせずに)数えます。
5524これを使うと C<experimental::regex_sets> カテゴリの警告が発生します。
41755525
4176 'aaab' =~ /a+b?(?{print "$&\n"; $count++})(*FAIL)/;
4177 print "Count=$count\n";
4178
4179=begin original
4180
4181which produces:
4182
4183=end original
4184
4185この出力:
4186
4187 aaab
4188 aaa
4189 aa
4190 a
4191 aab
4192 aa
4193 a
4194 ab
4195 a
4196 Count=9
4197
4198=begin original
4199
4200If we add a C<(*PRUNE)> before the count like the following
4201
4202=end original
4203
4204次のように数える前に C<(*PRUNE)> を加えると
4205
4206 'aaab' =~ /a+b?(*PRUNE)(?{print "$&\n"; $count++})(*FAIL)/;
4207 print "Count=$count\n";
4208
4209=begin original
4210
4211we prevent backtracking and find the count of the longest matching string
4212at each matching starting point like so:
4213
4214=end original
4215
4216バックトラックを妨げ次のように各開始位置での一番長いマッチング文字列を
4217数えるようになります:
4218
4219 aaab
4220 aab
4221 ab
4222 Count=3
4223
4224=begin original
4225
4226Any number of C<(*PRUNE)> assertions may be used in a pattern.
4227
4228=end original
4229
42301つのパターン内で C<(*PRUNE)> 言明はいくつでも使えます。
4231
4232=begin original
4233
4234See also C<< (?>pattern) >> and possessive quantifiers for other ways to
4235control backtracking. In some cases, the use of C<(*PRUNE)> can be
4236replaced with a C<< (?>pattern) >> with no functional difference; however,
4237C<(*PRUNE)> can be used to handle cases that cannot be expressed using a
4238C<< (?>pattern) >> alone.
4239
4240=end original
4241
4242バックトラックを制御する他の方法として C<< (?>pattern) >>
4243及び絶対最大量指定子も参照してください。
4244幾つかのケースにおいては C<(*PRUNE)> の利用は機能的な違いなしに
4245C<< (?>pattern) >> で置き換えることができます; しかしながら C<(*PRUNE)> は
4246C<< (?>pattern) >> 単独では表現できないケースを扱うために使えます。
4247
4248=item C<(*SKIP)> C<(*SKIP:NAME)>
4249X<(*SKIP)>
4250
4251=begin original
4252
4253This zero-width pattern is similar to C<(*PRUNE)>, except that on
4254failure it also signifies that whatever text that was matched leading up
4255to the C<(*SKIP)> pattern being executed cannot be part of I<any> match
4256of this pattern. This effectively means that the regex engine "skips" forward
4257to this position on failure and tries to match again, (assuming that
4258there is sufficient room to match).
4259
4260=end original
4261
4262このゼロ幅のパターンは C<*PRUNE> と似ていますが、実行されている
4263C<(*SKIP)> パターンまでにマッチングしたテキストはこのパターンの
4264I<どの>マッチングの一部にもならないことを示します。
4265これは正規表現エンジンがこの位置まで失敗として「スキップ」して(マッチングに
4266十分な空間があれば)再びマッチングを試みることを効率的に意味します。
4267
4268=begin original
4269
4270The name of the C<(*SKIP:NAME)> pattern has special significance. If a
4271C<(*MARK:NAME)> was encountered while matching, then it is that position
4272which is used as the "skip point". If no C<(*MARK)> of that name was
4273encountered, then the C<(*SKIP)> operator has no effect. When used
4274without a name the "skip point" is where the match point was when
4275executing the (*SKIP) pattern.
4276
4277=end original
4278
4279C<(*SKIP:NAME)> パターンの名前部分には特別な意味があります。
4280もしマッチングにおいて C<(*MARK:NAME)> に遭遇すると、それは「スキップ
4281位置」として使われる位置になります。
4282その名前の C<(*MARK)> と東宮していなければ、C<(*SKIP)> 操作は効果を
4283持ちません。
4284名前がなければ「スキップ位置」は(*SKIP)パターンの実行されたときに
4285マッチングポイントが使われます。
4286
4287=begin original
4288
4289Compare the following to the examples in C<(*PRUNE)>; note the string
4290is twice as long:
4291
4292=end original
4293
4294以下の例を C<(*PRUNE)> と比べてみてください;
4295文字列が2倍になってることに注意してください:
4296
4297 'aaabaaab' =~ /a+b?(*SKIP)(?{print "$&\n"; $count++})(*FAIL)/;
4298 print "Count=$count\n";
4299
4300=begin original
4301
4302outputs
4303
4304=end original
4305
4306これの出力は
4307
4308 aaab
4309 aaab
4310 Count=2
4311
4312=begin original
4313
4314Once the 'aaab' at the start of the string has matched, and the C<(*SKIP)>
4315executed, the next starting point will be where the cursor was when the
4316C<(*SKIP)> was executed.
4317
4318=end original
4319
4320いったん文字列の最初の 'aaab' がマッチングして、C<(*SKIP)> が実行されると、
4321次の開始位置は C<(*SKIP)> が実行されたときのカーソルがいた位置になります。
4322
4323=item C<(*MARK:NAME)> C<(*:NAME)>
4324X<(*MARK)> X<(*MARK:NAME)> X<(*:NAME)>
4325
4326=begin original
4327
4328This zero-width pattern can be used to mark the point reached in a string
4329when a certain part of the pattern has been successfully matched. This
4330mark may be given a name. A later C<(*SKIP)> pattern will then skip
4331forward to that point if backtracked into on failure. Any number of
4332C<(*MARK)> patterns are allowed, and the NAME portion may be duplicated.
4333
4334=end original
4335
4336このゼロ幅のマッチングはパターン内の特定の箇所がマッチングに成功したときに、
4337文字列の中で達した位置を記録するために使われます。
4338このマークには名前をつけることもできます。
4339後者の C<(*SKIP)> パターンは失敗時でバックトラックしたときにその箇所まで
4340スキップします。
4341C<(*MARK)> パターンはいくつでも使うことができて、NAME 部分は
4342重複することもあります。
4343
4344=begin original
4345
4346In addition to interacting with the C<(*SKIP)> pattern, C<(*MARK:NAME)>
4347can be used to "label" a pattern branch, so that after matching, the
4348program can determine which branches of the pattern were involved in the
4349match.
4350
4351=end original
4352
4353C<(*SKIP)> パターンとの相互動作に加えて、C<(*MARK:NAME)> はパターン分岐の
4354「ラベル」としても使うことができます; このためマッチングの後で、プログラムは
4355そのマッチングにおいてパターンのどの分岐が使われたのかを知ることができます。
4356
4357=begin original
4358
4359When a match is successful, the C<$REGMARK> variable will be set to the
4360name of the most recently executed C<(*MARK:NAME)> that was involved
4361in the match.
4362
4363=end original
4364
4365マッチングの成功時に、C<$REGMARK> 変数はマッチングの中で一番最近に
4366実行された C<(*MARK:NAME)> の名前を設定します。
4367
4368=begin original
4369
4370This can be used to determine which branch of a pattern was matched
4371without using a separate capture group for each branch, which in turn
4372can result in a performance improvement, as perl cannot optimize
4373C</(?:(x)|(y)|(z))/> as efficiently as something like
4374C</(?:x(*MARK:x)|y(*MARK:y)|z(*MARK:z))/>.
4375
4376=end original
4377
4378これは書く分岐で別々の捕捉グループを使うことなしにパターンのどの分岐が
4379マッチングしたのかを知るために使うことができます; これは perl は
4380C</(?:(x)|(y)|(z))/> を C</(?:x(*MARK:x)|y(*MARK:y)|z(*MARK:z))/> 程度に
4381効率的には最適化できないためパフォーマンスの向上をもたらします。
4382
4383=begin original
4384
4385When a match has failed, and unless another verb has been involved in
4386failing the match and has provided its own name to use, the C<$REGERROR>
4387variable will be set to the name of the most recently executed
4388C<(*MARK:NAME)>.
4389
4390=end original
4391
4392マッチングが失敗して、そして他の記号がマッチングの失敗で行われずかつ名前を
4393持っているというのでなければ、C<$REGERROR> 変数には一番最近に実行された
4394名前が設定されます。
4395
4396=begin original
4397
4398See L</(*SKIP)> for more details.
4399
4400=end original
4401
4402詳細は L</(*SKIP)> を参照してください。
4403
4404=begin original
4405
4406As a shortcut C<(*MARK:NAME)> can be written C<(*:NAME)>.
4407
4408=end original
4409
4410C<(*MARK:NAME)> の短縮形として C<(*:NAME)> とも記述できます。
4411
4412=item C<(*THEN)> C<(*THEN:NAME)>
4413
4414=begin original
4415
4416This is similar to the "cut group" operator C<::> from Perl 6. Like
4417C<(*PRUNE)>, this verb always matches, and when backtracked into on
4418failure, it causes the regex engine to try the next alternation in the
4419innermost enclosing group (capturing or otherwise) that has alternations.
4420The two branches of a C<(?(condition)yes-pattern|no-pattern)> do not
4421count as an alternation, as far as C<(*THEN)> is concerned.
4422
4423=end original
4424
4425これは Perl 6 の "cut group" 演算子 C<::> と似ています。
4426C<(*PRUNE)> のように、この記号は常にマッチングし、そして失敗で
4427バックトラックした時に正規表現エンジンに、代替のある一番内側で閉じている
4428グループ(キャプチャでもそうでなくとも)で次の代替を試みるようにさせます。
4429C<(*THEN)> が有効である限り、
4430C<(?(condition)yes-pattern|no-pattern)> の二つの枝は代替とは扱われません。
4431
4432=begin original
4433
4434Its name comes from the observation that this operation combined with the
4435alternation operator (C<|>) can be used to create what is essentially a
4436pattern-based if/then/else block:
4437
4438=end original
4439
4440この名前は代替演算子(C<|>) と連結されたこの演算子で本質的にパターンベースの
4441if/then/else ブロックとなるものを作るために使うことが
4442できることからきています:
4443
4444 ( COND (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ )
4445
4446=begin original
4447
4448Note that if this operator is used and NOT inside of an alternation then
4449it acts exactly like the C<(*PRUNE)> operator.
4450
4451=end original
4452
4453この演算子が使われていてそしてそれが代替の内側ではなければ
4454これはちょうど C<(*PRUNE)> 演算子のように動作します。
4455
4456 / A (*PRUNE) B /
4457
4458=begin original
4459
4460is the same as
4461
4462=end original
4463
4464は次と同じです
4465
4466 / A (*THEN) B /
4467
4468=begin original
4469
4470but
4471
4472=end original
4473
4474しかし
4475
4476 / ( A (*THEN) B | C (*THEN) D ) /
4477
4478=begin original
4479
4480is not the same as
4481
4482=end original
4483
4484は次と同じではありません
4485
4486 / ( A (*PRUNE) B | C (*PRUNE) D ) /
4487
4488=begin original
4489
4490as after matching the A but failing on the B the C<(*THEN)> verb will
4491backtrack and try C; but the C<(*PRUNE)> verb will simply fail.
4492
4493=end original
4494
4495A にマッチングしたけれど B に失敗した後 C<(*THEN)> 記号はバックトラックして
4496C を試みます; しかし C<(*PRUNE)> 記号であれば単純に失敗します。
4497
44985526=back
44995527
4500=item Verbs without an argument
4501
4502=over 4
4503
4504=item C<(*COMMIT)>
4505X<(*COMMIT)>
4506
4507=begin original
4508
4509This is the Perl 6 "commit pattern" C<< <commit> >> or C<:::>. It's a
4510zero-width pattern similar to C<(*SKIP)>, except that when backtracked
4511into on failure it causes the match to fail outright. No further attempts
4512to find a valid match by advancing the start pointer will occur again.
4513For example,
4514
4515=end original
4516
4517これは Perl 6 の"コミットパターン" C<< <commit> >> または C<:::> です。
4518これは C<(*SKIP)> と似たゼロ幅のパターンですが、失敗でバックトラックした
4519際にマッチングがすぐに失敗する点で異なります。
4520それ以降で開始位置を進めて有効なマッチングを探す試行は行われません。
4521例えば、
4522
4523 'aaabaaab' =~ /a+b?(*COMMIT)(?{print "$&\n"; $count++})(*FAIL)/;
4524 print "Count=$count\n";
4525
4526=begin original
4527
4528outputs
4529
4530=end original
4531
4532これの出力は
4533
4534 aaab
4535 Count=1
4536
4537=begin original
4538
4539In other words, once the C<(*COMMIT)> has been entered, and if the pattern
4540does not match, the regex engine will not try any further matching on the
4541rest of the string.
4542
4543=end original
4544
4545言い換えると、いったん C<(*COMMIT)> に入った後に、そのパターンが
4546マッチングしなかったのなら、正規表現エンジンは文字列の残りに対して
4547それ以上のマッチングを試みません。
4548
4549=item C<(*FAIL)> C<(*F)>
4550X<(*FAIL)> X<(*F)>
4551
4552=begin original
4553
4554This pattern matches nothing and always fails. It can be used to force the
4555engine to backtrack. It is equivalent to C<(?!)>, but easier to read. In
4556fact, C<(?!)> gets optimised into C<(*FAIL)> internally.
4557
4558=end original
4559
4560このパターンは何にもマッチングせず常に失敗します。
4561これはエンジンを強制的にバックトラックさせるために使えます。
4562これは C<(?!)> と等価ですが、より読みやすくなっています。
4563実際、C<(?!)> は内部的には C<(*FAIL)> に最適化されます。
4564
4565=begin original
4566
4567It is probably useful only when combined with C<(?{})> or C<(??{})>.
4568
4569=end original
4570
4571これはおそらく C<(?{})> または C<(??{})> と組み合わせた時にだけ
4572役に立つでしょう。
4573
4574=item C<(*ACCEPT)>
4575X<(*ACCEPT)>
4576
4577=begin original
4578
4579B<WARNING:> This feature is highly experimental. It is not recommended
4580for production code.
4581
4582=end original
4583
4584B<警告:> この機能は強く実験的です。
4585製品コードでは推奨されません。
4586
4587=begin original
4588
4589This pattern matches nothing and causes the end of successful matching at
4590the point at which the C<(*ACCEPT)> pattern was encountered, regardless of
4591whether there is actually more to match in the string. When inside of a
4592nested pattern, such as recursion, or in a subpattern dynamically generated
4593via C<(??{})>, only the innermost pattern is ended immediately.
4594
4595=end original
4596
4597このパターンマッチングは何もせず C<(*ACCEPT)> パターンと遭遇した場所で
4598文字列の中で実際にもっとマッチングするものがあるかどうかにかかわらず
4599成功のマッチングを終了させます。
4600再帰、または C<(??{})> といったネストしたパターンの内側では、一番内側の
4601パターンのみがすぐに終了します。
4602
4603=begin original
4604
4605If the C<(*ACCEPT)> is inside of capturing groups then the groups are
4606marked as ended at the point at which the C<(*ACCEPT)> was encountered.
4607For instance:
4608
4609=end original
4610
4611C<(*ACCEPT)> が捕捉グループの内側で使われた場合捕捉グループは
4612C<(*ACCEPT)> と遭遇した位置で終了とマークされます。
4613例えば:
4614
4615 'AB' =~ /(A (A|B(*ACCEPT)|C) D)(E)/x;
4616
4617=begin original
4618
4619will match, and C<$1> will be C<AB> and C<$2> will be C<B>, C<$3> will not
4620be set. If another branch in the inner parentheses was matched, such as in the
4621string 'ACDE', then the C<D> and C<E> would have to be matched as well.
4622
4623=end original
4624
4625はマッチングし、C<$1> は C<AB> になり、C<$2> は C<B> に、そして
4626C<$3> は設定されません。
4627'ACDE' のようにかっこの内側で他の分岐がマッチングしたのなら、C<D> 及び
4628C<E> もマッチングします。
4629
4630=back
4631
4632=back
4633
46345528=head2 Backtracking
46355529X<backtrack> X<backtracking>
46365530
46375531(バックトラック)
46385532
46395533=begin original
46405534
46415535NOTE: This section presents an abstract approximation of regular
46425536expression behavior. For a more rigorous (and complicated) view of
46435537the rules involved in selecting a match among possible alternatives,
4644see L<Combining RE Pieces>.
5538see L</Combining RE Pieces>.
46455539
46465540=end original
46475541
46485542補足: このセクションでは正規表現の振る舞いに関する抽象的な概要を
46495543説明します。
46505544可能な代替におけるマッチングの選択におけるルールの厳密な(そして複雑な)
4651説明は L<Combining RE Pieces> を参照してください。
5545説明は L</Combining RE Pieces> を参照してください。
46525546
46535547=begin original
46545548
46555549A fundamental feature of regular expression matching involves the
46565550notion called I<backtracking>, which is currently used (when needed)
4657by all regular non-possessive expression quantifiers, namely C<*>, C<*?>, C<+>,
5551by all regular non-possessive expression quantifiers, namely C<"*">, C<*?>, C<"+">,
46585552C<+?>, C<{n,m}>, and C<{n,m}?>. Backtracking is often optimized
46595553internally, but the general principle outlined here is valid.
46605554
46615555=end original
46625556
46635557正規表現マッチングの基本的な機能には最近(必要であれば)すべての強欲でない
4664正規表現量指定子、つまり、C<*>, C<*?>, C<+>, C<+?>, C<{n,m}>, C<{n,m}?> で
5558正規表現量指定子、つまり、
5559C<"*">, C<*?>, C<"+">, C<+?>, C<{n,m}>, C<{n,m}?> で
46655560使われる I<バックトラッキング> と呼ばれる概念が含まれています。
46665561バックトラックはしばしば内部で最適化されますが、ここで概説する一般的な
46675562原則は妥当です。
46685563
46695564=begin original
46705565
46715566For a regular expression to match, the I<entire> regular expression must
46725567match, not just part of it. So if the beginning of a pattern containing a
46735568quantifier succeeds in a way that causes later parts in the pattern to
46745569fail, the matching engine backs up and recalculates the beginning
46755570part--that's why it's called backtracking.
46765571
46775572=end original
46785573
46795574正規表現がマッチングする時、その正規表現の一部ではなく、
46805575I<全体> がマッチングしなければなりません。
46815576そのためもしパターンの前半にパターンの後半部分を失敗させてしまう
46825577量指定子が含まれているのなら、マッチングングエンジンはいったん戻って
46835578開始位置を再計算します -- これがバックトラッキングと呼ばれる所以です。
46845579
46855580=begin original
46865581
46875582Here is an example of backtracking: Let's say you want to find the
46885583word following "foo" in the string "Food is on the foo table.":
46895584
46905585=end original
46915586
46925587バックトラッキングの例をあげてみます: "Food is on the foo table." という
46935588文字列の中で "foo" に続く単語を取り出してください:
46945589
46955590 $_ = "Food is on the foo table.";
46965591 if ( /\b(foo)\s+(\w+)/i ) {
46975592 print "$2 follows $1.\n";
46985593 }
46995594
47005595=begin original
47015596
47025597When the match runs, the first part of the regular expression (C<\b(foo)>)
47035598finds a possible match right at the beginning of the string, and loads up
4704$1 with "Foo". However, as soon as the matching engine sees that there's
5599C<$1> with "Foo". However, as soon as the matching engine sees that there's
4705no whitespace following the "Foo" that it had saved in $1, it realizes its
5600no whitespace following the "Foo" that it had saved in C<$1>, it realizes its
47065601mistake and starts over again one character after where it had the
47075602tentative match. This time it goes all the way until the next occurrence
47085603of "foo". The complete regular expression matches this time, and you get
47095604the expected output of "table follows foo."
47105605
47115606=end original
47125607
47135608マッチングが実行される時、正規表現の最初の部分 (C<\b(foo)>) は開始文字列の
4714右側で可能なマッチングを探します; そして $1 に "Foo" をロードします。
5609右側で可能なマッチングを探します; そして C<$1> に "Foo" をロードします。
4715しかし、すぐにマッチングエンジンは $1 に保存した "Foo" の後に空白が
5610しかし、すぐにマッチングエンジンは C<$1> に保存した "Foo" の後に空白が
47165611無いことを見つけ、それが失敗だったことを検出して仮にマッチングさせた
4717場所の1文字後から開始します。
5612場所の 1 文字後から開始します。
47185613この時次の "foo" の出現まで進みます。
47195614この時に正規表現は完全にマッチングし、予測した出力 "table follows foo." を
47205615得ます。
47215616
47225617=begin original
47235618
47245619Sometimes minimal matching can help a lot. Imagine you'd like to match
47255620everything between "foo" and "bar". Initially, you write something
47265621like this:
47275622
47285623=end original
47295624
47305625最小マッチングが役立つこともあります。
47315626"foo" と "bar" の間の全てにマッチングしたいと考えてください。
47325627最初に、次のように書くかもしれません:
47335628
47345629 $_ = "The food is under the bar in the barn.";
47355630 if ( /foo(.*)bar/ ) {
47365631 print "got <$1>\n";
47375632 }
47385633
47395634=begin original
47405635
47415636Which perhaps unexpectedly yields:
47425637
47435638=end original
47445639
47455640しかしこれは考えたのと違う結果となるでしょう:
47465641
47475642 got <d is under the bar in the >
47485643
47495644=begin original
47505645
47515646That's because C<.*> was greedy, so you get everything between the
47525647I<first> "foo" and the I<last> "bar". Here it's more effective
47535648to use minimal matching to make sure you get the text between a "foo"
47545649and the first "bar" thereafter.
47555650
47565651=end original
47575652
47585653これは C<.*> が貪欲であり、そのために I<最初の> "foo" と I<最後の>
47595654"bar" の間にある全てを取り出してしまいます。
47605655次に "foo" とその後の最初の "bar" の間にあるテキストを取り出す
47615656最小マッチングを使ったもっと効率的な方法を示します:
47625657
47635658 if ( /foo(.*?)bar/ ) { print "got <$1>\n" }
47645659 got <d is under the >
47655660
47665661=begin original
47675662
47685663Here's another example. Let's say you'd like to match a number at the end
47695664of a string, and you also want to keep the preceding part of the match.
47705665So you write this:
47715666
47725667=end original
47735668
47745669別の例も出してみます。
47755670文字列の最後にある数字にマッチングさせて、そのマッチングの前の部分も
47765671保持させてみましょう。
47775672そしてあなたは次のように書くかもしれません。
47785673
47795674 $_ = "I have 2 numbers: 53147";
47805675 if ( /(.*)(\d*)/ ) { # Wrong!
47815676 print "Beginning is <$1>, number is <$2>.\n";
47825677 }
47835678
47845679=begin original
47855680
47865681That won't work at all, because C<.*> was greedy and gobbled up the
47875682whole string. As C<\d*> can match on an empty string the complete
47885683regular expression matched successfully.
47895684
47905685=end original
47915686
47925687これは全く動作しません、なぜなら C<.*> は貪欲であり文字列全体を
47935688飲み込んでしまいます。
47945689C<\d*> は空の文字列にマッチングできるので正規表現は完全に正常に
47955690マッチングします。
47965691
47975692 Beginning is <I have 2 numbers: 53147>, number is <>.
47985693
47995694=begin original
48005695
48015696Here are some variants, most of which don't work:
48025697
48035698=end original
48045699
48055700動作しない主なバリエーションをあげておきます:
48065701
48075702 $_ = "I have 2 numbers: 53147";
48085703 @pats = qw{
48095704 (.*)(\d*)
48105705 (.*)(\d+)
48115706 (.*?)(\d*)
48125707 (.*?)(\d+)
48135708 (.*)(\d+)$
48145709 (.*?)(\d+)$
48155710 (.*)\b(\d+)$
48165711 (.*\D)(\d+)$
48175712 };
48185713
48195714 for $pat (@pats) {
48205715 printf "%-12s ", $pat;
48215716 if ( /$pat/ ) {
48225717 print "<$1> <$2>\n";
48235718 } else {
48245719 print "FAIL\n";
48255720 }
48265721 }
48275722
48285723=begin original
48295724
48305725That will print out:
48315726
48325727=end original
48335728
48345729これらの結果は次のようになります:
48355730
48365731 (.*)(\d*) <I have 2 numbers: 53147> <>
48375732 (.*)(\d+) <I have 2 numbers: 5314> <7>
48385733 (.*?)(\d*) <> <>
48395734 (.*?)(\d+) <I have > <2>
48405735 (.*)(\d+)$ <I have 2 numbers: 5314> <7>
48415736 (.*?)(\d+)$ <I have 2 numbers: > <53147>
48425737 (.*)\b(\d+)$ <I have 2 numbers: > <53147>
48435738 (.*\D)(\d+)$ <I have 2 numbers: > <53147>
48445739
48455740=begin original
48465741
48475742As you see, this can be a bit tricky. It's important to realize that a
48485743regular expression is merely a set of assertions that gives a definition
48495744of success. There may be 0, 1, or several different ways that the
48505745definition might succeed against a particular string. And if there are
48515746multiple ways it might succeed, you need to understand backtracking to
48525747know which variety of success you will achieve.
48535748
48545749=end original
48555750
48565751このように、これは幾分トリッキーです。
48575752重要なのは正規表現は成功の定義を定める主張の集合にすぎないことを
48585753認識することです。
48595754特定の文字列で成功となる定義には 0, 1 または複数の違ったやり方が存在します。
48605755そしてもし成功する複数の方法が存在するのなら成功したうちのどれが目的と
48615756するものなのかを知るためにバックトラッキングを理解しておく必要があります。
48625757
48635758=begin original
48645759
4865When using look-ahead assertions and negations, this can all get even
5760When using lookahead assertions and negations, this can all get even
48665761trickier. Imagine you'd like to find a sequence of non-digits not
48675762followed by "123". You might try to write that as
48685763
48695764=end original
48705765
48715766前読みの言明及び否定を使っている時にはこれはますますトリッキーになります。
48725767"123" が後ろに続かない数字以外の列を探したいと考えてみてください。
48735768あなたは次のように書くかもしれません。
48745769
48755770 $_ = "ABC123";
48765771 if ( /^\D*(?!123)/ ) { # Wrong!
48775772 print "Yup, no 123 in $_\n";
48785773 }
48795774
48805775=begin original
48815776
48825777But that isn't going to match; at least, not the way you're hoping. It
48835778claims that there is no 123 in the string. Here's a clearer picture of
48845779why that pattern matches, contrary to popular expectations:
48855780
48865781=end original
48875782
48885783ですがこれはマッチングしません; 少なくともなってほしかったようには。
48895784これは文字列の中に 123 がないことを要求します。
48905785よくある予想と比較してなぜパターンがマッチングするのかのわかりやすい
48915786説明を次に示します:
48925787
48935788 $x = 'ABC123';
48945789 $y = 'ABC445';
48955790
48965791 print "1: got $1\n" if $x =~ /^(ABC)(?!123)/;
48975792 print "2: got $1\n" if $y =~ /^(ABC)(?!123)/;
48985793
48995794 print "3: got $1\n" if $x =~ /^(\D*)(?!123)/;
49005795 print "4: got $1\n" if $y =~ /^(\D*)(?!123)/;
49015796
49025797=begin original
49035798
49045799This prints
49055800
49065801=end original
49075802
49085803これは次の出力となります
49095804
49105805 2: got ABC
49115806 3: got AB
49125807 4: got ABC
49135808
49145809=begin original
49155810
49165811You might have expected test 3 to fail because it seems to a more
49175812general purpose version of test 1. The important difference between
49185813them is that test 3 contains a quantifier (C<\D*>) and so can use
49195814backtracking, whereas test 1 will not. What's happening is
4920that you've asked "Is it true that at the start of $x, following 0 or more
5815that you've asked "Is it true that at the start of C<$x>, following 0 or more
49215816non-digits, you have something that's not 123?" If the pattern matcher had
49225817let C<\D*> expand to "ABC", this would have caused the whole pattern to
49235818fail.
49245819
49255820=end original
49265821
49275822テスト 3 はテスト 1 のより一般的なバージョンなのでそれが失敗すると
49285823考えたかもしれません。
49295824この 2 つの重要な違いは、テスト 3 には量指定子(C<\D*>)が含まれているので
49305825テスト1ではできなかったバックトラッキングを行うことが
49315826できるところにあります。
4932ここであなたは「$x のはじめで 0 個以上の非数字があるから 123 ではない
5827ここであなたは「C<$x> のはじめで 0 個以上の非数字があるから 123 ではない
49335828何かを得られるんじゃないの?」と聞くでしょう。
49345829このパターンマッチングが C<\D*> を "ABC" に展開させると
49355830これはパターン全体を失敗させることになります。
49365831
49375832=begin original
49385833
49395834The search engine will initially match C<\D*> with "ABC". Then it will
49405835try to match C<(?!123)> with "123", which fails. But because
49415836a quantifier (C<\D*>) has been used in the regular expression, the
49425837search engine can backtrack and retry the match differently
49435838in the hope of matching the complete regular expression.
49445839
49455840=end original
49465841
49475842探索エンジンは最初に C<\D*> を "ABC" にマッチングさせます。
49485843そして C<(?!123)> を "123" にマッチングさせ、これは失敗します。
49495844けれども量指定子 (C<\D*>) が正規表現の中で使われているので、探索エンジンは
49505845バックトラックしてこの正規表現全体をマッチングさせるように異なるマッチングを
49515846行うことができます。
49525847
49535848=begin original
49545849
49555850The pattern really, I<really> wants to succeed, so it uses the
49565851standard pattern back-off-and-retry and lets C<\D*> expand to just "AB" this
49575852time. Now there's indeed something following "AB" that is not
49585853"123". It's "C123", which suffices.
49595854
49605855=end original
49615856
49625857このパターンは本当に、I<本当に> 成功したいので、これは標準的なパターンの
49635858後退再試行を行い、この時に C<\D*> を "AB" のみに展開させます。
49645859そして確かに "AB" の後ろは "123" ではありません。
49655860"C123" は十分満たしています。
49665861
49675862=begin original
49685863
49695864We can deal with this by using both an assertion and a negation.
4970We'll say that the first part in $1 must be followed both by a digit
5865We'll say that the first part in C<$1> must be followed both by a digit
4971and by something that's not "123". Remember that the look-aheads
5866and by something that's not "123". Remember that the lookaheads
49725867are zero-width expressions--they only look, but don't consume any
49735868of the string in their match. So rewriting this way produces what
49745869you'd expect; that is, case 5 will fail, but case 6 succeeds:
49755870
49765871=end original
49775872
49785873これは言明と否定の両方を使うことで処理することができます。
4979$1 の最初の部分は数字が続きかつそれは "123" ではないことを宣言します。
5874C<$1> の最初の部分は数字が続きかつそれは "123" ではないことを宣言します。
49805875先読みはゼロ幅の式なのでそれがマッチングした文字列を全く消費しないことを
49815876思い出してください。
49825877そしてこれを必要なものを生成するように書き換えます;
49835878つまり、5 のケースでは失敗し、6 のケースは成功します:
49845879
49855880 print "5: got $1\n" if $x =~ /^(\D*)(?=\d)(?!123)/;
49865881 print "6: got $1\n" if $y =~ /^(\D*)(?=\d)(?!123)/;
49875882
49885883 6: got ABC
49895884
49905885=begin original
49915886
49925887In other words, the two zero-width assertions next to each other work as though
49935888they're ANDed together, just as you'd use any built-in assertions: C</^$/>
49945889matches only if you're at the beginning of the line AND the end of the
49955890line simultaneously. The deeper underlying truth is that juxtaposition in
49965891regular expressions always means AND, except when you write an explicit OR
49975892using the vertical bar. C</ab/> means match "a" AND (then) match "b",
49985893although the attempted matches are made at different positions because "a"
49995894is not a zero-width assertion, but a one-width assertion.
50005895
50015896=end original
50025897
50035898言い換えると、このそれぞれの次にある2つのゼロ幅の言明はちょうど何か組み込みの
50045899言明を使ったかのようにそれらがともに AND されているかのように動作します:
50055900C</^$/> は行の始まりで且つ同時に行の終了でる時にのみマッチングします。
50065901もっと深部での真実は、併記された正規表現は垂直線を使って明示的に OR を
50075902書いたとき以外は常に AND を意味します。
50085903C</ab/> は、"a" がゼロ幅の言明ではなく 1 文字幅の言明なので異なる場所で
50095904マッチングが行われはしますが、 "a" にマッチング且つ(そして) "b" に
50105905マッチングということを意味します。
50115906
50125907=begin original
50135908
50145909B<WARNING>: Particularly complicated regular expressions can take
50155910exponential time to solve because of the immense number of possible
50165911ways they can use backtracking to try for a match. For example, without
50175912internal optimizations done by the regular expression engine, this will
50185913take a painfully long time to run:
50195914
50205915=end original
50215916
50225917B<警告>: 特にコンパイルされた正規表現はマッチングのために
50235918できる限りのバックトラックを非常に多くの回数行うので
50245919解くために指数的な時間を必要とすることがあります。
50255920例えば、正規表現エンジンの内部で行われる最適化がなかったときには、次の評価は
50265921尋常じゃないくらい長時間かかります:
50275922
50285923 'aaaaaaaaaaaa' =~ /((a{0,5}){0,5})*[c]/
50295924
50305925=begin original
50315926
5032And if you used C<*>'s in the internal groups instead of limiting them
5927And if you used C<"*">'s in the internal groups instead of limiting them
50335928to 0 through 5 matches, then it would take forever--or until you ran
50345929out of stack space. Moreover, these internal optimizations are not
5035always applicable. For example, if you put C<{0,5}> instead of C<*>
5930always applicable. For example, if you put C<{0,5}> instead of C<"*">
50365931on the external group, no current optimization is applicable, and the
50375932match takes a long time to finish.
50385933
50395934=end original
50405935
50415936そしてもし内側のグループで 0 から 5 回にマッチングを制限する代わりに
5042C<*> を使うと、永久に、またはスタックを使い果たすまで
5937C<"*"> を使うと、永久に、またはスタックを使い果たすまで
50435938実行し続けることになります。
50445939その上、これらの最適化は常にできるわけではありません。
5045例えば、外側のグループで C<*> の代わりに C<{0,5}> を使ったときに、現在の
5940例えば、外側のグループで C<"*"> の代わりに C<{0,5}> を使ったときに、現在の
50465941最適化は適用されません; そしてマッチングが終わるまでの長い時間が
50475942必要になります。
50485943
50495944=begin original
50505945
50515946A powerful tool for optimizing such beasts is what is known as an
50525947"independent group",
50535948which does not backtrack (see L</C<< (?>pattern) >>>). Note also that
5054zero-length look-ahead/look-behind assertions will not backtrack to make
5949zero-length lookahead/lookbehind assertions will not backtrack to make
50555950the tail match, since they are in "logical" context: only
50565951whether they match is considered relevant. For an example
5057where side-effects of look-ahead I<might> have influenced the
5952where side-effects of lookahead I<might> have influenced the
50585953following match, see L</C<< (?>pattern) >>>.
50595954
50605955=end original
50615956
50625957そのような野獣のような最適化のためのパワフルなツールとして
50635958知られているものに、「独立グループ」があります; これはバックトラックを
50645959行いません (L</C<< (?>pattern) >>> を参照)。
50655960ゼロ幅の先読み/後読みの言明も「論理的な」文脈なので末尾のマッチングを
50665961バックトラックしません: マッチングが関連して考慮されるかどうかだけです。
50675962先読みの言明の副作用がそれに続くマッチングに影響する I<かもしれない> 例は、
50685963L</C<< (?>pattern) >>> を参照してください。
50695964
5070=head2 Version 8 Regular Expressions
5965=head2 Script Runs
5071X<regular expression, version 8> X<regex, version 8> X<regexp, version 8>
5966X<(*script_run:...)> X<(sr:...)>
5967X<(*atomic_script_run:...)> X<(asr:...)>
50725968
5073(バージョン 8 正規表現)
5969(用字並び)
50745970
50755971=begin original
50765972
5077In case you're not familiar with the "regular" Version 8 regex
5973A script run is basically a sequence of characters, all from the same
5078routines, here are the pattern-matching rules not described above.
5974Unicode script (see L<perlunicode/Scripts>), such as Latin or Greek. In
5975most places a single word would never be written in multiple scripts,
5976unless it is a spoofing attack. An infamous example, is
50795977
50805978=end original
50815979
5082「通常の」バージョン 8 正規表現ルーチン詳しくないであればここには
5980用字連続は基本的は、ラテン文字やギリシャ文字ような
5083これまでに説明されいないパターンマッチングルールがあります。
5981同じ Unicode 用字 (L<perlunicode/Scripts> 参照) からの文字の並びです。
5982ほとんどの場所では、なりすまし攻撃でない限り、一つの単語は決して複数の用字で
5983書かれることはありません。
5984悪名高い例は次のものです:
50845985
5986 paypal.com
5987
50855988=begin original
50865989
5087Any single character matches itself, unless it is a I<metacharacter>
5990Those letters could all be Latin (as in the example just above), or they
5088with a special meaning described here or above. You can cause
5991could be all Cyrillic (except for the dot), or they could be a mixture
5089characters that normally function as metacharacters to be interpreted
5992of the two. In the case of an internet address the C<.com> would be in
5090literally by prefixing them with a "\" (e.g., "\." matches a ".", not any
5993Latin, And any Cyrillic ones would cause it to be a mixture, not a
5091character; "\\" matches a "\"). This escape mechanism is also required
5994script run. Someone clicking on such a link would not be directed to
5092for the character used as the pattern delimiter.
5995the real Paypal website, but an attacker would craft a look-alike one to
5996attempt to gather sensitive information from the person.
50935997
50945998=end original
50955999
5096すべて単一の文字は、そが個々でたはこれまでに説明た特別な意味を
6000これらの文字は全て (直前のように) ラテン文字かもしれません
5097持って I<メタ文字> である場合以外は、文字それ自身にマッチングします。
6001(ドットを除て) 全てキリル文字かもせんし、二つの混合かも
5098文字は "\" で前置されることで通常はメタ文字とての機能を持っている文字を
6002れません。
5099リテラルとして処理させれるようになります(つまり"\."任意の 1 文字ではなく
6003インターネットアドレスの場合C<.com>ラテン文字で
5100"." にマッチングするようになり、"\\" "\" にマッチングするようになりま
6004キリル文字は混合となり用字連続でりません
5101このエスケープ機構はパター区切りとして使われている文字でも必要で
6005誰かがこのようなリクをクリックると、本当の Paypal ウェブサイトに
6006移動せず、攻撃者がその人から機微情報を集めようとするために
6007見た目が似たものを細工するかもしれません。
51026008
51036009=begin original
51046010
5105A series of characters matches that series of characters in the target
6011Starting in Perl 5.28, it is now easy to detect strings that aren't
5106string, so the pattern C<blurfl> would match "blurfl" in the target
6012script runs. Simply enclose just about any pattern like either of
5107string.
6013these:
51086014
51096015=end original
51106016
5111文字の並びはターゲット文列の文字の並びマッチングしす; 従って
6017Perl 5.28 から連続でない文字列を簡単検出できるようになりした。
5112パターン C<blurfl> は、ターゲット文字列 "blurfl" にマッチングします
6018単にパターンを次どちらかのような形で囲みます:
51136019
6020 (*script_run:pattern)
6021 (*sr:pattern)
6022
51146023=begin original
51156024
5116You can specify a character class, by enclosing a list of characters
6025What happens is that after I<pattern> succeeds in matching, it is
5117in C<[]>, which will match any character from the list. If the
6026subjected to the additional criterion that every character in it must be
5118first character after the "[" is "^", the class matches any character not
6027from the same script (see exceptions below). If this isn't true,
5119in the list. Within a list, the "-" character specifies a
6028backtracking occurs until something all in the same script is found that
5120range, so that C<a-z> represents all characters between "a" and "z",
6029matches, or all possibilities are exhausted. This can cause a lot of
5121inclusive. If you want either "-" or "]" itself to be a member of a
6030backtracking, but generally, only malicious input will result in this,
5122class, put it at the start of the list (possibly after a "^"), or
6031though the slow down could cause a denial of service attack. If your
5123escape it with a backslash. "-" is also taken literally when it is
6032needs permit, it is best to make the pattern atomic to cut down on the
5124at the end of the list, just before the closing "]". (The
6033amount of backtracking. This is so likely to be what you want, that
5125following all specify the same class of three characters: C<[-az]>,
6034instead of writing this:
5126C<[az-]>, and C<[a\-z]>. All are different from C<[a-z]>, which
5127specifies a class containing twenty-six characters, even on EBCDIC-based
5128character sets.) Also, if you try to use the character
5129classes C<\w>, C<\W>, C<\s>, C<\S>, C<\d>, or C<\D> as endpoints of
5130a range, the "-" is understood literally.
51316035
51326036=end original
51336037
5134C<[]> で文字リストを囲むことで文字クラスを指定することきます;
6038I<pattern> がマッチングした後に次のことがきます;
5135これはリストの中の任意の文字にマッチングします
6039の中の全ての文字が同じ用字であるという追加の条件が課されます
5136もし "[" のの最初の文字が "^" だったときには、その文字クラスは
6040(述する例外参照)。
5137リストの中にない任意の文マッチングしま
6041これが真でない場合、全て同じ用マッチングする何かが見つかるか、
5138リスト文字 "-" は範囲を意味します; なので C<a-z> は "a" と "z" を
6042全て可能性がなくなるまで、バックトラッキングが発生します
5139含めてそ間にあるすべての文字表現します
6043は多くバックトラッキング引き起こしますが、一般的に、
5140文字クラス要素とて "-" たは "]" を使いたい時には、リストの先頭に
6044悪意ある入力だけがこれを引き起こします;
5141(あるいは"^"の後に)置くか、バックラッシュ使ってエスケープしま
6045この速度低下はサービ不能攻撃引き起こすかもせん
5142"-" はリストの終端リスを閉じる "]" 直前にあっときもリテラルとして
6046事情が許すならバックラッキング量を減らすめにパターンを
6047アトミックに書くのが最善です。
6048これはおそらくあなたが求めているものなので、次のように書く代わりに:
6049
6050 (*script_run:(?>pattern))
6051
6052=begin original
6053
6054you can write either of these:
6055
6056=end original
6057
6058次のどちらかのように書けます:
6059
6060 (*atomic_script_run:pattern)
6061 (*asr:pattern)
6062
6063=begin original
6064
6065(See L</C<(?E<gt>I<pattern>)>>.)
6066
6067=end original
6068
6069(L</C<(?E<gt>I<pattern>)>> を参照してください。)
6070
6071=begin original
6072
6073In Taiwan, Japan, and Korea, it is common for text to have a mixture of
6074characters from their native scripts and base Chinese. Perl follows
6075Unicode's UTS 39 (L<http://unicode.org/reports/tr39/>) Unicode Security
6076Mechanisms in allowing such mixtures. For example, the Japanese scripts
6077Katakana and Hiragana are commonly mixed together in practice, along
6078with some Chinese characters, and hence are treated as being in a single
6079script run by Perl.
6080
6081=end original
6082
6083台湾、日本、韓国では、独自の用字からの文字と中国語を基にした文字が
6084混合している文章は一般的です。
6085Perl はこのような混合を許すために Unicode の UTS 39
6086(L<http://unicode.org/reports/tr39/>) Unicode Security Mechanisms に
6087従います。
6088例えば、日本語用字のカタカナとひらがなは実際には一部の中国語文字と共に
6089混合しているのが一般的なので、Perl によって単一の用字連続として
51436090扱われます。
5144(次の例はすべて同じ3文字からなる文字クラスです: C<[-az]>, C<[az-]>,
5145C<[a\-z]>。
5146これらはすべて EBCDIC ベースの文字集合であっても26文字からなる文字集合
5147C<[a-z]> とは異なります。) また、範囲の端点として文字クラス C<\w>, C<\W>,
5148C<\s>, C<\S>, C<\d>, C<\D> を使ったときも "-" はリテラルとして
5149処理されます。
51506091
51516092=begin original
51526093
5153Note also that the whole range idea is rather unportable between
6094The rules used for matching decimal digits are slightly stricter. Many
5154character sets--and even within character sets they may cause results
6095scripts have their own sets of digits equivalent to the Western C<0>
5155you probably didn't expect. A sound principle is to use only ranges
6096through C<9> ones. A few, such as Arabic, have more than one set. For
5156that begin from and end at either alphabetics of equal case ([a-e],
6097a string to be considered a script run, all digits in it must come from
5157[A-E]), or digits ([0-9]). Anything else is unsafe. If in doubt,
6098the same set of ten, as determined by the first digit encountered.
5158spell out the character sets in full.
6099As an example,
51596100
51606101=end original
51616102
5162範囲全体いうアイデア文字集合間でポータブルではありません -- そして
610310 進数字マッチングするために使われる規則少し厳密になります。
5163結果となる文字集合では予期したものではないでしょう
6104多くの用字は、西洋の C<0> から C<9> 等価独自の数集合を持ちます
5164ひとつ安全策としては同じケース英字([a-e], [A-E]),
6105アラビア文字ように、複数集合を持つももあります。
5165または数([0-9])とう範囲でみ使うことです。
6106連続が考慮される文字列につては、全て数字は、遭遇した
5166以外は安全はありません。
6107最初の数字で決定さるものと同じ集合なければなりません。
5167もし信じられないのであれ文字集合を完全につづってみてください。
6108例え:
51686109
6110 qr/(*script_run: \d+ \b )/x
6111
51696112=begin original
51706113
5171Characters may be specified using a metacharacter syntax much like that
6114guarantees that the digits matched will all be from the same set of 10.
5172used in C: "\n" matches a newline, "\t" a tab, "\r" a carriage return,
6115You won't get a look-alike digit from a different script that has a
5173"\f" a form feed, etc. More generally, \I<nnn>, where I<nnn> is a string
6116different value than what it appears to be.
5174of three octal digits, matches the character whose coded character set value
5175is I<nnn>. Similarly, \xI<nn>, where I<nn> are hexadecimal digits,
5176matches the character whose ordinal is I<nn>. The expression \cI<x>
5177matches the character control-I<x>. Finally, the "." metacharacter
5178matches any character except "\n" (unless you use C</s>).
51796117
51806118=end original
51816119
5182文字C でよく使われているようなメタ文字の構文を使って指定すること
6120これマッチングした数字が全て同じ集合の 10 文字からであることを保証します。
5183できます: "\n" は改行にマッチングし"\t" はタブ、"\r" 復帰に、
6121見た目と異なる値を持つ異なる用字からの数字見えるものを得ること
5184"\f" はフォームフィードにといった具合にマッチングし
6122ありせん
5185より一般的に、\I<nnn> (I<nnn> は 3 桁の 8 進数字) はその文字集合でコード値
5186I<nnn> の文字にマッチングします。
5187同じように、\xI<nn> (I<nn> は16進数字) は数値で I<nn> になる文字に
5188マッチングします。
5189式 \cI<x> は制御文字 I<x> にマッチングします。
5190そして最後に、"." メタ文字は (C</s> を使っていない限り) "\n" 以外の任意の
5191文字にマッチングします。
51926123
51936124=begin original
51946125
5195You can specify a series of alternatives for a pattern using "|" to
6126Unicode has three pseudo scripts that are handled specially.
5196separate them, so that C<fee|fie|foe> will match any of "fee", "fie",
5197or "foe" in the target string (as would C<f(e|i|o)e>). The
5198first alternative includes everything from the last pattern delimiter
5199("(", "(?:", etc. or the beginning of the pattern) up to the first "|", and
5200the last alternative contains everything from the last "|" to the next
5201closing pattern delimiter. That's why it's common practice to include
5202alternatives in parentheses: to minimize confusion about where they
5203start and end.
52046127
52056128=end original
52066129
5207"|" を使ってパターンを区切って一連の代替を指定すこともできます; なので
6130Unicode には特別に扱われ三つの疑似用字があります
5208C<fee|fie|foe> は対象の文字列の "fee"、"fie"、または "foe" のいずれかに
5209(C<f(e|i|o)e> のように)マッチングします。
5210最初の代替には最後のパターン区切り ("(", "(?:" など、またはパターンの始まり)から
5211最初の "|" までのすべてが含まれ、最後の代替には最後の "|" から
5212次の閉じパターン区切りまでが含まれます。
5213通常代替をかっこの中に入れるのは、その開始位置と終了位置が少しはわかりやすく
5214なるようにです。
52156131
52166132=begin original
52176133
5218Alternatives are tried from left to right, so the first
6134"Unknown" is applied to code points whose meaning has yet to be
5219alternative found for which the entire expression matches, is the one that
6135determined. Perl currently will match as a script run, any single
5220is chosen. This means that alternatives are not necessarily greedy. For
6136character string consisting of one of these code points. But any string
5221example: when matching C<foo|foot> against "barefoot", only the "foo"
6137longer than one code point containing one of these will not be
5222part will match, as that is the first alternative tried, and it successfully
6138considered a script run.
5223matches the target string. (This might not seem important, but it is
5224important when you are capturing matched text using parentheses.)
52256139
52266140=end original
52276141
5228代替左から右へと試されます、なので最初の代替がその完全な式で
6142"Unknown" 、意味がまだ決定されていない符号位置に適用されます
5229マッチングしたならそれが選択されます。
6143Perl は現在それらのうちの一つの符号位置からなる 1 文字文字列を
5230これは代替は貪欲である必要はないいうことを意味します。
6144用字連続してマッチングします。
5231例えば: "barefoot" に対 C<foo|foot> をマッチングさせると、最初代替から
6145かし、それらの一つを含む 2 符号位置以上文字列は用字連続として
5232試さるので、"foo" の部分がマッチングし、これは対象の文字列に対して成功で
6146扱わません。
5233マッチングします。
5234(これは重要ではないでしょうが、かっこを使ってマッチングしたテキストを
5235捕捉しているときには重要でしょう。)
52366147
52376148=begin original
52386149
5239Also remember that "|" is interpreted as a literal within square brackets,
6150"Inherited" is applied to characters that modify another, such as an
5240so if you write C<[fee|fie|foe]> you're really only matching C<[feio|]>.
6151accent of some type. These are considered to be in the script of the
6152master character, and so never cause a script run to not match.
52416153
52426154=end original
52436155
5244また "|" は角かっこ中ではリテラルとして処理される
6156"Inherited" は、一部アクセントように他のものを変更する文字に
5245C<[fee|fie|foe]> と書くとこは実際には C<[feio|]> にのみマッチングします。
6157適用されます。
6158これらは元の文字の用字として扱われるので、
6159マッチングしない用字連続になることはありません。
52466160
52476161=begin original
52486162
5249Within a pattern, you may designate subpatterns for later reference
6163The other one is "Common". This consists of mostly punctuation, emoji,
5250by enclosing them in parentheses, and you may refer back to the
6164and characters used in mathematics and music, the ASCII digits C<0>
5251I<n>th subpattern later in the pattern using the metacharacter
6165through C<9>, and full-width forms of these digits. These characters
5252\I<n> or \gI<n>. Subpatterns are numbered based on the left to right order
6166can appear intermixed in text in many of the world's scripts. These
5253of their opening parenthesis. A backreference matches whatever
6167also don't cause a script run to not match. But like other scripts, all
5254actually matched the subpattern in the string being examined, not
6168digits in a run must come from the same set of 10.
5255the rules for that subpattern. Therefore, C<(0|0x)\d*\s\g1\d*> will
5256match "0x1234 0x4321", but not "0x1234 01234", because subpattern
52571 matched "0x", even though the rule C<0|0x> could potentially match
5258the leading 0 in the second number.
52596169
52606170=end original
52616171
5262パターンにおいて、後参照るためにかっこで括って部分パターンを指定できます;
6172もう一つは "Common" です
5263してメタ文字 \I<n> または \gI<n> を使ってパターンの後の方 I<n> 番目の
6173これはほとんど句読点、絵文字、数学と音楽使われる文字、
5264部分パターンを参照することがます。
6174ASCII の数字 C<0> から C<9>、およびそれらの全角版 構成されます。
5265部分パターン開きかっこ左から右へ順に番号づけられます。
6175これらの文字世界中多く用字文章で混ぜれて現れます。
5266後方参照は評価された文字列の中でその部分パターンに実際にマッチングしたもの
6176らもまマッチングしない用字連続はなりません。
5267マッチングます。
6177かし他の幼児と同様、連続の中の全ての数字は 10 の同じ集合からの
5268従って、C<(0|0x)\d*\s\g1\d*> は "0x1234 0x4321" にはマッチングしすが、
6178ものでなければなりせん。
5269"0x1234 01234" にはマッチングしません; なぜなら、C<0|0x> は二つめ数字の
5270先頭にある 0 にマッチングすることができるのですが、
5271部分パターン 1 は "0x" にマッチングするためです。
52726179
5273=head2 Warning on \1 Instead of $1
6180=begin original
52746181
5275($1 ではなく \1 だったときの警告)
6182This construct is non-capturing. You can add parentheses to I<pattern>
6183to capture, if desired. You will have to do this if you plan to use
6184L</(*ACCEPT) (*ACCEPT:arg)> and not have it bypass the script run
6185checking.
52766186
6187=end original
6188
6189この構文は捕捉しません。
6190捕捉したい場合は、その I<pattern> にかっこを追加できます。
6191L</(*ACCEPT) (*ACCEPT:arg)> を使う予定で、用字連続チェックを
6192迂回しない場合は、こうすることが必要でしょう。
6193
52776194=begin original
52786195
6196This feature is experimental, and the exact syntax and details of
6197operation are subject to change; using it yields a warning in the
6198C<experimental::script_run> category.
6199
6200=end original
6201
6202この機能は実験的で、正確な文法と操作の詳細は変更される予定です;
6203これを使うと、C<experimental::script_run> カテゴリの警告が出力されます。
6204
6205=begin original
6206
6207The C<Script_Extensions> property as modified by UTS 39
6208(L<http://unicode.org/reports/tr39/>) is used as the basis for this
6209feature.
6210
6211=end original
6212
6213この機能の基礎としては、UTS 39 (L<http://unicode.org/reports/tr39/>) によって
6214修正された C<Script_Extensions> 特性が使われます。
6215
6216=begin original
6217
6218To summarize,
6219
6220=end original
6221
6222まとめると:
6223
6224=over 4
6225
6226=item *
6227
6228=begin original
6229
6230All length 0 or length 1 sequences are script runs.
6231
6232=end original
6233
6234全ての長さ 0 または長さ 1 の並びは用字連続です。
6235
6236=item *
6237
6238=begin original
6239
6240A longer sequence is a script run if and only if B<all> of the following
6241conditions are met:
6242
6243=end original
6244
6245より長い並びは、以下の条件の B<全て> に合致した場合にのみ用字連続です:
6246
6247Z<>
6248
6249=over
6250
6251=item 1
6252
6253=begin original
6254
6255No code point in the sequence has the C<Script_Extension> property of
6256C<Unknown>.
6257
6258=end original
6259
6260並びの中に C<Script_Extension> 特性が C<Unknown> の符号位置がない。
6261
6262=begin original
6263
6264This currently means that all code points in the sequence have been
6265assigned by Unicode to be characters that aren't private use nor
6266surrogate code points.
6267
6268=end original
6269
6270これは現在の所、並びの中の全ての符号位置は、私用領域やサロゲート
6271符号位置でない、文字として Unicode によって割り当てられていることを
6272意味します。
6273
6274=item 2
6275
6276=begin original
6277
6278All characters in the sequence come from the Common script and/or the
6279Inherited script and/or a single other script.
6280
6281=end original
6282
6283並びの全ての文字は、Common 用字と Inherited 用字とその他の単一の用字の
6284いずれかである。
6285
6286=begin original
6287
6288The script of a character is determined by the C<Script_Extensions>
6289property as modified by UTS 39 (L<http://unicode.org/reports/tr39/>), as
6290described above.
6291
6292=end original
6293
6294文字の用字は、前述の UTS 39 (L<http://unicode.org/reports/tr39/>) で
6295修正された C<Script_Extensions> 特性によって決定されます。
6296
6297=item 3
6298
6299=begin original
6300
6301All decimal digits in the sequence come from the same block of 10
6302consecutive digits.
6303
6304=end original
6305
6306並びの全ての 10 進数字は 10 の連続した数字の同じブロックからの
6307ものである。
6308
6309=back
6310
6311=back
6312
6313=head2 Special Backtracking Control Verbs
6314
6315(特殊なバックトラック制御記号)
6316
6317=begin original
6318
6319These special patterns are generally of the form C<(*I<VERB>:I<arg>)>. Unless
6320otherwise stated the I<arg> argument is optional; in some cases, it is
6321mandatory.
6322
6323=end original
6324
6325これらの特殊なパターンは C<(*I<VERB>:I<arg>)> という一般形式を持っています。
6326特に記されていない限り、I<arg> はオプションです; 一部の場合では、
6327これは必須です。
6328
6329=begin original
6330
6331Any pattern containing a special backtracking verb that allows an argument
6332has the special behaviour that when executed it sets the current package's
6333C<$REGERROR> and C<$REGMARK> variables. When doing so the following
6334rules apply:
6335
6336=end original
6337
6338引数を許可する特殊バックトラック制御記号を含んでいる全てのパターンは、
6339それが実行されると現在のパッケージの C<$REGERROR> 及び C<$REGMARK> 変数を
6340設定する特殊な振る舞いを持っています。
6341これが行われる時以下の手順が適用されます。
6342
6343=begin original
6344
6345On failure, the C<$REGERROR> variable will be set to the I<arg> value of the
6346verb pattern, if the verb was involved in the failure of the match. If the
6347I<arg> part of the pattern was omitted, then C<$REGERROR> will be set to the
6348name of the last C<(*MARK:I<NAME>)> pattern executed, or to TRUE if there was
6349none. Also, the C<$REGMARK> variable will be set to FALSE.
6350
6351=end original
6352
6353失敗時には C<$REGERROR> 変数には、記号がマッチングの失敗の中で
6354使われていたのならその記号パターンの I<arg> の値がセットされます。
6355もしパターンの I<arg> 部分が省略されていたときには、C<$REGERROR> には
6356最後に実行された C<(*MARK:I<NAME>)> パターンの名前、またはそれもなければ
6357真に設定されます。
6358また、C<$REGMARK> 変数は偽に設定されます。
6359
6360=begin original
6361
6362On a successful match, the C<$REGERROR> variable will be set to FALSE, and
6363the C<$REGMARK> variable will be set to the name of the last
6364C<(*MARK:I<NAME>)> pattern executed. See the explanation for the
6365C<(*MARK:I<NAME>)> verb below for more details.
6366
6367=end original
6368
6369マッチングの成功時には、C<$REGERROR> 変数は偽に設定され、C<$REGMARK> 変数には
6370最後に実行された C<(*MARK:I<NAME>)> パターンの名前が設定されます。
6371詳細は C<(*MARK:I<NAME>)> 記号の説明を参照してください。
6372
6373=begin original
6374
6375B<NOTE:> C<$REGERROR> and C<$REGMARK> are not magic variables like C<$1>
6376and most other regex-related variables. They are not local to a scope, nor
6377readonly, but instead are volatile package variables similar to C<$AUTOLOAD>.
6378They are set in the package containing the code that I<executed> the regex
6379(rather than the one that compiled it, where those differ). If necessary, you
6380can use C<local> to localize changes to these variables to a specific scope
6381before executing a regex.
6382
6383=end original
6384
6385B<補足:> C<$REGERROR> 及び C<$REGMARK> は C<$1> や他の多くの
6386正規表現関連の変数のようにマジック変数ではありません。
6387それらはスコープ内にローカルにならず、読み込み専用でもありませんが、
6388C<$AUTOLOAD> と似た揮発するパッケージ変数です。
6389これらには正規表現が I<実行される> コードを含むパッケージが設定されます
6390(コンパイルされるコードではありません; これは異なることがあります)。
6391必要な場合は、正規表現を実行する前に
6392これらの変数の変更を特定のスコープ内に留めるために C<local> を使えます。
6393
6394=begin original
6395
6396If a pattern does not contain a special backtracking verb that allows an
6397argument, then C<$REGERROR> and C<$REGMARK> are not touched at all.
6398
6399=end original
6400
6401もしパターンが引数を許可する特殊バックトラック記号を含んでなかった場合には、
6402C<$REGERROR> 及び C<$REGMARK> は全く触られません。
6403
6404=over 3
6405
6406=item Verbs
6407
6408(動詞)
6409
6410=over 4
6411
6412=item C<(*PRUNE)> C<(*PRUNE:I<NAME>)>
6413X<(*PRUNE)> X<(*PRUNE:NAME)>
6414
6415=begin original
6416
6417This zero-width pattern prunes the backtracking tree at the current point
6418when backtracked into on failure. Consider the pattern C</I<A> (*PRUNE) I<B>/>,
6419where I<A> and I<B> are complex patterns. Until the C<(*PRUNE)> verb is reached,
6420I<A> may backtrack as necessary to match. Once it is reached, matching
6421continues in I<B>, which may also backtrack as necessary; however, should B
6422not match, then no further backtracking will take place, and the pattern
6423will fail outright at the current starting position.
6424
6425=end original
6426
6427このゼロ幅のパターンは失敗でバックトラックしてきたときに現在の位置で
6428バックトラックツリーを刈り取ります。
6429C</I<A> (*PRUNE) I<B>/> というパターンで I<A> も I<B> も複雑なパターンである時を
6430考えてみます。
6431C<(*PRUNE)> に達するまでは、I<A> はマッチングに必要であれば
6432バックトラックしていきます。
6433しかし一旦そこに達して I<B> に続くと、そこでも必要に応じてバックトラックします;
6434しかしながら、B がマッチングしなかったときにはそれ以上のバックトラックは
6435行われず、現在の開始位置でのマッチングはすぐに失敗します。
6436
6437=begin original
6438
6439The following example counts all the possible matching strings in a
6440pattern (without actually matching any of them).
6441
6442=end original
6443
6444次の例ではパターンに対してマッチングできるすべての文字列を(実際には
6445マッチングさせずに)数えます。
6446
6447 'aaab' =~ /a+b?(?{print "$&\n"; $count++})(*FAIL)/;
6448 print "Count=$count\n";
6449
6450=begin original
6451
6452which produces:
6453
6454=end original
6455
6456この出力:
6457
6458 aaab
6459 aaa
6460 aa
6461 a
6462 aab
6463 aa
6464 a
6465 ab
6466 a
6467 Count=9
6468
6469=begin original
6470
6471If we add a C<(*PRUNE)> before the count like the following
6472
6473=end original
6474
6475次のように数える前に C<(*PRUNE)> を加えると
6476
6477 'aaab' =~ /a+b?(*PRUNE)(?{print "$&\n"; $count++})(*FAIL)/;
6478 print "Count=$count\n";
6479
6480=begin original
6481
6482we prevent backtracking and find the count of the longest matching string
6483at each matching starting point like so:
6484
6485=end original
6486
6487バックトラックを妨げ次のように各開始位置での一番長いマッチング文字列を
6488数えるようになります:
6489
6490 aaab
6491 aab
6492 ab
6493 Count=3
6494
6495=begin original
6496
6497Any number of C<(*PRUNE)> assertions may be used in a pattern.
6498
6499=end original
6500
65011つのパターン内で C<(*PRUNE)> 言明はいくつでも使えます。
6502
6503=begin original
6504
6505See also C<<< L<< /(?>I<pattern>) >> >>> and possessive quantifiers for
6506other ways to
6507control backtracking. In some cases, the use of C<(*PRUNE)> can be
6508replaced with a C<< (?>pattern) >> with no functional difference; however,
6509C<(*PRUNE)> can be used to handle cases that cannot be expressed using a
6510C<< (?>pattern) >> alone.
6511
6512=end original
6513
6514バックトラックを制御する他の方法として C<<< L<< /(?>I<pattern>) >> >>>
6515及び絶対最大量指定子も参照してください。
6516幾つかのケースにおいては C<(*PRUNE)> の利用は機能的な違いなしに
6517C<< (?>pattern) >> で置き換えることができます; しかしながら C<(*PRUNE)> は
6518C<< (?>pattern) >> 単独では表現できないケースを扱うために使えます。
6519
6520=item C<(*SKIP)> C<(*SKIP:I<NAME>)>
6521X<(*SKIP)>
6522
6523=begin original
6524
6525This zero-width pattern is similar to C<(*PRUNE)>, except that on
6526failure it also signifies that whatever text that was matched leading up
6527to the C<(*SKIP)> pattern being executed cannot be part of I<any> match
6528of this pattern. This effectively means that the regex engine "skips" forward
6529to this position on failure and tries to match again, (assuming that
6530there is sufficient room to match).
6531
6532=end original
6533
6534このゼロ幅のパターンは C<*PRUNE> と似ていますが、実行されている
6535C<(*SKIP)> パターンまでにマッチングしたテキストはこのパターンの
6536I<どの>マッチングの一部にもならないことを示します。
6537これは正規表現エンジンがこの位置まで失敗として「スキップ」して(マッチングに
6538十分な空間があれば)再びマッチングを試みることを効率的に意味します。
6539
6540=begin original
6541
6542The name of the C<(*SKIP:I<NAME>)> pattern has special significance. If a
6543C<(*MARK:I<NAME>)> was encountered while matching, then it is that position
6544which is used as the "skip point". If no C<(*MARK)> of that name was
6545encountered, then the C<(*SKIP)> operator has no effect. When used
6546without a name the "skip point" is where the match point was when
6547executing the C<(*SKIP)> pattern.
6548
6549=end original
6550
6551C<(*SKIP:I<NAME>)> パターンの名前部分には特別な意味があります。
6552もしマッチングにおいて C<(*MARK:I<NAME>)> に遭遇すると、それは「スキップ
6553位置」として使われる位置になります。
6554その名前の C<(*MARK)> と東宮していなければ、C<(*SKIP)> 操作は効果を
6555持ちません。
6556名前がなければ「スキップ位置」は C<(*SKIP)> パターンの実行されたときに
6557マッチングポイントが使われます。
6558
6559=begin original
6560
6561Compare the following to the examples in C<(*PRUNE)>; note the string
6562is twice as long:
6563
6564=end original
6565
6566以下の例を C<(*PRUNE)> と比べてみてください;
6567文字列が2倍になってることに注意してください:
6568
6569 'aaabaaab' =~ /a+b?(*SKIP)(?{print "$&\n"; $count++})(*FAIL)/;
6570 print "Count=$count\n";
6571
6572=begin original
6573
6574outputs
6575
6576=end original
6577
6578これの出力は
6579
6580 aaab
6581 aaab
6582 Count=2
6583
6584=begin original
6585
6586Once the 'aaab' at the start of the string has matched, and the C<(*SKIP)>
6587executed, the next starting point will be where the cursor was when the
6588C<(*SKIP)> was executed.
6589
6590=end original
6591
6592いったん文字列の最初の 'aaab' がマッチングして、C<(*SKIP)> が実行されると、
6593次の開始位置は C<(*SKIP)> が実行されたときのカーソルがいた位置になります。
6594
6595=item C<(*MARK:I<NAME>)> C<(*:I<NAME>)>
6596X<(*MARK)> X<(*MARK:NAME)> X<(*:NAME)>
6597
6598=begin original
6599
6600This zero-width pattern can be used to mark the point reached in a string
6601when a certain part of the pattern has been successfully matched. This
6602mark may be given a name. A later C<(*SKIP)> pattern will then skip
6603forward to that point if backtracked into on failure. Any number of
6604C<(*MARK)> patterns are allowed, and the I<NAME> portion may be duplicated.
6605
6606=end original
6607
6608このゼロ幅のマッチングはパターン内の特定の箇所がマッチングに成功したときに、
6609文字列の中で達した位置を記録するために使われます。
6610このマークには名前をつけることもできます。
6611後者の C<(*SKIP)> パターンは失敗時でバックトラックしたときにその箇所まで
6612スキップします。
6613C<(*MARK)> パターンはいくつでも使うことができて、I<NAME> 部分は
6614重複することもあります。
6615
6616=begin original
6617
6618In addition to interacting with the C<(*SKIP)> pattern, C<(*MARK:I<NAME>)>
6619can be used to "label" a pattern branch, so that after matching, the
6620program can determine which branches of the pattern were involved in the
6621match.
6622
6623=end original
6624
6625C<(*SKIP)> パターンとの相互動作に加えて、C<(*MARK:I<NAME>)> はパターン分岐の
6626「ラベル」としても使うことができます; このためマッチングの後で、プログラムは
6627そのマッチングにおいてパターンのどの分岐が使われたのかを知ることができます。
6628
6629=begin original
6630
6631When a match is successful, the C<$REGMARK> variable will be set to the
6632name of the most recently executed C<(*MARK:I<NAME>)> that was involved
6633in the match.
6634
6635=end original
6636
6637マッチングの成功時に、C<$REGMARK> 変数はマッチングの中で一番最近に
6638実行された C<(*MARK:I<NAME>)> の名前を設定します。
6639
6640=begin original
6641
6642This can be used to determine which branch of a pattern was matched
6643without using a separate capture group for each branch, which in turn
6644can result in a performance improvement, as perl cannot optimize
6645C</(?:(x)|(y)|(z))/> as efficiently as something like
6646C</(?:x(*MARK:x)|y(*MARK:y)|z(*MARK:z))/>.
6647
6648=end original
6649
6650これは書く分岐で別々の捕捉グループを使うことなしにパターンのどの分岐が
6651マッチングしたのかを知るために使うことができます; これは perl は
6652C</(?:(x)|(y)|(z))/> を C</(?:x(*MARK:x)|y(*MARK:y)|z(*MARK:z))/> 程度に
6653効率的には最適化できないためパフォーマンスの向上をもたらします。
6654
6655=begin original
6656
6657When a match has failed, and unless another verb has been involved in
6658failing the match and has provided its own name to use, the C<$REGERROR>
6659variable will be set to the name of the most recently executed
6660C<(*MARK:I<NAME>)>.
6661
6662=end original
6663
6664マッチングが失敗して、そして他の記号がマッチングの失敗で行われずかつ名前を
6665持っているというのでなければ、C<$REGERROR> 変数には一番最近に実行された
6666C<(*MARK:I<NAME>)> の名前が設定されます。
6667
6668=begin original
6669
6670See L</(*SKIP)> for more details.
6671
6672=end original
6673
6674詳細は L</(*SKIP)> を参照してください。
6675
6676=begin original
6677
6678As a shortcut C<(*MARK:I<NAME>)> can be written C<(*:I<NAME>)>.
6679
6680=end original
6681
6682C<(*MARK:I<NAME>)> の短縮形として C<(*:I<NAME>)> とも記述できます。
6683
6684=item C<(*THEN)> C<(*THEN:I<NAME>)>
6685
6686=begin original
6687
6688This is similar to the "cut group" operator C<::> from Perl 6. Like
6689C<(*PRUNE)>, this verb always matches, and when backtracked into on
6690failure, it causes the regex engine to try the next alternation in the
6691innermost enclosing group (capturing or otherwise) that has alternations.
6692The two branches of a C<(?(I<condition>)I<yes-pattern>|I<no-pattern>)> do not
6693count as an alternation, as far as C<(*THEN)> is concerned.
6694
6695=end original
6696
6697これは Perl 6 の "cut group" 演算子 C<::> と似ています。
6698C<(*PRUNE)> のように、この記号は常にマッチングし、そして失敗で
6699バックトラックした時に正規表現エンジンに、代替のある一番内側で閉じている
6700グループ(キャプチャでもそうでなくとも)で次の代替を試みるようにさせます。
6701C<(*THEN)> が有効である限り、
6702C<(?(I<condition>)I<yes-pattern>|I<no-pattern>)> の二つの枝は代替とは
6703扱われません。
6704
6705=begin original
6706
6707Its name comes from the observation that this operation combined with the
6708alternation operator (C<"|">) can be used to create what is essentially a
6709pattern-based if/then/else block:
6710
6711=end original
6712
6713この名前は代替演算子 (C<"|">) と連結されたこの演算子で本質的にパターンベースの
6714if/then/else ブロックとなるものを作るために使うことが
6715できることからきています:
6716
6717 ( COND (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ )
6718
6719=begin original
6720
6721Note that if this operator is used and NOT inside of an alternation then
6722it acts exactly like the C<(*PRUNE)> operator.
6723
6724=end original
6725
6726この演算子が使われていてそしてそれが代替の内側ではなければ
6727これはちょうど C<(*PRUNE)> 演算子のように動作します。
6728
6729 / A (*PRUNE) B /
6730
6731=begin original
6732
6733is the same as
6734
6735=end original
6736
6737は次と同じです
6738
6739 / A (*THEN) B /
6740
6741=begin original
6742
6743but
6744
6745=end original
6746
6747しかし
6748
6749 / ( A (*THEN) B | C ) /
6750
6751=begin original
6752
6753is not the same as
6754
6755=end original
6756
6757は次と同じではありません
6758
6759 / ( A (*PRUNE) B | C ) /
6760
6761=begin original
6762
6763as after matching the I<A> but failing on the I<B> the C<(*THEN)> verb will
6764backtrack and try I<C>; but the C<(*PRUNE)> verb will simply fail.
6765
6766=end original
6767
6768I<A> にマッチングしたけれど I<B> に失敗した後 C<(*THEN)> 記号は
6769バックトラックして I<C> を試みます; しかし C<(*PRUNE)> 記号であれば
6770単純に失敗します。
6771
6772=item C<(*COMMIT)> C<(*COMMIT:I<arg>)>
6773X<(*COMMIT)>
6774
6775=begin original
6776
6777This is the Perl 6 "commit pattern" C<< <commit> >> or C<:::>. It's a
6778zero-width pattern similar to C<(*SKIP)>, except that when backtracked
6779into on failure it causes the match to fail outright. No further attempts
6780to find a valid match by advancing the start pointer will occur again.
6781For example,
6782
6783=end original
6784
6785これは Perl 6 の"コミットパターン" C<< <commit> >> または C<:::> です。
6786これは C<(*SKIP)> と似たゼロ幅のパターンですが、失敗でバックトラックした
6787際にマッチングがすぐに失敗する点で異なります。
6788それ以降で開始位置を進めて有効なマッチングを探す試行は行われません。
6789例えば、
6790
6791 'aaabaaab' =~ /a+b?(*COMMIT)(?{print "$&\n"; $count++})(*FAIL)/;
6792 print "Count=$count\n";
6793
6794=begin original
6795
6796outputs
6797
6798=end original
6799
6800これの出力は
6801
6802 aaab
6803 Count=1
6804
6805=begin original
6806
6807In other words, once the C<(*COMMIT)> has been entered, and if the pattern
6808does not match, the regex engine will not try any further matching on the
6809rest of the string.
6810
6811=end original
6812
6813言い換えると、いったん C<(*COMMIT)> に入った後に、そのパターンが
6814マッチングしなかったのなら、正規表現エンジンは文字列の残りに対して
6815それ以上のマッチングを試みません。
6816
6817=item C<(*FAIL)> C<(*F)> C<(*FAIL:I<arg>)>
6818X<(*FAIL)> X<(*F)>
6819
6820=begin original
6821
6822This pattern matches nothing and always fails. It can be used to force the
6823engine to backtrack. It is equivalent to C<(?!)>, but easier to read. In
6824fact, C<(?!)> gets optimised into C<(*FAIL)> internally. You can provide
6825an argument so that if the match fails because of this C<FAIL> directive
6826the argument can be obtained from C<$REGERROR>.
6827
6828=end original
6829
6830このパターンは何にもマッチングせず常に失敗します。
6831これはエンジンを強制的にバックトラックさせるために使えます。
6832これは C<(?!)> と等価ですが、より読みやすくなっています。
6833実際、C<(?!)> は内部的には C<(*FAIL)> に最適化されます。
6834この C<FAIL> 指示子によってマッチングが失敗したときに
6835C<$REGERROR> から得られる、引数を指定できます。
6836
6837=begin original
6838
6839It is probably useful only when combined with C<(?{})> or C<(??{})>.
6840
6841=end original
6842
6843これはおそらく C<(?{})> または C<(??{})> と組み合わせた時にだけ
6844役に立つでしょう。
6845
6846=item C<(*ACCEPT)> C<(*ACCEPT:I<arg>)>
6847X<(*ACCEPT)>
6848
6849=begin original
6850
6851This pattern matches nothing and causes the end of successful matching at
6852the point at which the C<(*ACCEPT)> pattern was encountered, regardless of
6853whether there is actually more to match in the string. When inside of a
6854nested pattern, such as recursion, or in a subpattern dynamically generated
6855via C<(??{})>, only the innermost pattern is ended immediately.
6856
6857=end original
6858
6859このパターンマッチングは何もせず C<(*ACCEPT)> パターンと遭遇した場所で
6860文字列の中で実際にもっとマッチングするものがあるかどうかにかかわらず
6861成功のマッチングを終了させます。
6862再帰、または C<(??{})> といったネストしたパターンの内側では、一番内側の
6863パターンのみがすぐに終了します。
6864
6865=begin original
6866
6867If the C<(*ACCEPT)> is inside of capturing groups then the groups are
6868marked as ended at the point at which the C<(*ACCEPT)> was encountered.
6869For instance:
6870
6871=end original
6872
6873C<(*ACCEPT)> が捕捉グループの内側で使われた場合捕捉グループは
6874C<(*ACCEPT)> と遭遇した位置で終了とマークされます。
6875例えば:
6876
6877 'AB' =~ /(A (A|B(*ACCEPT)|C) D)(E)/x;
6878
6879=begin original
6880
6881will match, and C<$1> will be C<AB> and C<$2> will be C<"B">, C<$3> will not
6882be set. If another branch in the inner parentheses was matched, such as in the
6883string 'ACDE', then the C<"D"> and C<"E"> would have to be matched as well.
6884
6885=end original
6886
6887はマッチングし、C<$1> は C<AB> になり、C<$2> は C<"B"> に、そして
6888C<$3> は設定されません。
6889'ACDE' のようにかっこの内側で他の分岐がマッチングしたのなら、
6890C<"D"> と C<"E"> もマッチングします。
6891
6892=begin original
6893
6894You can provide an argument, which will be available in the var
6895C<$REGMARK> after the match completes.
6896
6897=end original
6898
6899マッチングが成功した後に C<$REGMARK> 変数で利用可能な引数を指定できます。
6900
6901=back
6902
6903=back
6904
6905=head2 Warning on C<\1> Instead of C<$1>
6906
6907(C<$1> ではなく C<\1> だったときの警告)
6908
6909=begin original
6910
52796911Some people get too used to writing things like:
52806912
52816913=end original
52826914
52836915次のように書くことになれている人も中にはいるでしょう:
52846916
52856917 $pattern =~ s/(\W)/\\\1/g;
52866918
52876919=begin original
52886920
52896921This is grandfathered (for \1 to \9) for the RHS of a substitute to avoid
52906922shocking the
52916923B<sed> addicts, but it's a dirty habit to get into. That's because in
52926924PerlThink, the righthand side of an C<s///> is a double-quoted string. C<\1> in
52936925the usual double-quoted string means a control-A. The customary Unix
52946926meaning of C<\1> is kludged in for C<s///>. However, if you get into the habit
52956927of doing that, you get yourself into trouble if you then add an C</e>
52966928modifier.
52976929
52986930=end original
52996931
53006932(\1 から \9 については) B<sed> 中毒な人をびっくりさせないための RHS 置換の
53016933祖先ですが、しかしこれは汚らしい癖です。
53026934Perl においては、C<s///> の右側はダブルクォートされた文字列と
53036935考えられるためです。
53046936通常のダブルクォートされた文字列の中では C<\1> は control-A を意味します。
53056937C<\1> の Unix での習慣的な意味は C<s///> だけのその場しのぎです。
53066938しかしながら、この癖に漬かっていると C</e> 修飾子を使ったときに
53076939トラブルとなるでしょう。
53086940
53096941 s/(\d+)/ \1 + 1 /eg; # causes warning under -w
53106942
53116943=begin original
53126944
53136945Or if you try to do
53146946
53156947=end original
53166948
53176949または次のようにするかもしれません
53186950
53196951 s/(\d+)/\1000/;
53206952
53216953=begin original
53226954
53236955You can't disambiguate that by saying C<\{1}000>, whereas you can fix it with
53246956C<${1}000>. The operation of interpolation should not be confused
53256957with the operation of matching a backreference. Certainly they mean two
53266958different things on the I<left> side of the C<s///>.
53276959
53286960=end original
53296961
53306962これを解消するために C<\{1}000> としないでください;
53316963ここでは C<${1}000> とするべきです。
53326964埋め込みの処理は後方参照にマッチングさせる操作より混乱は少ないでしょう。
53336965特に C<s///> の I<左> 側では2つの異なった意味になります。
53346966
53356967=head2 Repeated Patterns Matching a Zero-length Substring
53366968
53376969(ゼロ幅の部分文字列にマッチングするパターンの繰り返し)
53386970
53396971=begin original
53406972
53416973B<WARNING>: Difficult material (and prose) ahead. This section needs a rewrite.
53426974
53436975=end original
53446976
53456977B<警告>: この先には難しい(そして無味乾燥な)内容があります。
53466978このセクションは書き直す必要があるでしょう。
53476979
53486980=begin original
53496981
53506982Regular expressions provide a terse and powerful programming language. As
53516983with most other power tools, power comes together with the ability
53526984to wreak havoc.
53536985
53546986=end original
53556987
53566988正規表現は簡潔でパワフルなプログラミング言語を提供します。
53576989他の多くの強力なツールとともに、力は破壊の源にもなります。
53586990
53596991=begin original
53606992
53616993A common abuse of this power stems from the ability to make infinite
53626994loops using regular expressions, with something as innocuous as:
53636995
53646996=end original
53656997
53666998この力のよくある乱用は無害な何かとともに、正規表現使った
53676999無限ループとなります。
53687000
53697001 'foo' =~ m{ ( o? )* }x;
53707002
53717003=begin original
53727004
5373The C<o?> matches at the beginning of C<'foo'>, and since the position
7005The C<o?> matches at the beginning of "C<foo>", and since the position
53747006in the string is not moved by the match, C<o?> would match again and again
5375because of the C<*> quantifier. Another common way to create a similar cycle
7007because of the C<"*"> quantifier. Another common way to create a similar cycle
5376is with the looping modifier C<//g>:
7008is with the looping modifier C</g>:
53777009
53787010=end original
53797011
5380C<o?> は C<'foo'> の始まりにマッチングし、文字列中での位置はこの
7012C<o?> は "C<foo>" の始まりにマッチングし、文字列中での位置はこの
5381マッチングでは動かないので、C<o?> は C<*> 量指定子によって何回も
7013マッチングでは動かないので、C<o?> は C<"*"> 量指定子によって何回も
53827014マッチングします。
5383同じような繰り返しを作るもう一つのよくある形として C<//g> 修飾子を使った
7015同じような繰り返しを作るもう一つのよくある形として C</g> 修飾子を使った
53847016ループがあります:
53857017
53867018 @matches = ( 'foo' =~ m{ o? }xg );
53877019
53887020=begin original
53897021
53907022or
53917023
53927024=end original
53937025
53947026または
53957027
53967028 print "match: <$&>\n" while 'foo' =~ m{ o? }xg;
53977029
53987030=begin original
53997031
5400or the loop implied by split().
7032or the loop implied by C<split()>.
54017033
54027034=end original
54037035
5404または split() による暗黙のループ。
7036または C<split()> による暗黙のループ。
54057037
54067038=begin original
54077039
54087040However, long experience has shown that many programming tasks may
54097041be significantly simplified by using repeated subexpressions that
54107042may match zero-length substrings. Here's a simple example being:
54117043
54127044=end original
54137045
54147046しかしながら、長きにわたる経験からいくつかのプログラミングタスクは
54157047ゼロ幅の部分文字列に対するマッチングを行う部分式の繰り返しで大幅に
54167048単純にできることがわかりました。
54177049簡単な例を挙げてみます:
54187050
54197051 @chars = split //, $string; # // is not magic in split
54207052 ($whitewashed = $string) =~ s/()/ /g; # parens avoid magic s// /
54217053
54227054=begin original
54237055
54247056Thus Perl allows such constructs, by I<forcefully breaking
54257057the infinite loop>. The rules for this are different for lower-level
54267058loops given by the greedy quantifiers C<*+{}>, and for higher-level
5427ones like the C</g> modifier or split() operator.
7059ones like the C</g> modifier or C<split()> operator.
54287060
54297061=end original
54307062
54317063このように Perl は I<強制的に無限ループを砕く> ことによってこういった構築を
54327064可能にしています。
54337065このためのルールは貪欲な量指定子 C<*+{}> によって与えられる
5434低レベルなループとも、C</g> 修飾子や split() 演算子による
7066低レベルなループとも、C</g> 修飾子や C<split()> 演算子による
54357067高レベルなループとも異なります。
54367068
54377069=begin original
54387070
54397071The lower-level loops are I<interrupted> (that is, the loop is
54407072broken) when Perl detects that a repeated expression matched a
54417073zero-length substring. Thus
54427074
54437075=end original
54447076
54457077低レベルなループは Perl がゼロ幅の部分文字列に対してマッチングする式が
54467078繰り返されたことを検出すると I<中断> されます (つまり、ループは壊されます)。
54477079従って
54487080
54497081 m{ (?: NON_ZERO_LENGTH | ZERO_LENGTH )* }x;
54507082
54517083=begin original
54527084
54537085is made equivalent to
54547086
54557087=end original
54567088
54577089は次と等価にされます
54587090
54597091 m{ (?: NON_ZERO_LENGTH )* (?: ZERO_LENGTH )? }x;
54607092
54617093=begin original
54627094
54637095For example, this program
54647096
54657097=end original
54667098
54677099例えば、以下のプログラムは
54687100
54697101 #!perl -l
54707102 "aaaaab" =~ /
54717103 (?:
54727104 a # non-zero
54737105 | # or
54747106 (?{print "hello"}) # print hello whenever this
54757107 # branch is tried
54767108 (?=(b)) # zero-width assertion
54777109 )* # any number of times
54787110 /x;
54797111 print $&;
54807112 print $1;
54817113
54827114=begin original
54837115
54847116prints
54857117
54867118=end original
54877119
54887120以下を表示します
54897121
54907122 hello
54917123 aaaaa
54927124 b
54937125
54947126=begin original
54957127
54967128Notice that "hello" is only printed once, as when Perl sees that the sixth
54977129iteration of the outermost C<(?:)*> matches a zero-length string, it stops
5498the C<*>.
7130the C<"*">.
54997131
55007132=end original
55017133
55027134"hello" は一度だけ表示されることに注目して下さい; Perl は
55037135一番外側の C<(?:)*> の 6 回目の繰り返しがゼロ長文字列にマッチングするのを
5504見るので、C<*> を止めます。
7136見るので、C<"*"> を止めます。
55057137
55067138=begin original
55077139
55087140The higher-level loops preserve an additional state between iterations:
55097141whether the last match was zero-length. To break the loop, the following
55107142match after a zero-length match is prohibited to have a length of zero.
5511This prohibition interacts with backtracking (see L<"Backtracking">),
7143This prohibition interacts with backtracking (see L</"Backtracking">),
55127144and so the I<second best> match is chosen if the I<best> match is of
55137145zero length.
55147146
55157147=end original
55167148
55177149高レベルのループは各繰り返しの間に最後のマッチングがゼロ幅だったかどうかを
55187150追加で保持しています。
55197151ループを終えるために、ゼロ幅のマッチングの後のマッチングはゼロ幅と
55207152なることを拒否します。
5521この禁則処理はバックトラックと相互に動作し(L<"Backtracking"> 参照)、そして
7153この禁則処理はバックトラックと相互に動作し(L</"Backtracking"> 参照)、そして
55227154I<ベストな> マッチングがゼロ幅だったのなら I<2 番目にベストな>
55237155マッチングが選択されます。
55247156
55257157=begin original
55267158
55277159For example:
55287160
55297161=end original
55307162
55317163例:
55327164
55337165 $_ = 'bar';
55347166 s/\w??/<$&>/g;
55357167
55367168=begin original
55377169
55387170results in C<< <><b><><a><><r><> >>. At each position of the string the best
55397171match given by non-greedy C<??> is the zero-length match, and the I<second
55407172best> match is what is matched by C<\w>. Thus zero-length matches
55417173alternate with one-character-long matches.
55427174
55437175=end original
55447176
55457177これは C<< <><b><><a><><r><> >> となります。
55467178文字列の各位置に於いて、貪欲でない C<??> によって得られるベストな
55477179マッチングはゼロ幅のマッチングです、
55487180そして I<2 番目にベストな>マッチングは C<\w> によってマッチングするものです。
55497181従ってゼロ幅のマッチングは 1 文字幅のマッチングの代替となります。
55507182
55517183=begin original
55527184
55537185Similarly, for repeated C<m/()/g> the second-best match is the match at the
55547186position one notch further in the string.
55557187
55567188=end original
55577189
55587190同じように、C<m/()/g> の繰り返しでは文字列中の境界一つ遠い位置に 2 番目に
55597191ベストなマッチングがマッチングします。
55607192
55617193=begin original
55627194
55637195The additional state of being I<matched with zero-length> is associated with
5564the matched string, and is reset by each assignment to pos().
7196the matched string, and is reset by each assignment to C<pos()>.
55657197Zero-length matches at the end of the previous match are ignored
55667198during C<split>.
55677199
55687200=end original
55697201
55707202I<ゼロ幅にマッチングしている> という追加の状態はマッチングした文字列に
5571関連づけられていて、pos() に対する割り当てによってリセットされます。
7203関連づけられていて、C<pos()> に対する割り当てによってリセットされます。
55727204前のマッチングの終端でのゼロ幅のマッチングは C<split> の間は無視されます。
55737205
55747206=head2 Combining RE Pieces
55757207
55767208(RE の欠片の結合)
55777209
55787210=begin original
55797211
55807212Each of the elementary pieces of regular expressions which were described
55817213before (such as C<ab> or C<\Z>) could match at most one substring
55827214at the given position of the input string. However, in a typical regular
55837215expression these elementary pieces are combined into more complicated
5584patterns using combining operators C<ST>, C<S|T>, C<S*> etc.
7216patterns using combining operators C<ST>, C<S|T>, C<S*> I<etc>.
5585(in these examples C<S> and C<T> are regular subexpressions).
7217(in these examples C<"S"> and C<"T"> are regular subexpressions).
55867218
55877219=end original
55887220
55897221これまでに説明された (C<ab> や C<\Z> といった) 正規表現の基本的な欠片
55907222それぞれは、入力文字列上の与えられた位置で多くとも1つの部分文字列に
55917223マッチングします。
55927224しかしながら、典型的な正規表現ではこれらの基本的な欠片は結合演算
5593C<ST>、C<S|T>、C<S*> 等(ここで C<S> や C<T> は正規表現の部分式)を使って
7225C<ST>、C<S|T>、C<S*> 等(ここで C<"S"> や C<"T"> は正規表現の部分式)を使って
55947226より複雑なパターンへと合成することができます。
55957227
55967228=begin original
55977229
55987230Such combinations can include alternatives, leading to a problem of choice:
55997231if we match a regular expression C<a|ab> against C<"abc">, will it match
56007232substring C<"a"> or C<"ab">? One way to describe which substring is
5601actually matched is the concept of backtracking (see L<"Backtracking">).
7233actually matched is the concept of backtracking (see L</"Backtracking">).
56027234However, this description is too low-level and makes you think
56037235in terms of a particular implementation.
56047236
56057237=end original
56067238
56077239このような合成には選択の問題を導くために代替を含めることができます:
56087240正規表現 C<a|ab> を C<"abc"> に対してマッチングさせようとしたとき、これは
56097241C<"a"> と C<"ab"> のどちらにマッチングするのか?
56107242実際にどちらがマッチングするのかを説明する1つの方法として、
5611バックトラッキングのコンセプトがあります(L<"Backtracking"> 参照)。
7243バックトラッキングのコンセプトがあります(L</"Backtracking"> 参照)。
56127244しかしながら、この説明は低レベルすぎて特定の実装を考えなければなりません。
56137245
56147246=begin original
56157247
56167248Another description starts with notions of "better"/"worse". All the
56177249substrings which may be matched by the given regular expression can be
56187250sorted from the "best" match to the "worst" match, and it is the "best"
56197251match which is chosen. This substitutes the question of "what is chosen?"
56207252by the question of "which matches are better, and which are worse?".
56217253
56227254=end original
56237255
56247256もう一つの説明は"より良い"/"より悪い"の考え方で始めます。
56257257与えられた正規表現にマッチングするすべての部分文字列は「最良の」
56267258マッチングから「最悪の」マッチングへとソートすることができます; そして
56277259「最良の」マッチングが選択されます。
56287260これは「どれが選ばれるのか?」という問いかけを「どのマッチングがより良くて、
56297261それがより悪いのか?」という問いかけに置き換えることができます。
56307262
56317263=begin original
56327264
56337265Again, for elementary pieces there is no such question, since at most
56347266one match at a given position is possible. This section describes the
56357267notion of better/worse for combining operators. In the description
5636below C<S> and C<T> are regular subexpressions.
7268below C<"S"> and C<"T"> are regular subexpressions.
56377269
56387270=end original
56397271
56407272そして、基本的な要素ではそういった問いかけはありません;
56417273なぜならこれらは与えられた位置で可能なマッチングは多くとも1つだからです。
56427274このセクションでは結合演算のより良い/より悪いの考え方で説明していきます。
5643以下の説明では C<S> 及び C<T> は正規表現の部分式です。
7275以下の説明では C<"S"> 及び C<"T"> は正規表現の部分式です。
56447276
56457277=over 4
56467278
56477279=item C<ST>
56487280
56497281=begin original
56507282
5651Consider two possible matches, C<AB> and C<A'B'>, C<A> and C<A'> are
7283Consider two possible matches, C<AB> and C<A'B'>, C<"A"> and C<A'> are
5652substrings which can be matched by C<S>, C<B> and C<B'> are substrings
7284substrings which can be matched by C<"S">, C<"B"> and C<B'> are substrings
5653which can be matched by C<T>.
7285which can be matched by C<"T">.
56547286
56557287=end original
56567288
565772892つの可能なマッチング、C<AB> 及び C<A'B'> を考えます;
5658ここで C<A> 及び C<A'> は C<S> にマッチングする部分文字列、
7290ここで C<"A"> 及び C<A'> は C<"S"> にマッチングする部分文字列、
5659そして C<B> 及び C<B'> は C<T> にマッチングする部分文字列とします。
7291そして C<"B"> 及び C<B'> は C<"T"> にマッチングする部分文字列とします。
56607292
56617293=begin original
56627294
5663If C<A> is a better match for C<S> than C<A'>, C<AB> is a better
7295If C<"A"> is a better match for C<"S"> than C<A'>, C<AB> is a better
56647296match than C<A'B'>.
56657297
56667298=end original
56677299
5668もし C<A> が C<S> に対して C<A'> よりも良いマッチングであれば、
7300もし C<"A"> が C<"S"> に対して C<A'> よりも良いマッチングであれば、
56697301C<AB> は C<A'B'> よりも良いマッチングです。
56707302
56717303=begin original
56727304
5673If C<A> and C<A'> coincide: C<AB> is a better match than C<AB'> if
7305If C<"A"> and C<A'> coincide: C<AB> is a better match than C<AB'> if
5674C<B> is a better match for C<T> than C<B'>.
7306C<"B"> is a better match for C<"T"> than C<B'>.
56757307
56767308=end original
56777309
5678もし C<A> と C<A'> が同じであれば: C<B> が C<T> に対して C<B'> よりも
7310もし C<"A"> と C<A'> が同じであれば: C<"B"> が C<"T"> に対して C<B'> よりも
56797311良いマッチングであれば C<AB> は C<AB'> よりも良いマッチングです。
56807312
56817313=item C<S|T>
56827314
56837315=begin original
56847316
5685When C<S> can match, it is a better match than when only C<T> can match.
7317When C<"S"> can match, it is a better match than when only C<"T"> can match.
56867318
56877319=end original
56887320
5689C<S> がマッチングできる時は C<T> のみがマッチングするよりも良い
7321C<"S"> がマッチングできる時は C<"T"> のみがマッチングするよりも良い
56907322マッチングです。
56917323
56927324=begin original
56937325
5694Ordering of two matches for C<S> is the same as for C<S>. Similar for
7326Ordering of two matches for C<"S"> is the same as for C<"S">. Similar for
5695two matches for C<T>.
7327two matches for C<"T">.
56967328
56977329=end original
56987330
5699C<S> に対する2つのマッチングの順序は C<S> と同じです。
7331C<"S"> に対する2つのマッチングの順序は C<"S"> と同じです。
5700C<T> に対する2つのマッチングも同様です。
7332C<"T"> に対する2つのマッチングも同様です。
57017333
57027334=item C<S{REPEAT_COUNT}>
57037335
57047336=begin original
57057337
57067338Matches as C<SSS...S> (repeated as many times as necessary).
57077339
57087340=end original
57097341
57107342C<SSS...S> (必要なだけ繰り返し)としてマッチングします。
57117343
57127344=item C<S{min,max}>
57137345
57147346=begin original
57157347
57167348Matches as C<S{max}|S{max-1}|...|S{min+1}|S{min}>.
57177349
57187350=end original
57197351
57207352C<S{max}|S{max-1}|...|S{min+1}|S{min}> としてマッチングします。
57217353
57227354=item C<S{min,max}?>
57237355
57247356=begin original
57257357
57267358Matches as C<S{min}|S{min+1}|...|S{max-1}|S{max}>.
57277359
57287360=end original
57297361
57307362C<S{min}|S{min+1}|...|S{max-1}|S{max}> としてマッチングします。
57317363
57327364=item C<S?>, C<S*>, C<S+>
57337365
57347366=begin original
57357367
57367368Same as C<S{0,1}>, C<S{0,BIG_NUMBER}>, C<S{1,BIG_NUMBER}> respectively.
57377369
57387370=end original
57397371
57407372それぞれ C<S{0,1}>, C<S{0,BIG_NUMBER}>, C<S{1,BIG_NUMBER}> と同じです。
57417373
57427374=item C<S??>, C<S*?>, C<S+?>
57437375
57447376=begin original
57457377
57467378Same as C<S{0,1}?>, C<S{0,BIG_NUMBER}?>, C<S{1,BIG_NUMBER}?> respectively.
57477379
57487380=end original
57497381
57507382それぞれ C<S{0,1}?>, C<S{0,BIG_NUMBER}?>, C<S{1,BIG_NUMBER}?> と同じです。
57517383
57527384=item C<< (?>S) >>
57537385
57547386=begin original
57557387
5756Matches the best match for C<S> and only that.
7388Matches the best match for C<"S"> and only that.
57577389
57587390=end original
57597391
5760C<S> の最良のみマッチングします。
7392C<"S"> の最良のみマッチングします。
57617393
57627394=item C<(?=S)>, C<(?<=S)>
57637395
57647396=begin original
57657397
5766Only the best match for C<S> is considered. (This is important only if
7398Only the best match for C<"S"> is considered. (This is important only if
5767C<S> has capturing parentheses, and backreferences are used somewhere
7399C<"S"> has capturing parentheses, and backreferences are used somewhere
57687400else in the whole regular expression.)
57697401
57707402=end original
57717403
5772C<S> の最良のマッチングのみが考慮されます。
7404C<"S"> の最良のマッチングのみが考慮されます。
5773(これは C<S> がキャプチャかっこを持っていて、そして正規表現全体の
7405(これは C<"S"> がキャプチャかっこを持っていて、そして正規表現全体の
57747406どこかで後方参照が使われている時のみ重要です.)
57757407
57767408=item C<(?!S)>, C<(?<!S)>
57777409
57787410=begin original
57797411
57807412For this grouping operator there is no need to describe the ordering, since
5781only whether or not C<S> can match is important.
7413only whether or not C<"S"> can match is important.
57827414
57837415=end original
57847416
5785このグループ演算子では、C<S> がマッチングできるかどうかのみが重要なので、
7417このグループ演算子では、C<"S"> がマッチングできるかどうかのみが重要なので、
57867418順序についての説明は必要ありません。
57877419
5788=item C<(??{ EXPR })>, C<(?PARNO)>
7420=item C<(??{ I<EXPR> })>, C<(?I<PARNO>)>
57897421
57907422=begin original
57917423
57927424The ordering is the same as for the regular expression which is
5793the result of EXPR, or the pattern contained by capture group PARNO.
7425the result of I<EXPR>, or the pattern contained by capture group I<PARNO>.
57947426
57957427=end original
57967428
5797順序は EXPR の結果の正規表現、または捕捉グループ PARNO に含まれている
7429順序は I<EXPR> の結果の正規表現、または捕捉グループ I<PARNO> に含まれている
57987430パターンと同じです。
57997431
5800=item C<(?(condition)yes-pattern|no-pattern)>
7432=item C<(?(I<condition>)I<yes-pattern>|I<no-pattern>)>
58017433
58027434=begin original
58037435
5804Recall that which of C<yes-pattern> or C<no-pattern> actually matches is
7436Recall that which of I<yes-pattern> or I<no-pattern> actually matches is
58057437already determined. The ordering of the matches is the same as for the
58067438chosen subexpression.
58077439
58087440=end original
58097441
5810既に決定している C<yes-pattern> または C<no-pattern> を実際に
7442既に決定している I<yes-pattern> または I<no-pattern> を実際に
58117443マッチングさせます。
58127444マッチングの順序は選択された部分式と同じです。
58137445
58147446=back
58157447
58167448=begin original
58177449
58187450The above recipes describe the ordering of matches I<at a given position>.
58197451One more rule is needed to understand how a match is determined for the
58207452whole regular expression: a match at an earlier position is always better
58217453than a match at a later position.
58227454
58237455=end original
58247456
58257457ここにあげたレシピはI<与えられた位置での>マッチングの順序について
58267458説明しています。
58277459正規表現全体でマッチングがどのように決定されるかを理解するためには
58287460もう少しルールが必要です:
58297461より若い位置でのマッチングは後ろの方でのマッチングよりもより良いです。
58307462
58317463=head2 Creating Custom RE Engines
58327464
58337465(カスタム RE エンジンの作成)
58347466
58357467=begin original
58367468
58377469As of Perl 5.10.0, one can create custom regular expression engines. This
58387470is not for the faint of heart, as they have to plug in at the C level. See
58397471L<perlreapi> for more details.
58407472
58417473=end original
58427474
58437475Perl 5.10.0 から、誰でもカスタム正規表現エンジンを作成できます。
58447476これは気弱な人向けではありません; C レベルでプラグインする必要があるからです。
58457477さらなる詳細については L<perlreapi> を参照して下さい。
58467478
58477479=begin original
58487480
58497481As an alternative, overloaded constants (see L<overload>) provide a simple
58507482way to extend the functionality of the RE engine, by substituting one
58517483pattern for another.
58527484
58537485=end original
58547486
58557487代替案として、オーバーロードされた定数(L<overload> 参照)は
58567488あるパターンを別のパターンに置き換えることで、RE エンジンの機能を
58577489拡張する簡単な方法を提供します。
58587490
58597491=begin original
58607492
58617493Suppose that we want to enable a new RE escape-sequence C<\Y|> which
58627494matches at a boundary between whitespace characters and non-whitespace
58637495characters. Note that C<(?=\S)(?<!\S)|(?!\S)(?<=\S)> matches exactly
58647496at these positions, so we want to have each C<\Y|> in the place of the
58657497more complicated version. We can create a module C<customre> to do
58667498this:
58677499
58687500=end original
58697501
58707502新しい正規表現エスケープシーケンス、空白文字と非空白文字との
58717503境界にマッチングする C<\Y|> を作ってみることにします。
58727504この位置には実際には C<(?=\S)(?<!\S)|(?!\S)(?<=\S)> がマッチングするので、
58737505この複雑なバージョンを C<\Y|> で置き換えたいとします。
58747506このために C<customre> モジュールを作ります:
58757507
58767508 package customre;
58777509 use overload;
58787510
58797511 sub import {
58807512 shift;
58817513 die "No argument to customre::import allowed" if @_;
58827514 overload::constant 'qr' => \&convert;
58837515 }
58847516
58857517 sub invalid { die "/$_[0]/: invalid escape '\\$_[1]'"}
58867518
58877519 # We must also take care of not escaping the legitimate \\Y|
58887520 # sequence, hence the presence of '\\' in the conversion rules.
58897521 my %rules = ( '\\' => '\\\\',
58907522 'Y|' => qr/(?=\S)(?<!\S)|(?!\S)(?<=\S)/ );
58917523 sub convert {
58927524 my $re = shift;
58937525 $re =~ s{
58947526 \\ ( \\ | Y . )
58957527 }
58967528 { $rules{$1} or invalid($re,$1) }sgex;
58977529 return $re;
58987530 }
58997531
59007532=begin original
59017533
59027534Now C<use customre> enables the new escape in constant regular
5903expressions, i.e., those without any runtime variable interpolations.
7535expressions, I<i.e.>, those without any runtime variable interpolations.
59047536As documented in L<overload>, this conversion will work only over
59057537literal parts of regular expressions. For C<\Y|$re\Y|> the variable
59067538part of this regular expression needs to be converted explicitly
5907(but only if the special meaning of C<\Y|> should be enabled inside $re):
7539(but only if the special meaning of C<\Y|> should be enabled inside C<$re>):
59087540
59097541=end original
59107542
59117543これで C<use customre> することで正規表現定数の中で新しいエスケープを
59127544使うことが出来ます; すなわち、これには何の実行時変数の埋め込みもいりません。
59137545L<overload> に書かれているように、この変換は正規表現のリテラル部分にのみ
59147546動作します。
59157547C<\Y|$re\Y|> であればこの正規表現の変数部分は明示的に変換する
5916必要があります(とはいえ $re の中でも C<\Y|> を有効にしたい時のみ)。
7548必要があります(とはいえ C<$re> の中でも C<\Y|> を有効にしたい時のみ)。
59177549
59187550 use customre;
59197551 $re = <>;
59207552 chomp $re;
59217553 $re = customre::convert $re;
59227554 /\Y|$re\Y|/;
59237555
7556=head2 Embedded Code Execution Frequency
7557
7558(組み込みコードの実行頻度)
7559
7560=begin original
7561
7562The exact rules for how often C<(??{})> and C<(?{})> are executed in a pattern
7563are unspecified. In the case of a successful match you can assume that
7564they DWIM and will be executed in left to right order the appropriate
7565number of times in the accepting path of the pattern as would any other
7566meta-pattern. How non-accepting pathways and match failures affect the
7567number of times a pattern is executed is specifically unspecified and
7568may vary depending on what optimizations can be applied to the pattern
7569and is likely to change from version to version.
7570
7571=end original
7572
7573パターン中で C<(??{})> と C<(?{})> がどれくらいの頻度で実行されるかの
7574正確な規則は未規定です。
7575マッチングが成功した場合、それらは DWIM を行い、
7576他のメタパターンと同様、
7577パターンの受け入れられたパスの中で左から右の順序で適切な回数
7578実行されることを仮定できます。
7579受け入れられなかったパスとマッチングの失敗がどれくらいパターンの実行回数に
7580影響を与えるかは明確に非規定で、
7581パターンにどの最適化が適用できるかに依存し、バージョン毎に
7582変わる可能性が高いです。
7583
7584=begin original
7585
7586For instance in
7587
7588=end original
7589
7590例えば:
7591
7592 "aaabcdeeeee"=~/a(?{print "a"})b(?{print "b"})cde/;
7593
7594=begin original
7595
7596the exact number of times "a" or "b" are printed out is unspecified for
7597failure, but you may assume they will be printed at least once during
7598a successful match, additionally you may assume that if "b" is printed,
7599it will be preceded by at least one "a".
7600
7601=end original
7602
7603失敗時に "a" や "b" が何回表示されるかは未規定ですが、
7604マッチングに成功したときに少なくとも 1 回表示されることは仮定でき、
7605さらに "b" が表示されるとき、その前には少なくとも 1 回 "a" が
7606表示されることも仮定できます。
7607
7608=begin original
7609
7610In the case of branching constructs like the following:
7611
7612=end original
7613
7614次のような分岐構文の場合:
7615
7616 /a(b|(?{ print "a" }))c(?{ print "c" })/;
7617
7618=begin original
7619
7620you can assume that the input "ac" will output "ac", and that "abc"
7621will output only "c".
7622
7623=end original
7624
7625入力が "ac" なら出力は "ac"、入力が "abc" なら出力は
7626"c" だけと仮定できます。
7627
7628=begin original
7629
7630When embedded code is quantified, successful matches will call the
7631code once for each matched iteration of the quantifier. For
7632example:
7633
7634=end original
7635
7636組み込みコードが量指定された場合、マッチングに成功すると
7637量指定子のそれぞれのマッチングした反復毎に 1 回コードを呼び出します。
7638例えば:
7639
7640 "good" =~ /g(?:o(?{print "o"}))*d/;
7641
7642=begin original
7643
7644will output "o" twice.
7645
7646=end original
7647
7648これは "o" を 2 回出力します。
7649
59247650=head2 PCRE/Python Support
59257651
59267652(PCRE/Python サポート)
59277653
59287654=begin original
59297655
59307656As of Perl 5.10.0, Perl supports several Python/PCRE-specific extensions
59317657to the regex syntax. While Perl programmers are encouraged to use the
59327658Perl-specific syntax, the following are also accepted:
59337659
59347660=end original
59357661
59367662Perl 5.10.0 時点では Perl は幾つかの Python/PCRE 的な正規表現構文拡張を
59377663サポートします。
59387664Perl プログラマはこれらの Perl としての構文を推奨しますが、以下のものも
59397665受理されます:
59407666
59417667=over 4
59427668
5943=item C<< (?PE<lt>NAMEE<gt>pattern) >>
7669=item C<< (?PE<lt>I<NAME>E<gt>I<pattern>) >>
59447670
59457671=begin original
59467672
5947Define a named capture group. Equivalent to C<< (?<NAME>pattern) >>.
7673Define a named capture group. Equivalent to C<< (?<I<NAME>>I<pattern>) >>.
59487674
59497675=end original
59507676
59517677名前付の捕捉グループの定義。
5952C<< (?<NAME>pattern) >> と等価。
7678C<< (?<I<NAME>>I<pattern>) >> と等価。
59537679
5954=item C<< (?P=NAME) >>
7680=item C<< (?P=I<NAME>) >>
59557681
59567682=begin original
59577683
5958Backreference to a named capture group. Equivalent to C<< \g{NAME} >>.
7684Backreference to a named capture group. Equivalent to C<< \g{I<NAME>} >>.
59597685
59607686=end original
59617687
59627688名前付捕捉グループへの後方参照。
5963C<< \g{NAME} >> と等価。
7689C<< \g{I<NAME>} >> と等価。
59647690
5965=item C<< (?P>NAME) >>
7691=item C<< (?P>I<NAME>) >>
59667692
59677693=begin original
59687694
5969Subroutine call to a named capture group. Equivalent to C<< (?&NAME) >>.
7695Subroutine call to a named capture group. Equivalent to C<< (?&I<NAME>) >>.
59707696
59717697=end original
59727698
59737699名前付き捕捉グループへの関数呼び出し。
5974C<< (?&NAME) >> と等価。
7700C<< (?&I<NAME>) >> と等価。
59757701
59767702=back
59777703
59787704=head1 BUGS
59797705
59807706=begin original
59817707
5982Many regular expression constructs don't work on EBCDIC platforms.
5983
5984=end original
5985
5986多くの正規表現構文は EBCDIC プラットフォームでは動作しません。
5987
5988=begin original
5989
59907708There are a number of issues with regard to case-insensitive matching
5991in Unicode rules. See C<i> under L</Modifiers> above.
7709in Unicode rules. See C<"i"> under L</Modifiers> above.
59927710
59937711=end original
59947712
59957713Unicode ルールでの大文字小文字を無視したマッチングには多くの問題が
59967714あります。
5997上述の L</Modifiers> の C<i> を参照してください。
7715上述の L</Modifiers> の C<"i"> を参照してください。
59987716
59997717=begin original
60007718
60017719This document varies from difficult to understand to completely
60027720and utterly opaque. The wandering prose riddled with jargon is
60037721hard to fathom in several places.
60047722
60057723=end original
60067724
60077725この文書は、理解が困難なところから、完全かつ徹底的に不明瞭なところまで
60087726さまざまです。
60097727jargon に満ちたとりとめのない散文は幾つかの箇所で理解するのに
60107728難儀ではあるでしょう。
60117729
60127730=begin original
60137731
60147732This document needs a rewrite that separates the tutorial content
60157733from the reference content.
60167734
60177735=end original
60187736
60197737この文書はリファレンス的な内容からチュートリアル的な内容を分離して
60207738書き直す必要があります。
60217739
60227740=head1 SEE ALSO
7741
7742=begin original
7743
7744The syntax of patterns used in Perl pattern matching evolved from those
7745supplied in the Bell Labs Research Unix 8th Edition (Version 8) regex
7746routines. (The code is actually derived (distantly) from Henry
7747Spencer's freely redistributable reimplementation of those V8 routines.)
7748
7749=end original
7750
7751Perl のパターンマッチングで使われる文法は、
7752the Bell Labs Research Unix 8th Edition (Version 8) 正規表現ルーチンで
7753提供されているものからの派生です。
7754(コードは実際には Henry Spencer の自由に再配布可能な V8 ルーチンの再実装から
7755(遠く)派生しています)。
60237756
60247757L<perlrequick>.
60257758
60267759L<perlretut>.
60277760
60287761L<perlop/"Regexp Quote-Like Operators">.
60297762
60307763L<perlop/"Gory details of parsing quoted constructs">.
60317764
60327765L<perlfaq6>.
60337766
60347767L<perlfunc/pos>.
60357768
60367769L<perllocale>.
60377770
60387771L<perlebcdic>.
60397772
60407773=begin original
60417774
60427775I<Mastering Regular Expressions> by Jeffrey Friedl, published
60437776by O'Reilly and Associates.
60447777
60457778=end original
60467779
60477780O'Reilly and Associates から出版されている、Jeffrey Friedl による
60487781I<Mastering Regular Expressions> (詳説 正規表現)
60497782
60507783=begin meta
60517784
60527785Translate: 山科 氷魚 (YAMASHINA Hio) <hio@hio.jp> (5.10.0)
60537786Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.1-)
60547787Status: completed
60557788
60567789=end meta