perlre >
5.16.1
との差分
perlre 5.16.1 と 5.28.0 の差分
1 | 1 | |
2 | 2 | =encoding euc-jp |
3 | 3 | |
4 | 4 | =head1 NAME |
5 | 5 | X<regular expression> X<regex> X<regexp> |
6 | 6 | |
7 | 7 | =begin original |
8 | 8 | |
9 | 9 | perlre - Perl regular expressions |
10 | 10 | |
11 | 11 | =end original |
12 | 12 | |
13 | 13 | perlre - Perl の正規表現 |
14 | 14 | |
15 | 15 | =head1 DESCRIPTION |
16 | 16 | |
17 | 17 | =begin original |
18 | 18 | |
19 | 19 | This page describes the syntax of regular expressions in Perl. |
20 | 20 | |
21 | 21 | =end original |
22 | 22 | |
23 | 23 | このページでは Perl での正規表現の構文について説明します。 |
24 | 24 | |
25 | 25 | =begin original |
26 | 26 | |
27 | If you haven't used regular expressions before, a | |
27 | If you haven't used regular expressions before, a tutorial introduction | |
28 | i | |
28 | is available in L<perlretut>. If you know just a little about them, | |
29 | introduction is available in L<perlre | |
29 | a quick-start introduction is available in L<perlrequick>. | |
30 | 30 | |
31 | 31 | =end original |
32 | 32 | |
33 | もしこれまでに正規表現を使ったことがない | |
33 | もしこれまでに正規表現を使ったことがないなら、 | |
34 | L<perlre | |
34 | L<perlretut> にチュートリアルがあります。 | |
35 | ||
35 | 正規表現を少しだけ知っているなら、L<perlrequick> に | |
36 | クイックスタートがあります。 | |
36 | 37 | |
37 | 38 | =begin original |
38 | 39 | |
39 | ||
40 | Except for L</The Basics> section, this page assumes you are familiar | |
40 | ||
41 | with regular expression basics, like what is a "pattern", what does it | |
41 | ||
42 | look like, and how it is basically used. For a reference on how they | |
42 | ||
43 | are used, plus various examples of the same, see discussions of C<m//>, | |
44 | C<s///>, C<qr//> and C<"??"> in L<perlop/"Regexp Quote-Like Operators">. | |
43 | 45 | |
44 | 46 | =end original |
45 | 47 | |
46 | ||
48 | L</The Basics> 節を例外として、このページは、「パターン」とは何か、 | |
49 | それはどのような見た目のものか、基本的にどのようにして使われるか、といった | |
50 | 正規表現の基本に親しんでいることを仮定しています。 | |
51 | それらをどのように使うのかやそれに関する様々な例に | |
47 | 52 | 関しては、L<perlop/"Regexp Quote-Like Operators"> にある |
48 | C<m//>, C<s///>, C<qr//>, C<??> の説明を参照して下さい。 | |
53 | C<m//>, C<s///>, C<qr//>, C<"??"> の説明を参照して下さい。 | |
49 | 54 | |
55 | =begin original | |
56 | ||
57 | New in v5.22, L<C<use re 'strict'>|re/'strict' mode> applies stricter | |
58 | rules than otherwise when compiling regular expression patterns. It can | |
59 | find things that, while legal, may not be what you intended. | |
60 | ||
61 | =end original | |
62 | ||
63 | v5.22 から、L<C<use re 'strict'>|re/'strict' mode> は、 | |
64 | 正規表現パターンをコンパイルするときにその他よりもより厳しい規則を | |
65 | 適用します。 | |
66 | これは、正当ではあるけれども、意図していないかもしれないものを | |
67 | 見つけることができます。 | |
68 | ||
69 | =head2 The Basics | |
70 | X<regular expression, version 8> X<regex, version 8> X<regexp, version 8> | |
71 | ||
72 | (基本) | |
73 | ||
74 | =begin original | |
75 | ||
76 | Regular expressions are strings with the very particular syntax and | |
77 | meaning described in this document and auxiliary documents referred to | |
78 | by this one. The strings are called "patterns". Patterns are used to | |
79 | determine if some other string, called the "target", has (or doesn't | |
80 | have) the characteristics specified by the pattern. We call this | |
81 | "matching" the target string against the pattern. Usually the match is | |
82 | done by having the target be the first operand, and the pattern be the | |
83 | second operand, of one of the two binary operators C<=~> and C<!~>, | |
84 | listed in L<perlop/Binding Operators>; and the pattern will have been | |
85 | converted from an ordinary string by one of the operators in | |
86 | L<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 | ||
107 | This evaluates to true if and only if the string in the variable C<$foo> | |
108 | contains somewhere in it, the sequence of characters "a", "b", then "c". | |
109 | (The C<=~ m>, or match operator, is described in | |
110 | L<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 | ||
121 | Patterns that aren't already stored in some variable must be delimitted, | |
122 | at both ends, by delimitter characters. These are often, as in the | |
123 | example above, forward slashes, and the typical way a pattern is written | |
124 | in documentation is with those slashes. In most cases, the delimitter | |
125 | is the same character, fore and aft, but there are a few cases where a | |
126 | character looks like it has a mirror-image mate, where the opening | |
127 | version is the beginning delimiter, and the closing one is the ending | |
128 | delimiter, like | |
129 | ||
130 | =end original | |
131 | ||
132 | 何らかの変数にまだ格納されていないパターンは、両端が区切り文字で | |
133 | 区切られている必要があります。 | |
134 | これらは上の例のようにスラッシュであることが多く、文書中で | |
135 | パターンを記述する一般的な方法はこれらのスラッシュです。 | |
136 | ほとんどの場合、区切り文字は前と後で同じ文字ですが、文字が鏡像のように | |
137 | 見える場合がいくつかあります; | |
138 | 次のように、開くのが開始区切り文字で、閉じるのが終了区切り文字です: | |
139 | ||
140 | $foo =~ m<abc> | |
141 | ||
142 | =begin original | |
143 | ||
144 | Most times, the pattern is evaluated in double-quotish context, but it | |
145 | is 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 | ||
156 | If the pattern contains its delimiter within it, that delimiter must be | |
157 | escaped. Prefixing it with a backslash (I<e.g.>, C<"/foo\/bar/">) | |
158 | serves this purpose. | |
159 | ||
160 | =end original | |
161 | ||
162 | パターン内に区切り文字が含まれている場合は、その区切り文字を | |
163 | エスケープする必要があります。 | |
164 | 逆スラッシュを前置すると (例えば、C<"/foo\/bar/">)、この目的を | |
165 | 達成できます。 | |
166 | ||
167 | =begin original | |
168 | ||
169 | Any single character in a pattern matches that same character in the | |
170 | target string, unless the character is a I<metacharacter> with a special | |
171 | meaning described in this document. A sequence of non-metacharacters | |
172 | matches the same sequence in the target string, as we saw above with | |
173 | C<m/abc/>. | |
174 | ||
175 | =end original | |
176 | ||
177 | パターン中のすべての単一の文字は、その文字が個々でまたはこの文書で | |
178 | 説明されている特別な意味を持った I<メタ文字> である場合以外は、 | |
179 | ターゲット文字列内の同じ文字にマッチングします。 | |
180 | 非メタ文字の並びは、前述の C<m/abc/> で見たように、 | |
181 | ターゲット文字列の同じ並びにマッチングします。 | |
182 | ||
183 | =begin original | |
184 | ||
185 | Only a few characters (all of them being ASCII punctuation characters) | |
186 | are metacharacters. The most commonly used one is a dot C<".">, which | |
187 | normally matches almost any character (including a dot itself). | |
188 | ||
189 | =end original | |
190 | ||
191 | ほんのいくつかの文字 (全て ASCII の句読点文字) がメタ文字です。 | |
192 | もっとも一般的に使われるものはドット C<"."> で、これは通常 | |
193 | (ドット自身を含む) ほとんどの文字にマッチングします。 | |
194 | ||
195 | =begin original | |
196 | ||
197 | You can cause characters that normally function as metacharacters to be | |
198 | interpreted literally by prefixing them with a C<"\">, just like the | |
199 | pattern's delimiter must be escaped if it also occurs within the | |
200 | pattern. Thus, C<"\."> matches just a literal dot, C<"."> instead of | |
201 | its normal meaning. This means that the backslash is also a | |
202 | metacharacter, so C<"\\"> matches a single C<"\">. And a sequence that | |
203 | contains an escaped metacharacter matches the same sequence (but without | |
204 | the escape) in the target string. So, the pattern C</blur\\fl/> would | |
205 | match 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 | ||
224 | The 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 | ||
235 | is TRUE if and only if C<$foo> contains either the sequence C<"this"> or | |
236 | the sequence C<"that">. Like all metacharacters, prefixing the C<"|"> | |
237 | with a backslash makes it match the plain punctuation character; in its | |
238 | case, 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 | ||
251 | is 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 | ||
260 | You 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 | ||
270 | is TRUE if and only if C<$foo> contains any of those 4 sequences from | |
271 | the children's story "Jack and the Beanstalk". | |
272 | ||
273 | =end original | |
274 | ||
275 | これは、C<$foo> に童話「ジャックとまめの木」から取った | |
276 | 四つの並びのいずれがを含んでいる場合にのみ真になります。 | |
277 | ||
278 | =begin original | |
279 | ||
280 | As you can see, the C<"|"> binds less tightly than a sequence of | |
281 | ordinary characters. We can override this by using the grouping | |
282 | metacharacters, 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 | ||
294 | is TRUE if and only if C<$foo> contains either the sequence S<C<"this | |
295 | thing">> or the sequence S<C<"that thing">>. The portions of the string | |
296 | that match the portions of the pattern enclosed in parentheses are | |
297 | normally made available separately for use later in the pattern, | |
298 | substitution, or program. This is called "capturing", and it can get | |
299 | complicated. See L</Capture groups>. | |
300 | ||
301 | =end original | |
302 | ||
303 | これは C<$foo> に並び S<C<"this thing">> または並び | |
304 | S<C<"that thing">> のいずれかが含まれている場合にのみ TRUE が返されます。 | |
305 | かっこで囲まれたパターンの部分と一致する文字列の部分は、通常、後でパターン、 | |
306 | 置換、プログラムで使用するために個別に使用できます。 | |
307 | これは「捕捉」(capturing)と呼ばれ、複雑になる場合があります。 | |
308 | L</Capture groups> を参照してください。 | |
309 | ||
310 | =begin original | |
311 | ||
312 | The first alternative includes everything from the last pattern | |
313 | delimiter (C<"(">, C<"(?:"> (described later), I<etc>. or the beginning | |
314 | of the pattern) up to the first C<"|">, and the last alternative | |
315 | contains everything from the last C<"|"> to the next closing pattern | |
316 | delimiter. That's why it's common practice to include alternatives in | |
317 | parentheses: 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 | ||
329 | Alternatives are tried from left to right, so the first | |
330 | alternative found for which the entire expression matches, is the one that | |
331 | is chosen. This means that alternatives are not necessarily greedy. For | |
332 | example: when matching C<foo|foot> against C<"barefoot">, only the C<"foo"> | |
333 | part will match, as that is the first alternative tried, and it successfully | |
334 | matches the target string. (This might not seem important, but it is | |
335 | important 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 | ||
350 | Besides taking away the special meaning of a metacharacter, a prefixed | |
351 | backslash changes some letter and digit characters away from matching | |
352 | just themselves to instead have special meaning. These are called | |
353 | "escape sequences", and all such are described in L<perlrebackslash>. A | |
354 | backslash sequence (of a letter or digit) that doesn't currently have | |
355 | special meaning to Perl will raise a warning if warnings are enabled, | |
356 | as 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 | ||
371 | One such sequence is C<\b>, which matches a boundary of some sort. | |
372 | C<\b{wb}> and a few others give specialized types of boundaries. | |
373 | (They are all described in detail starting at | |
374 | L<perlrebackslash/\b{}, \b, \B{}, \B>.) Note that these don't match | |
375 | characters, but the zero-width spaces between characters. They are an | |
376 | example of a L<zero-width assertion|/Assertions>. Consider again, | |
377 | ||
378 | =end original | |
379 | ||
380 | そのようなシーケンスのひとつは C<\b> です; | |
381 | これはある種の境界にマッチします。 | |
382 | C<\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 | ||
394 | It 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 | |
396 | judicious use of C<\b> (or better (because it is designed to handle | |
397 | natural language) C<\b{wb}>), we can make sure that only the Giant's | |
398 | words are matched: | |
399 | ||
400 | =end original | |
401 | ||
402 | これは、これら四つの単語以外に、"feed", "field", "Defoe", "fume", | |
403 | その他多くのシーケンスのいずれかが C<$foo> にある場合、TRUE と評価されます。 | |
404 | C<\b>(または(自然言語を処理するように設計されているため) より良い | |
405 | C<\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 | ||
413 | The final example shows that the characters C<"{"> and C<"}"> are | |
414 | metacharacters. | |
415 | ||
416 | =end original | |
417 | ||
418 | 最後の例は、文字 C<"{"> と C<"}"> がメタ文字であることを示しています。 | |
419 | ||
420 | =begin original | |
421 | ||
422 | Another use for escape sequences is to specify characters that cannot | |
423 | (or which you prefer not to) be written literally. These are described | |
424 | in detail in L<perlrebackslash/Character Escapes>, but the next three | |
425 | paragraphs briefly describe some of them. | |
426 | ||
427 | =end original | |
428 | ||
429 | エスケープシーケンスのもう一つの使用法は、文字通りに書くことができない | |
430 | (あるいは書きたくない)文字を指定することです。 | |
431 | これらについては | |
432 | L<perlrebackslash/Character Escapes> で詳しく説明していますが、 | |
433 | 次の三つの段落でその一部を簡単に説明します。 | |
434 | ||
435 | =begin original | |
436 | ||
437 | Various control characters can be written in C language style: C<"\n"> | |
438 | matches a newline, C<"\t"> a tab, C<"\r"> a carriage return, C<"\f"> a | |
439 | form feed, I<etc>. | |
440 | ||
441 | =end original | |
442 | ||
443 | 様々な制御文字は C 言語形式で書くことができます: | |
444 | "\n" は改行にマッチングし、C<"\t"> はタブに、C<"\r"> は復帰に、 | |
445 | C<"\f"> はフォームフィードにといった具合にマッチングします。 | |
446 | ||
447 | =begin original | |
448 | ||
449 | More generally, C<\I<nnn>>, where I<nnn> is a string of three octal | |
450 | digits, matches the character whose native code point is I<nnn>. You | |
451 | can easily run into trouble if you don't have exactly three digits. So | |
452 | always use three, or since Perl 5.14, you can use C<\o{...}> to specify | |
453 | any 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 | ||
465 | Similarly, C<\xI<nn>>, where I<nn> are hexadecimal digits, matches the | |
466 | character whose native ordinal is I<nn>. Again, not using exactly two | |
467 | digits is a recipe for disaster, but you can use C<\x{...}> to specify | |
468 | any 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 | ||
479 | Besides being a metacharacter, the C<"."> is an example of a "character | |
480 | class", something that can match any single character of a given set of | |
481 | them. In its case, the set is just about all possible characters. Perl | |
482 | predefines several character classes besides the C<".">; there is a | |
483 | separate reference page about just these, L<perlrecharclass>. | |
484 | ||
485 | =end original | |
486 | ||
487 | メタ文字であることに加えて、C<"."> は、特定の集合の任意の 1 文字に | |
488 | マッチングする「文字クラス」の例です。 | |
489 | この場合、集合ははほぼすべての可能な文字です。 | |
490 | Perlは C<"."> 以外にもいくつかの文字クラスを事前定義しています; | |
491 | これらについては、L<perlrecharclass> という別のリファレンスページが | |
492 | あります。 | |
493 | ||
494 | =begin original | |
495 | ||
496 | You can define your own custom character classes, by putting into your | |
497 | pattern in the appropriate place(s), a list of all the characters you | |
498 | want in the set. You do this by enclosing the list within C<[]> bracket | |
499 | characters. These are called "bracketed character classes" when we are | |
500 | being precise, but often the word "bracketed" is dropped. (Dropping it | |
501 | usually doesn't cause confusion.) This means that the C<"["> character | |
502 | is another metacharacter. It doesn't match anything just by itself; it | |
503 | is used only to tell Perl that what follows it is a bracketed character | |
504 | class. If you want to match a literal left square bracket, you must | |
505 | escape it, like C<"\[">. The matching C<"]"> is also a metacharacter; | |
506 | again it doesn't match anything by itself, but just marks the end of | |
507 | your custom class to Perl. It is an example of a "sometimes | |
508 | metacharacter". It isn't a metacharacter if there is no corresponding | |
509 | C<"[">, and matches its literal self: | |
510 | ||
511 | =end original | |
512 | ||
513 | 独自のカスタム文字クラスを定義するには、パターン内の適切な場所に、集合内に | |
514 | 必要なすべての文字のリストを配置します。 | |
515 | これを行うには、リストを C<[]> 大かっこ文字で囲みます。 | |
516 | これらは、正確にであれば「大かっこ文字クラス」と呼ばれますが、 | |
517 | 「大かっこ」という単語が削除されることがよくあります。 | |
518 | (通常は、これを削除しても混乱は生じません。) | |
519 | これは、C<"["> 文字はもう一つのメタ文字であることを意味します。 | |
520 | これ自身だけでは何にもマッチングしません; | |
521 | Perl に対して、後に続くものが大かっこ文字クラスであることを | |
522 | 伝えるためにのみ使用されます。 | |
523 | リテラルの左大かっこにマッチさせたい場合は、C<"\["> のように | |
524 | エスケープする必要があります。 | |
525 | 一致する C<"]"> もメタ文字です; | |
526 | ここでも何にもマッチしませんが、カスタムクラスの終わりを Perl に | |
527 | マークするだけです。 | |
528 | これは「時々メタ文字」の例です。 | |
529 | 対応する C<"["> が存在しない場合はメタ文字ではなく、 | |
530 | リテラルにマッチングします。 | |
531 | ||
532 | print "]" =~ /]/; # prints 1 | |
533 | ||
534 | =begin original | |
535 | ||
536 | The list of characters within the character class gives the set of | |
537 | characters matched by the class. C<"[abc]"> matches a single "a" or "b" | |
538 | or "c". But if the first character after the C<"["> is C<"^">, the | |
539 | class instead matches any character not in the list. Within a list, the | |
540 | C<"-"> character specifies a range of characters, so that C<a-z> | |
541 | represents all characters between "a" and "z", inclusive. If you want | |
542 | either C<"-"> or C<"]"> itself to be a member of a class, put it at the | |
543 | start of the list (possibly after a C<"^">), or escape it with a | |
544 | backslash. C<"-"> is also taken literally when it is at the end of the | |
545 | list, just before the closing C<"]">. (The following all specify the | |
546 | same class of three characters: C<[-az]>, C<[az-]>, and C<[a\-z]>. All | |
547 | are different from C<[a-z]>, which specifies a class containing | |
548 | twenty-six characters, even on EBCDIC-based character sets.) | |
549 | ||
550 | =end original | |
551 | ||
552 | 文字クラスの中の文字のリストは、そのクラスがマッチングする | |
553 | 文字の集合を表しています。 | |
554 | C<"[abc]"> は単一の "a" または "b" または "c" にマッチングします。. | |
555 | しかし、C<"["> の後の最初の文字が C<"^"> だったときには、その文字クラスは | |
556 | リストの中にない任意の文字にマッチングします。 | |
557 | リストの中では、文字 C<"-"> は文字の範囲を意味します; | |
558 | なので C<a-z> は "a" と "z" を含めてそれらの間にあるすべての文字を表します。 | |
559 | 文字クラスの要素として C<"-"> または C<"]"> 自身を使いたい時には、 | |
560 | リストの先頭に (あるいは C<"^"> の後に) 置くか、逆スラッシュを使って | |
561 | エスケープします。 | |
562 | C<"-"> はリストの終端、リストを閉じる C<"]"> の直前にあったときも | |
563 | リテラルとして扱われます。 | |
564 | (次の例はすべて同じ3文字からなる文字クラスです: C<[-az]>, C<[az-]>, | |
565 | C<[a\-z]>。 | |
566 | これらはすべて EBCDIC ベースの文字集合であっても26文字からなる文字集合 | |
567 | C<[a-z]> とは異なります。) | |
568 | ||
569 | =begin original | |
570 | ||
571 | There is lots more to bracketed character classes; full details are in | |
572 | L<perlrecharclass/Bracketed Character Classes>. | |
573 | ||
574 | =end original | |
575 | ||
576 | 大かっこ文字クラスにはもっと色々な要素があります; 完全な詳細は | |
577 | L<perlrecharclass/Bracketed Character Classes> にあります。 | |
578 | ||
579 | =head3 Metacharacters | |
580 | X<metacharacter> | |
581 | X<\> X<^> X<.> X<$> X<|> X<(> X<()> X<[> X<[]> | |
582 | ||
583 | (メタ文字) | |
584 | ||
585 | =begin original | |
586 | ||
587 | L</The Basics> introduced some of the metacharacters. This section | |
588 | gives them all. Most of them have the same meaning as in the I<egrep> | |
589 | command. | |
590 | ||
591 | =end original | |
592 | ||
593 | L</The Basics> ではメタ文字の一部を導入しました。 | |
594 | この節ではその全てを示します。 | |
595 | そのほとんどは I<egrep> コマンドと同じ意味を持ちます。 | |
596 | ||
597 | =begin original | |
598 | ||
599 | Only the C<"\"> is always a metacharacter. The others are metacharacters | |
600 | just sometimes. The following tables lists all of them, summarizes | |
601 | their use, and gives the contexts where they are metacharacters. | |
602 | Outside those contexts or if prefixed by a C<"\">, they match their | |
603 | corresponding punctuation character. In some cases, their meaning | |
604 | varies depending on various pattern modifiers that alter the default | |
605 | behaviors. See L</Modifiers>. | |
606 | ||
607 | =end original | |
608 | ||
609 | C<"\"> のみが常にメタ文字です。 | |
610 | その他は時々にだけメタ文字です。 | |
611 | 次の表は、すべてのメタ文字の一覧、使用方法の概要、 | |
612 | メタ文字になるコンテキストを示しています。 | |
613 | これらのコンテキスト以外では、または C<"\"> で始まる場合は、 | |
614 | 対応する句読点文字とマッチングします。 | |
615 | 場合によっては、既定の動作を変更するさまざまなパターン修飾子によって | |
616 | 意味が異なります。 | |
617 | L</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 | ||
684 | Notice that most of the metacharacters lose their special meaning when | |
685 | they occur in a bracketed character class, except C<"^"> has a different | |
686 | meaning when it is at the beginning of such a class. And C<"-"> and C<"]"> | |
687 | are metacharacters only at restricted positions within bracketed | |
688 | character classes; while C<"}"> is a metacharacter only when closing a | |
689 | special 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 | ||
703 | In double-quotish context, as is usually the case, you need to be | |
704 | careful about C<"$"> and the non-metacharacter C<"@">. Those could | |
705 | interpolate variables, which may or may not be what you intended. | |
706 | ||
707 | =end original | |
708 | ||
709 | ダブルクォート風のコンテキストでは、通常の場合と同様、 | |
710 | C<"$"> とメタ文字でない C<"@"> に注意する必要があります。 | |
711 | これらは変数を補完することができますが、それは | |
712 | 意図したものである場合とない場合があります。 | |
713 | ||
714 | =begin original | |
715 | ||
716 | These rules were designed for compactness of expression, rather than | |
717 | legibility and maintainability. The L</E<sol>x and E<sol>xx> pattern | |
718 | modifiers allow you to insert white space to improve readability. And | |
719 | use of S<C<L<re 'strict'|re/'strict' mode>>> adds extra checking to | |
720 | catch some typos that might silently compile into something unintended. | |
721 | ||
722 | =end original | |
723 | ||
724 | これらの規則は、読みやすさや保守性ではなく、表現のコンパクトさを | |
725 | 考慮して設計されています。 | |
726 | L</E<sol>x and E<sol>xx> パターン修飾子を使用すると、読みやすさを | |
727 | 向上させるために空白を挿入できます。 | |
728 | また、S<C<L<re 'strict'|re/'strict' mode>>> を使用すると、 | |
729 | 意図しないものに暗黙的にコンパイルされる可能性のあるタイプミスを | |
730 | 捕捉するための追加チェックが追加されます。 | |
731 | ||
732 | =begin original | |
733 | ||
734 | By default, the C<"^"> character is guaranteed to match only the | |
735 | beginning of the string, the C<"$"> character only the end (or before the | |
736 | newline at the end), and Perl does certain optimizations with the | |
737 | assumption that the string contains only one line. Embedded newlines | |
738 | will not be matched by C<"^"> or C<"$">. You may, however, wish to treat a | |
739 | string as a multi-line buffer, such that the C<"^"> will match after any | |
740 | newline within the string (except if the newline is the last character in | |
741 | the string), and C<"$"> will match before any newline. At the | |
742 | cost of a little more overhead, you can do this by using the | |
743 | L</C<E<sol>m>> modifier on the pattern match operator. (Older programs | |
744 | did this by setting C<$*>, but this option was removed in perl 5.10.) | |
745 | X<^> X<$> X</m> | |
746 | ||
747 | =end original | |
748 | ||
749 | デフォルトでは、文字 C<"^"> は文字列の先頭にのみ、そして文字 C<"$"> は | |
750 | 末尾(または末尾の改行の前)にのみマッチングすることを保証し、そして Perl は | |
751 | 文字列が 1 行のみを含んでいるという仮定でいくつかの最適化を行います。 | |
752 | 埋め込まれている改行文字は C<"^"> や C<"$"> とはマッチングしません。 | |
753 | しかし文字列には複数行が格納されていて、C<"^"> は任意の改行の後(但し | |
754 | 改行文字が文字列の最後の文字だった場合は除く)、そして C<"$"> は任意の改行の | |
755 | 前でマッチングさせたいこともあるでしょう。 | |
756 | 小さなオーバーヘッドはありますが、これはパターンマッチングで | |
757 | L</C<E<sol>m>> 修飾子を使うことで行うことができます。 | |
758 | (古いプログラムでは C<$*> を設定することでこれを行っていましたが | |
759 | これは perl 5.10 では削除されています。) | |
760 | X<^> X<$> X</m> | |
761 | ||
762 | =begin original | |
763 | ||
764 | To simplify multi-line substitutions, the C<"."> character never matches a | |
765 | newline unless you use the L<C<E<sol>s>|/s> modifier, which in effect tells | |
766 | Perl to pretend the string is a single line--even if it isn't. | |
767 | X<.> X</s> | |
768 | ||
769 | =end original | |
770 | ||
771 | 複数行での利用を簡単にするために、文字 C<"."> は L<C<E<sol>s>|/s> 修飾子を | |
772 | 使って Perl に文字列を 1 行として処理すると Perl に伝えない限り、 | |
773 | 改行にはマッチングしません。 | |
774 | X<.> X</s> | |
775 | ||
50 | 776 | =head2 Modifiers |
51 | 777 | |
52 | 778 | (修飾子) |
53 | 779 | |
780 | =head3 Overview | |
781 | ||
782 | (概要) | |
783 | ||
54 | 784 | =begin original |
55 | 785 | |
56 | ||
786 | The default behavior for matching can be changed, using various | |
57 | that relate to the interpretation of the | |
787 | modifiers. Modifiers that relate to the interpretation of the pattern | |
58 | are listed below. Modifiers that alter the way a | |
788 | are listed just below. Modifiers that alter the way a pattern is used | |
59 | ||
789 | by Perl are detailed in L<perlop/"Regexp Quote-Like Operators"> and | |
60 | 790 | L<perlop/"Gory details of parsing quoted constructs">. |
61 | 791 | |
62 | 792 | =end original |
63 | 793 | |
64 | マッチング | |
794 | マッチングのデフォルトの振る舞いは、様々な修飾子 (modifier) で | |
65 | ||
795 | 変更できます。 | |
66 | ||
796 | パターンの解釈に関連する修飾子は、直後に一覧にしています。 | |
797 | Perl がパターンを使う方法を変更する | |
67 | 798 | 修飾子は L<perlop/"Regexp Quote-Like Operators"> |
68 | 799 | 及び L<perlop/"Gory details of parsing quoted constructs"> に |
69 | 800 | 説明されています。 |
70 | 801 | |
71 | 802 | =over 4 |
72 | 803 | |
73 | =item m | |
804 | =item B<C<m>> | |
74 | 805 | X</m> X<regex, multiline> X<regexp, multiline> X<regular expression, multiline> |
75 | 806 | |
76 | 807 | =begin original |
77 | 808 | |
78 | Treat string as multiple lines. That is, change "^" and "$" from matching | |
809 | Treat the string being matched against as multiple lines. That is, change C<"^"> and C<"$"> from matching | |
79 | the start o | |
810 | the start of the string's first line and the end of its last line to | |
80 | ||
811 | matching the start and end of each line within the string. | |
81 | 812 | |
82 | 813 | =end original |
83 | 814 | |
84 | 文字列を複数行として扱います。 | |
815 | 文字列を複数行としてマッチングするように扱います。 | |
85 | つまり、"^" 及び "$" は文字列の最初 | |
816 | つまり、C<"^"> 及び C<"$"> は文字列の最初の行の先頭および最後の行の末尾に対する | |
86 | 文字列中の各行の | |
817 | マッチングから、文字列中の各行の銭湯と末尾に対するマッチングへと | |
818 | 変更されます。 | |
87 | 819 | |
88 | =item s | |
820 | =item B<C<s>> | |
89 | 821 | X</s> X<regex, single-line> X<regexp, single-line> |
90 | 822 | X<regular expression, single-line> |
91 | 823 | |
92 | 824 | =begin original |
93 | 825 | |
94 | Treat string as single line. That is, change "." to match any character | |
826 | Treat the string as single line. That is, change C<"."> to match any character | |
95 | 827 | whatsoever, even a newline, which normally it would not match. |
96 | 828 | |
97 | 829 | =end original |
98 | 830 | |
99 | 831 | 文字列を 1 行として扱います。 |
100 | つまり、"." は任意の 1 文字、通常はマッチングしない改行でさえも | |
832 | つまり、C<"."> は任意の 1 文字、通常はマッチングしない改行でさえも | |
101 | 833 | マッチングするように変更されます。 |
102 | 834 | |
103 | 835 | =begin original |
104 | 836 | |
105 | Used together, as C</ms>, they let the "." match any character whatsoever, | |
837 | Used together, as C</ms>, they let the C<"."> match any character whatsoever, | |
106 | while still allowing "^" and "$" to match, respectively, just after | |
838 | while still allowing C<"^"> and C<"$"> to match, respectively, just after | |
107 | 839 | and just before newlines within the string. |
108 | 840 | |
109 | 841 | =end original |
110 | 842 | |
111 | C</ms> として共に使うと、"^" 及び "$" はそれぞれ | |
843 | C</ms> として共に使うと、C<"^"> 及び C<"$"> はそれぞれ | |
112 | 文字列中の改行の直前及び直後のマッチングでありつつ、"." は任意の文字に | |
844 | 文字列中の改行の直前及び直後のマッチングでありつつ、C<"."> は任意の文字に | |
113 | 845 | マッチングするようになります。 |
114 | 846 | |
115 | =item i | |
847 | =item B<C<i>> | |
116 | 848 | X</i> X<regex, case-insensitive> X<regexp, case-insensitive> |
117 | 849 | X<regular expression, case-insensitive> |
118 | 850 | |
119 | 851 | =begin original |
120 | 852 | |
121 | Do case-insensitive pattern matching. | |
853 | Do case-insensitive pattern matching. For example, "A" will match "a" | |
854 | under C</i>. | |
122 | 855 | |
123 | 856 | =end original |
124 | 857 | |
125 | 858 | 大文字小文字を区別しないパターンマッチングを行います。 |
859 | 例えば、C</i> の下では "A" は "a" にマッチングします。 | |
126 | 860 | |
127 | 861 | =begin original |
128 | 862 | |
129 | 863 | If locale matching rules are in effect, the case map is taken from the |
130 | 864 | current |
131 | 865 | locale for code points less than 255, and from Unicode rules for larger |
132 | 866 | code points. However, matches that would cross the Unicode |
133 | rules/non-Unicode rules boundary (ords 255/256) will not succeed | |
867 | rules/non-Unicode rules boundary (ords 255/256) will not succeed, unless | |
134 | L<perllocale>. | |
868 | the locale is a UTF-8 one. See L<perllocale>. | |
135 | 869 | |
136 | 870 | =end original |
137 | 871 | |
138 | 872 | ロケールマッチングルールが有効になっている場合、符号位置 255 以下の場合は |
139 | 873 | 現在のロケールから取られ、より大きい符号位置では Unicode ルールから |
140 | 874 | 取られます。 |
141 | 875 | しかし、Unicode ルールと非 Unicode ルールの境界(番号255/256) を |
142 | またぐマッチングは成功しません。 | |
876 | またぐマッチングは、ロケールが UTF-8 のものでない限り成功しません。 | |
143 | 877 | L<perllocale> を参照してください。 |
144 | 878 | |
145 | 879 | =begin original |
146 | 880 | |
147 | There are a number of Unicode characters that match | |
881 | There are a number of Unicode characters that match a sequence of | |
148 | under C</i>. For example, | |
882 | multiple characters under C</i>. For example, | |
149 | should match the sequence C<fi>. Perl is not | |
883 | C<LATIN SMALL LIGATURE FI> should match the sequence C<fi>. Perl is not | |
150 | 884 | currently able to do this when the multiple characters are in the pattern and |
151 | 885 | are split between groupings, or when one or more are quantified. Thus |
152 | 886 | |
153 | 887 | =end original |
154 | 888 | |
155 | C</i> の基で複数の文字にマッチングする Unicode 文字はたくさんあります。 | |
889 | C</i> の基で複数の文字の並びにマッチングする Unicode 文字はたくさんあります。 | |
156 | 890 | 例えば、C<LATIN SMALL LIGATURE FI> は並び C<fi> にマッチングするべきです。 |
157 | 891 | 複数の文字がパターン中にあってグループ化で分割されている場合、または |
158 | 892 | どれかの文字に量指定子が付いている場合、Perl は今のところこれを行えません。 |
159 | 893 | 従って |
160 | 894 | |
161 | 895 | =begin original |
162 | 896 | |
163 | 897 | "\N{LATIN SMALL LIGATURE FI}" =~ /fi/i; # Matches |
164 | 898 | "\N{LATIN SMALL LIGATURE FI}" =~ /[fi][fi]/i; # Doesn't match! |
165 | 899 | "\N{LATIN SMALL LIGATURE FI}" =~ /fi*/i; # Doesn't match! |
166 | 900 | |
167 | 901 | =end original |
168 | 902 | |
169 | 903 | "\N{LATIN SMALL LIGATURE FI}" =~ /fi/i; # マッチング |
170 | 904 | "\N{LATIN SMALL LIGATURE FI}" =~ /[fi][fi]/i; # マッチングしない! |
171 | 905 | "\N{LATIN SMALL LIGATURE FI}" =~ /fi*/i; # マッチングしない! |
172 | 906 | |
173 | 907 | =begin original |
174 | 908 | |
175 | 909 | # The below doesn't match, and it isn't clear what $1 and $2 would |
176 | 910 | # be even if it did!! |
177 | 911 | "\N{LATIN SMALL LIGATURE FI}" =~ /(f)(i)/i; # Doesn't match! |
178 | 912 | |
179 | 913 | =end original |
180 | 914 | |
181 | 915 | # 次のものはマッチングしないし、もししたとしても $1 と $2 が何になるか |
182 | 916 | # はっきりしない!! |
183 | 917 | "\N{LATIN SMALL LIGATURE FI}" =~ /(f)(i)/i; # マッチングしない! |
184 | 918 | |
185 | 919 | =begin original |
186 | 920 | |
187 | Perl doesn't match multiple characters in a | |
921 | Perl doesn't match multiple characters in a bracketed | |
188 | character class | |
922 | character class unless the character that maps to them is explicitly | |
923 | mentioned, and it doesn't match them at all if the character class is | |
924 | inverted, which otherwise could be highly confusing. See | |
925 | L<perlrecharclass/Bracketed Character Classes>, and | |
189 | 926 | L<perlrecharclass/Negation>. |
190 | 927 | |
191 | 928 | =end original |
192 | 929 | |
193 | Perl は、 | |
930 | Perl は、明示的にマッピングについて言及されていない限り、 | |
931 | 大かっこ文字クラスの複数の文字にはマッチングしません; | |
932 | そして文字クラスが否定された場合はそれらには全くマッチングしません; | |
194 | 933 | さもなければとても混乱することがあるからです。 |
934 | L<perlrecharclass/Bracketed Character Classes> と | |
195 | 935 | L<perlrecharclass/Negation> を参照して下さい。 |
196 | 936 | |
937 | =item B<C<x>> and B<C<xx>> | |
938 | X</x> | |
939 | ||
940 | (B<C<x>> と B<C<xx>>) | |
941 | ||
197 | 942 | =begin original |
198 | 943 | |
199 | ||
944 | Extend your pattern's legibility by permitting whitespace and comments. | |
200 | ||
945 | Details in L</E<sol>x and E<sol>xx> | |
201 | example, | |
202 | 946 | |
203 | 947 | =end original |
204 | 948 | |
205 | ||
949 | 空白やコメントを許可してパターンを読みやすくするように拡張します。 | |
206 | ||
950 | 詳細は L</E<sol>x and E<sol>xx> にあります。 | |
207 | 例えば: | |
208 | 951 | |
209 | ||
952 | =item B<C<p>> | |
953 | X</p> X<regex, preserve> X<regexp, preserve> | |
210 | 954 | |
211 | 955 | =begin original |
212 | 956 | |
213 | s | |
957 | Preserve the string matched such that C<${^PREMATCH}>, C<${^MATCH}>, and | |
214 | ||
958 | C<${^POSTMATCH}> are available for use after matching. | |
215 | (L<[perl #89774]|https://rt.perl.org/rt3/Ticket/Display.html?id=89774>) | |
216 | causes it to only match a single "s", even if the final larger match | |
217 | fails, and matching the double "ss" would have succeeded. | |
218 | 959 | |
219 | 960 | =end original |
220 | 961 | |
221 | ||
962 | C<${^PREMATCH}>, C<${^MATCH}>, C<${^POSTMATCH}> といったマッチングされた | |
222 | ||
963 | 文字列をマッチングの後も使えるように維持します。 | |
223 | しかし、このバグ | |
224 | (L<[perl #89774]|https://rt.perl.org/rt3/Ticket/Display.html?id=89774>) は | |
225 | 例え最後のより大きいマッチングが失敗し、"ss" が成功しても、 | |
226 | 単一の "s2 にのみマッチングするようになります。 | |
227 | 964 | |
228 | 965 | =begin original |
229 | 966 | |
230 | ||
967 | In Perl 5.20 and higher this is ignored. Due to a new copy-on-write | |
231 | ||
968 | mechanism, C<${^PREMATCH}>, C<${^MATCH}>, and C<${^POSTMATCH}> will be available | |
232 | ||
969 | after the match regardless of the modifier. | |
233 | in the process of reconsidering and revising their recommendations. | |
234 | 970 | |
235 | 971 | =end original |
236 | 972 | |
237 | ||
973 | Perl 5.20 以降ではこれは無視されます。 | |
238 | ||
974 | 新しいコピーオンライト機構により、 | |
239 | ||
975 | C<${^PREMATCH}>, C<${^MATCH}>, and C<${^POSTMATCH}> はこの修飾子に関わらず | |
240 | ||
976 | マッチングの後も利用可能です。 | |
241 | 977 | |
242 | =item | |
978 | =item B<C<a>>, B<C<d>>, B<C<l>>, and B<C<u>> | |
243 | X</ | |
979 | X</a> X</d> X</l> X</u> | |
244 | 980 | |
981 | (B<C<a>>, B<C<d>>, B<C<l>>, B<C<u>>) | |
982 | ||
245 | 983 | =begin original |
246 | 984 | |
247 | ||
985 | These modifiers, all new in 5.14, affect which character-set rules | |
248 | ||
986 | (Unicode, I<etc>.) are used, as described below in | |
987 | L</Character set modifiers>. | |
249 | 988 | |
250 | 989 | =end original |
251 | 990 | |
252 | ||
991 | 5.14 から導入されたこれらの新しい修飾子は、どの文字集合規則 | |
253 | ||
992 | (Unicode など) が使われるかに影響を与えます; | |
993 | L</Character set modifiers> で後述します。 | |
254 | 994 | |
255 | =item | |
995 | =item B<C<n>> | |
256 | X</ | |
996 | X</n> X<regex, non-capture> X<regexp, non-capture> | |
997 | X<regular expression, non-capture> | |
257 | 998 | |
258 | 999 | =begin original |
259 | 1000 | |
260 | Pre | |
1001 | Prevent the grouping metacharacters C<()> from capturing. This modifier, | |
261 | ||
1002 | new in 5.22, will stop C<$1>, C<$2>, I<etc>... from being filled in. | |
262 | 1003 | |
263 | 1004 | =end original |
264 | 1005 | |
265 | ||
1006 | グループ化メタ文字 C<()> が捕捉しないようにします。 | |
266 | ||
1007 | 5.22 からのこの修飾子は、C<$1>, C<$2> などを埋めるのを止めます。 | |
267 | 1008 | |
268 | =i | |
1009 | "hello" =~ /(hi|hello)/; # $1 is "hello" | |
269 | ||
1010 | "hello" =~ /(hi|hello)/n; # $1 is undef | |
270 | 1011 | |
271 | ||
1012 | =begin original | |
272 | 1013 | |
1014 | This 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 | ||
273 | 1022 | =begin original |
274 | 1023 | |
275 | ||
1024 | C</n> can be negated on a per-group basis. Alternatively, named captures | |
276 | ||
1025 | may still be used. | |
277 | rather than the regex itself. See | |
278 | L<perlretut/"Using regular expressions in Perl"> for further explanation | |
279 | of the g and c modifiers. | |
280 | 1026 | |
281 | 1027 | =end original |
282 | 1028 | |
283 | グ | |
1029 | C</n> はグループ単位で否定できます。 | |
284 | ||
1030 | その代わりに、名前付き捕捉はまだ使えます。 | |
285 | 正規表現そのものではなく正規表現の使われ方に作用します。 | |
286 | g 及び c 修飾子の詳細な説明は | |
287 | L<perlretut/"Using regular expressions in Perl"> を参照してください。 | |
288 | 1031 | |
289 | =i | |
1032 | "hello" =~ /(?-n:(hi|hello))/n; # $1 is "hello" | |
290 | ||
1033 | "hello" =~ /(?<greet>hi|hello)/n; # $1 is "hello", $+{greet} is | |
1034 | # "hello" | |
291 | 1035 | |
292 | ||
1036 | =item Other Modifiers | |
293 | 1037 | |
1038 | (その他の修飾子) | |
1039 | ||
294 | 1040 | =begin original |
295 | 1041 | |
296 | The | |
1042 | There are a number of flags that can be found at the end of regular | |
297 | ||
1043 | expression constructs that are I<not> generic regular expression flags, but | |
298 | ||
1044 | apply to the operation being performed, like matching or substitution (C<m//> | |
1045 | or C<s///> respectively). | |
299 | 1046 | |
300 | 1047 | =end original |
301 | 1048 | |
302 | ||
1049 | 一般的な正規表現フラグ I<ではない> ですが、マッチングや置換 (それぞれ C<m//> | |
303 | ||
1050 | や C<s///>) のような操作が実行される時に適用される | |
304 | ||
1051 | 多くのフラグが正規表現構文の末尾に見つけられます。 | |
305 | 1052 | |
1053 | =begin original | |
1054 | ||
1055 | Flags described further in | |
1056 | L<perlretut/"Using regular expressions in Perl"> are: | |
1057 | ||
1058 | =end original | |
1059 | ||
1060 | L<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 | ||
1068 | Substitution-specific modifiers described in | |
1069 | L<perlop/"s/PATTERN/REPLACEMENT/msixpodualngcer"> are: | |
1070 | ||
1071 | =end original | |
1072 | ||
1073 | 置換専用の修飾子で | |
1074 | L<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 | ||
306 | 1081 | =back |
307 | 1082 | |
308 | 1083 | =begin original |
309 | 1084 | |
310 | 1085 | Regular expression modifiers are usually written in documentation |
311 | as e.g., "the C</x> modifier", even though the delimiter | |
1086 | as I<e.g.>, "the C</x> modifier", even though the delimiter | |
312 | in question might not really be a slash. The modifiers C</imsxadlup> | |
1087 | in question might not really be a slash. The modifiers C</imnsxadlup> | |
313 | 1088 | may also be embedded within the regular expression itself using |
314 | 1089 | the C<(?...)> construct, see L</Extended Patterns> below. |
315 | 1090 | |
316 | 1091 | =end original |
317 | 1092 | |
318 | 1093 | 正規表現修飾子は文書中では通常「C</x> 修飾子」のように記述され、 |
319 | 1094 | これは区切りが実際にはスラッシュでなくてもそう記述されます。 |
320 | また、C</imsxadlup> 修飾子は C<(?...)> 構築子を使って正規表現内に | |
1095 | また、C</imnsxadlup> 修飾子は C<(?...)> 構築子を使って正規表現内に | |
321 | 1096 | 埋め込まれることもあります; 後述する L</Extended Patterns> を |
322 | 1097 | 参照してください。 |
323 | 1098 | |
324 | =head3 | |
1099 | =head3 Details on some modifiers | |
325 | 1100 | |
1101 | (一部の修飾子の詳細) | |
1102 | ||
326 | 1103 | =begin original |
327 | 1104 | |
328 | ||
1105 | Some of the modifiers require more explanation than given in the | |
1106 | L</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 | ||
1118 | A single C</x> tells | |
329 | 1119 | the regular expression parser to ignore most whitespace that is neither |
330 | backslashed nor within a character class. You can use this to | |
1120 | backslashed nor within a bracketed character class. You can use this to | |
331 | your regular expression into | |
1121 | break up your regular expression into more readable parts. | |
332 | character is | |
1122 | Also, the C<"#"> character is treated as a metacharacter introducing a | |
333 | ||
1123 | comment that runs up to the pattern's closing delimiter, or to the end | |
334 | ||
1124 | of the current line if the pattern extends onto the next line. Hence, | |
335 | ||
1125 | this is very much like an ordinary Perl code comment. (You can include | |
1126 | the closing delimiter within the comment only if you precede it with a | |
1127 | backslash, 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 | ||
1142 | Use of C</x> means that if you want real | |
1143 | whitespace or C<"#"> characters in the pattern (outside a bracketed character | |
1144 | class, which is unaffected by C</x>), then you'll either have to | |
336 | 1145 | escape them (using backslashes or C<\Q...\E>) or encode them using octal, |
337 | hex, or C<\N{}> escapes. | |
1146 | hex, or C<\N{}> escapes. | |
338 | ||
1147 | It is ineffective to try to continue a comment onto the next line by | |
339 | ||
1148 | escaping the C<\n> with a backslash or C<\Q>. | |
340 | ||
341 | ||
1150 | =end original | |
1151 | ||
1152 | C</x> の使用はまた、(C</x> の影響を受けない大かっこ文字クラス内以外で) | |
1153 | パターン中に本当の空白や | |
1154 | C<"#"> 文字を使いたい場合は、(逆スラッシュや C<\Q...\E> を使って) | |
1155 | エスケープするか、8 進数、16 進数、C<\N{}> エスケープのいずれかで | |
1156 | エンコードする必要があると言うことです。 | |
1157 | C<\n> を逆スラッシュや C<\Q> でエスケープすることで | |
1158 | コメントを次の行まで続けようとしても無効です。 | |
1159 | ||
1160 | =begin original | |
1161 | ||
1162 | You can use L</(?#text)> to create a comment that ends earlier than the | |
1163 | end of the current line, but C<text> also can't contain the closing | |
1164 | delimiter unless escaped with a backslash. | |
1165 | ||
1166 | =end original | |
1167 | ||
1168 | 現在の行の末尾より早く修了するコメントを作るために | |
1169 | L</(?#text)> が使えますが、やはり C<text> は逆スラッシュで | |
1170 | エスケープされない限り閉じ区切り文字を含むことはできません。 | |
1171 | ||
1172 | =begin original | |
1173 | ||
1174 | A common pitfall is to forget that C<"#"> characters begin a comment under | |
1175 | C</x> and are not matched literally. Just keep that in mind when trying | |
1176 | to 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 | ||
1187 | Starting in Perl v5.26, if the modifier has a second C<"x"> within it, | |
1188 | it does everything that a single C</x> does, but additionally | |
1189 | non-backslashed SPACE and TAB characters within bracketed character | |
1190 | classes are also generally ignored, and hence can be added to make the | |
1191 | classes more readable. | |
1192 | ||
1193 | =end original | |
1194 | ||
1195 | Perl 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 | ||
1205 | may 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 | ||
1216 | Taken together, these features go a long way towards | |
1217 | making 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 | ||
1234 | Note that anything inside | |
342 | 1235 | a C<\Q...\E> stays unaffected by C</x>. And note that C</x> doesn't affect |
343 | 1236 | space interpretation within a single multi-character construct. For |
344 | 1237 | example in C<\x{...}>, regardless of the C</x> modifier, there can be no |
345 | 1238 | spaces. Same for a L<quantifier|/Quantifiers> such as C<{3}> or |
346 | C<{5,}>. Similarly, C<(?:...)> can't have a space between the C< | |
1239 | C<{5,}>. Similarly, C<(?:...)> can't have a space between the C<"(">, | |
347 | ||
1240 | C<"?">, and C<":">. Within any delimiters for such a | |
348 | 1241 | construct, allowed spaces are not affected by C</x>, and depend on the |
349 | 1242 | construct. For example, C<\x{...}> can't have spaces because hexadecimal |
350 | 1243 | numbers don't have spaces in them. But, Unicode properties can have spaces, so |
351 | 1244 | in C<\p{...}> there can be spaces that follow the Unicode rules, for which see |
352 | 1245 | L<perluniprops/Properties accessible through \p{} and \P{}>. |
353 | 1246 | X</x> |
354 | 1247 | |
355 | 1248 | =end original |
356 | 1249 | |
357 | C</x> | |
1250 | C<\Q...\E> の内側のものは C</x> の影響を受けないことに注意してください。 | |
358 | ほとんどの空白を無視するように正規表現パーサに伝えます。 | |
359 | これは正規表現を(少し)読みやすく部分に分割するために使えます。 | |
360 | また、C<#> は通常の Perl コードと同様コメントを開始するメタ文字として | |
361 | 扱われます。 | |
362 | これはまた、(C</x> の影響を受けない文字クラス内以外で)パターン中に本当の空白や | |
363 | C<#> 文字を使いたい場合は、(逆スラッシュや C<\Q...\E> を使って) | |
364 | エスケープするか、8 進数、16 進数、C<\N{}> エスケープのいずれかで | |
365 | エンコードする必要があると言うことです。 | |
366 | まとめると、これらの機能は Perl の正規表現をより読みやすくするために | |
367 | 大きく役立ちます。 | |
368 | コメントにパターン区切りを含まないように注意する必要があります--perl は | |
369 | 早くパターンを終了したいわけではないと言うことを知る手段がありません。 | |
370 | L<perlop> の C 型式のコメントを削除するコードを参照してください。 | |
371 | また、C<\Q...\E> の内側のものは C</x> の影響を受けないことにも | |
372 | 注意してください。 | |
373 | 1251 | 例えば、C<\x{...}> の内部では、C</x> 修飾子に関わらず、スペースを |
374 | 1252 | 含むことはできません。 |
375 | 1253 | C<{3}> や C<{5,}> のような L<量指定子|/Quantifiers> も同様です。 |
376 | また、C<(?:...)> も | |
1254 | また、C<(?:...)> も | |
377 | C<(> | |
1255 | C<"(">, C<"?">, C<":"> の間にスペースを含むことはできません。 | |
378 | 1256 | このような構文の区切り文字の中では、スペースが許されるかどうかは |
379 | 1257 | C</x> に影響されず、構文自身に影響されます。 |
380 | 1258 | 例えば、16 進数はスペースを含むことができないので C<\x{...}> はスペースを |
381 | 1259 | 含むことができません。 |
382 | 1260 | しかし、Unicode 特性はスペースを含むことができるので、 |
383 | 1261 | C<\p{...}> は Unicode の規則に従ってスペースを含むことができます; |
384 | 1262 | L<perluniprops/Properties accessible through \p{} and \P{}> を |
385 | 1263 | 参照してください。 |
386 | 1264 | X</x> |
387 | 1265 | |
388 | = | |
1266 | =begin original | |
389 | 1267 | |
1268 | The set of characters that are deemed whitespace are those that Unicode | |
1269 | calls "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 | ||
390 | 1290 | (文字集合修飾子) |
391 | 1291 | |
392 | 1292 | =begin original |
393 | 1293 | |
394 | 1294 | C</d>, C</u>, C</a>, and C</l>, available starting in 5.14, are called |
395 | the character set modifiers; they affect the character set | |
1295 | the character set modifiers; they affect the character set rules | |
396 | 1296 | used for the regular expression. |
397 | 1297 | |
398 | 1298 | =end original |
399 | 1299 | |
400 | 1300 | 5.14 から利用可能な C</d>, C</u>, C</a>, C</l> は文字集合修飾子と呼ばれます; |
401 | これらは正規表現で使われる文字集合 | |
1301 | これらは正規表現で使われる文字集合規則に影響を与えます。 | |
402 | 1302 | |
403 | 1303 | =begin original |
404 | 1304 | |
405 | 1305 | The C</d>, C</u>, and C</l> modifiers are not likely to be of much use |
406 | 1306 | to you, and so you need not worry about them very much. They exist for |
407 | 1307 | Perl's internal use, so that complex regular expression data structures |
408 | 1308 | can be automatically serialized and later exactly reconstituted, |
409 | 1309 | including all their nuances. But, since Perl can't keep a secret, and |
410 | 1310 | there may be rare instances where they are useful, they are documented |
411 | 1311 | here. |
412 | 1312 | |
413 | 1313 | =end original |
414 | 1314 | |
415 | 1315 | C</d>, C</u>, C</l> 修飾子はよく使うことはないだろうものなので、 |
416 | 1316 | これらについてあまり心配する必要はありません。 |
417 | 1317 | これらは Perl の内部仕様のために存在しているので、 |
418 | 1318 | 複雑な正規表現データ構造は自動的に直列化されて、その後全てのニュアンスを |
419 | 1319 | 含めて正確に再構成されます。 |
420 | 1320 | |
421 | 1321 | =begin original |
422 | 1322 | |
423 | 1323 | The C</a> modifier, on the other hand, may be useful. Its purpose is to |
424 | 1324 | allow code that is to work mostly on ASCII data to not have to concern |
425 | 1325 | itself with Unicode. |
426 | 1326 | |
427 | 1327 | =end original |
428 | 1328 | |
429 | 1329 | 一方、C</a> 修飾子は有用かもしれません。 |
430 | 1330 | この目的は、Unicode に関して考慮する必要がないように、コードを |
431 | 1331 | ほとんど ASCII データとして動作するようにすることです。 |
432 | 1332 | |
433 | 1333 | =begin original |
434 | 1334 | |
435 | 1335 | Briefly, C</l> sets the character set to that of whatever B<L>ocale is in |
436 | 1336 | effect at the time of the execution of the pattern match. |
437 | 1337 | |
438 | 1338 | =end original |
439 | 1339 | |
440 | 1340 | 簡単に言うと、C</l> は、文字集合をパターンマッチングの実行時に有効な |
441 | 1341 | ロケール(B<L>ocale)に設定します。 |
442 | 1342 | |
443 | 1343 | =begin original |
444 | 1344 | |
445 | 1345 | C</u> sets the character set to B<U>nicode. |
446 | 1346 | |
447 | 1347 | =end original |
448 | 1348 | |
449 | 1349 | C</u> は文字集合を B<U>nicode に設定します。 |
450 | 1350 | |
451 | 1351 | =begin original |
452 | 1352 | |
453 | 1353 | C</a> also sets the character set to Unicode, BUT adds several |
454 | 1354 | restrictions for B<A>SCII-safe matching. |
455 | 1355 | |
456 | 1356 | =end original |
457 | 1357 | |
458 | 1358 | C</a> も文字コードを Unicode に設定しますが、 |
459 | 1359 | B<A>SCII セーフなマッチングのためにいくつかの制限を加えます。 |
460 | 1360 | |
461 | 1361 | =begin original |
462 | 1362 | |
463 | 1363 | C</d> is the old, problematic, pre-5.14 B<D>efault character set |
464 | 1364 | behavior. Its only use is to force that old behavior. |
465 | 1365 | |
466 | 1366 | =end original |
467 | 1367 | |
468 | 1368 | C</d> は古くて問題のある、5.14 以前のデフォルト(B<D>efault)文字集合の |
469 | 1369 | 振る舞いです。 |
470 | 1370 | これは古い振る舞いを強制するためだけに使います。 |
471 | 1371 | |
472 | 1372 | =begin original |
473 | 1373 | |
474 | 1374 | At any given time, exactly one of these modifiers is in effect. Their |
475 | 1375 | existence allows Perl to keep the originally compiled behavior of a |
476 | 1376 | regular expression, regardless of what rules are in effect when it is |
477 | 1377 | actually executed. And if it is interpolated into a larger regex, the |
478 | 1378 | original's rules continue to apply to it, and only it. |
479 | 1379 | |
480 | 1380 | =end original |
481 | 1381 | |
482 | 1382 | 任意のある瞬間において、これらの修飾子の内正確に一つだけが有効になります。 |
483 | 1383 | これにより、 |
484 | 1384 | それが実際に実行されるときにどの規則が有効かに関わらず、 |
485 | 1385 | Perl が元々コンパイルされた正規表現の振る舞いを保存できるようにします。 |
486 | 1386 | そしてそれがより大きな正規表現に展開された場合、元の規則だけが |
487 | 1387 | 適用され続けます。 |
488 | 1388 | |
489 | 1389 | =begin original |
490 | 1390 | |
491 | 1391 | The C</l> and C</u> modifiers are automatically selected for |
492 | 1392 | regular expressions compiled within the scope of various pragmas, |
493 | 1393 | and we recommend that in general, you use those pragmas instead of |
494 | 1394 | specifying these modifiers explicitly. For one thing, the modifiers |
495 | 1395 | affect only pattern matching, and do not extend to even any replacement |
496 | done, whereas using the pragmas give consistent results for all | |
1396 | done, whereas using the pragmas gives consistent results for all | |
497 | 1397 | appropriate operations within their scopes. For example, |
498 | 1398 | |
499 | 1399 | =end original |
500 | 1400 | |
501 | 1401 | C</l> と C</u> の修飾子は、様々なプラグマのスコープ内でコンパイルされた |
502 | 1402 | 正規表現で自動的に選択されます; |
503 | 1403 | 一般的にはこれらの修飾子を明示的に使うのではなく、これらのプラグマを |
504 | 1404 | 使うことを勧めます。 |
505 | 1405 | 一例を挙げると、修飾子はパターンマッチングに対してのみ影響を与え、 |
506 | 1406 | 置換には拡張されないことに注意してください; |
507 | 1407 | いっぽうプラグマを使うと、そのスコープ内の全ての適切な操作について |
508 | 1408 | 一貫した結果となります。 |
509 | 1409 | 例えば: |
510 | 1410 | |
511 | 1411 | s/foo/\Ubar/il |
512 | 1412 | |
513 | 1413 | =begin original |
514 | 1414 | |
515 | 1415 | will match "foo" using the locale's rules for case-insensitive matching, |
516 | 1416 | but the C</l> does not affect how the C<\U> operates. Most likely you |
517 | 1417 | want both of them to use locale rules. To do this, instead compile the |
518 | 1418 | regular expression within the scope of C<use locale>. This both |
519 | implicitly adds the C</l> and applies locale rules to the C<\U>. The | |
1419 | implicitly adds the C</l>, and applies locale rules to the C<\U>. The | |
520 | lesson is to C<use locale> and not C</l> explicitly. | |
1420 | lesson is to C<use locale>, and not C</l> explicitly. | |
521 | 1421 | |
522 | 1422 | =end original |
523 | 1423 | |
524 | 1424 | これは大文字小文字マッチングにロケールの規則を使って "foo" に |
525 | 1425 | マッチングしますが、C</l> は C<\U> がどう処理を行うかに影響を与えません。 |
526 | 1426 | あなたはほぼ確実にこれら二つにロケールの規則を使うことを臨むはずです。 |
527 | 1427 | これをするためには、代わりに |
528 | 1428 | C<use locale> のスコープ内で正規表現をコンパイルします。 |
529 | これらは両方とも暗黙に C</l> を追加し、C<\U> にロケールの規則を適用します。 | |
1429 | これらは両方とも暗黙に C</l> を追加し、C<\U> にロケールの規則を適用します。 | |
530 | 1430 | ここで学ぶべきことは、C<use locale> を使って、明示的に C</l> を |
531 | 1431 | 使わないことです。 |
532 | 1432 | |
533 | 1433 | =begin original |
534 | 1434 | |
535 | 1435 | Similarly, it would be better to use C<use feature 'unicode_strings'> |
536 | 1436 | instead of, |
537 | 1437 | |
538 | 1438 | =end original |
539 | 1439 | |
540 | 1440 | 同様に、Unicode の規則にするには次のようにする代わりに |
541 | 1441 | C<use feature 'unicode_strings'> を使った方が良いです: |
542 | 1442 | |
543 | 1443 | s/foo/\Lbar/iu |
544 | 1444 | |
545 | 1445 | =begin original |
546 | 1446 | |
547 | 1447 | to get Unicode rules, as the C<\L> in the former (but not necessarily |
548 | 1448 | the latter) would also use Unicode rules. |
549 | 1449 | |
550 | 1450 | =end original |
551 | 1451 | |
552 | 1452 | 前者の C<\L> (しかし後者は必要ではないです) も Unicode の規則を |
553 | 1453 | 使うからです。 |
554 | 1454 | |
555 | 1455 | =begin original |
556 | 1456 | |
557 | 1457 | More detail on each of the modifiers follows. Most likely you don't |
558 | 1458 | need to know this detail for C</l>, C</u>, and C</d>, and can skip ahead |
559 | 1459 | to L<E<sol>a|/E<sol>a (and E<sol>aa)>. |
560 | 1460 | |
561 | 1461 | =end original |
562 | 1462 | |
563 | 1463 | それぞれの修飾子に関するさらなる詳細は後述します。 |
564 | 1464 | ほとんど確実にあなたは C</l>, C</u>, C</d> の詳細を知る必要はなく、 |
565 | 1465 | L<E<sol>a|/E<sol>a (and E<sol>aa)> まで読み飛ばせます。 |
566 | 1466 | |
567 | 1467 | =head4 /l |
568 | 1468 | |
569 | 1469 | =begin original |
570 | 1470 | |
571 | 1471 | means to use the current locale's rules (see L<perllocale>) when pattern |
572 | 1472 | matching. For example, C<\w> will match the "word" characters of that |
573 | 1473 | locale, and C<"/i"> case-insensitive matching will match according to |
574 | 1474 | the locale's case folding rules. The locale used will be the one in |
575 | 1475 | effect at the time of execution of the pattern match. This may not be |
576 | 1476 | the same as the compilation-time locale, and can differ from one match |
577 | 1477 | to another if there is an intervening call of the |
578 | 1478 | L<setlocale() function|perllocale/The setlocale function>. |
579 | 1479 | |
580 | 1480 | =end original |
581 | 1481 | |
582 | 1482 | これはパターンマッチングのときに現在のロケールの規則 (L<perllocale> 参照) を |
583 | 1483 | 使うことを意味します。 |
584 | 1484 | 例えば、C<\w> はこのロケールの「単語」文字にマッチングし、 |
585 | 1485 | C<"/i"> の大文字小文字マッチングは、現在のロケールの大文字小文字畳み込み規則に |
586 | 1486 | 従ってマッチングします。 |
587 | 1487 | 使われるロケールはパターンマッチングが実行される時点で有効なものです。 |
588 | 1488 | これはコンパイル時のロケールと同じではないかもしれませんし、 |
589 | 1489 | L<setlocale() 関数|perllocale/The setlocale function> |
590 | 1490 | の呼び出しが間に入ると、呼び出し毎に異なることもあります。 |
591 | 1491 | |
592 | 1492 | =begin original |
593 | 1493 | |
594 | Perl | |
1494 | Prior to v5.20, Perl did not support multi-byte locales. Starting then, | |
595 | ||
1495 | UTF-8 locales are supported. No other multi byte locales are ever | |
1496 | likely to be supported. However, in all locales, one can have code | |
1497 | points above 255 and these will always be treated as Unicode no matter | |
1498 | what locale is in effect. | |
1499 | ||
1500 | =end original | |
1501 | ||
1502 | v5.20 より前では、Perl は複数バイトロケールに対応していませんでした。 | |
1503 | v5.20 から、UTF-8 ロケールに対応しました。 | |
1504 | 他の複数バイトロケールに対応することはおそらくありません。 | |
1505 | しかし、全てのロケールで、255 を越える符号位置を保持することができ、 | |
1506 | これはどのロケールが有効であるかに関わらず常に | |
1507 | Unicode として扱われます。 | |
1508 | ||
1509 | =begin original | |
1510 | ||
596 | 1511 | Under Unicode rules, there are a few case-insensitive matches that cross |
597 | the 255/256 boundary. | |
1512 | the 255/256 boundary. Except for UTF-8 locales in Perls v5.20 and | |
598 | ||
1513 | later, these are disallowed under C</l>. For example, 0xFF (on ASCII | |
599 | ||
1514 | platforms) does not caselessly match the character at 0x178, C<LATIN | |
600 | C | |
1515 | CAPITAL LETTER Y WITH DIAERESIS>, because 0xFF may not be C<LATIN SMALL | |
601 | ||
1516 | LETTER Y WITH DIAERESIS> in the current locale, and Perl has no way of | |
602 | ||
1517 | knowing if that character even exists in the locale, much less what code | |
1518 | point it is. | |
603 | 1519 | |
604 | 1520 | =end original |
605 | 1521 | |
606 | Perl は単一バイトロケールのみに対応します。 | |
607 | つまり、255 を越える符号位置は、どのロケールが有効であるかに関わらず | |
608 | Unicode として扱われるということです。 | |
609 | 1522 | Unicode の規則の基では、255/256 の境界をまたぐ大文字小文字を無視した |
610 | 1523 | マッチングがいくつかあります。 |
1524 | Perl v5.20 以降での UTF-8 ロケールを除いて、 | |
611 | 1525 | これらは C</l> の基では認められません。 |
612 | 1526 | 例えば、(ASCII プラットフォームで) 0xFF は 0x178, |
613 | 1527 | C<LATIN CAPITAL LETTER Y WITH DIAERESIS> と大文字小文字を無視した |
614 | 1528 | マッチングをしません; なぜなら 0xFF は現在のロケールでは |
615 | 1529 | C<LATIN SMALL LETTER Y WITH DIAERESIS> ではないかもしれず、Perl は |
616 | 1530 | このロケールでこの文字があるかどうかすら知る方法がなく、ましてや |
617 | 1531 | これがどの符号位置かを知る方法もないからです。 |
618 | 1532 | |
619 | 1533 | =begin original |
620 | 1534 | |
1535 | In a UTF-8 locale in v5.20 and later, the only visible difference | |
1536 | between locale and non-locale in regular expressions should be tainting | |
1537 | (see L<perlsec>). | |
1538 | ||
1539 | =end original | |
1540 | ||
1541 | v5.20 以降の UTF-8 ロケールでは、 | |
1542 | 正規表現でのロケールと非ロケールの目に見える唯一の違いは | |
1543 | 汚染だけのはずです | |
1544 | (L<perlsec> 参照)。 | |
1545 | ||
1546 | =begin original | |
1547 | ||
621 | 1548 | This modifier may be specified to be the default by C<use locale>, but |
622 | 1549 | see L</Which character set modifier is in effect?>. |
623 | 1550 | X</l> |
624 | 1551 | |
625 | 1552 | =end original |
626 | 1553 | |
627 | 1554 | この修飾子は C<use locale> によってデフォルトで設定されますが、 |
628 | 1555 | L</Which character set modifier is in effect?> を参照してください。 |
629 | 1556 | X</l> |
630 | 1557 | |
631 | 1558 | =head4 /u |
632 | 1559 | |
633 | 1560 | =begin original |
634 | 1561 | |
635 | 1562 | means to use Unicode rules when pattern matching. On ASCII platforms, |
636 | 1563 | this means that the code points between 128 and 255 take on their |
637 | 1564 | Latin-1 (ISO-8859-1) meanings (which are the same as Unicode's). |
638 | 1565 | (Otherwise Perl considers their meanings to be undefined.) Thus, |
639 | 1566 | under this modifier, the ASCII platform effectively becomes a Unicode |
640 | 1567 | platform; and hence, for example, C<\w> will match any of the more than |
641 | 1568 | 100_000 word characters in Unicode. |
642 | 1569 | |
643 | 1570 | =end original |
644 | 1571 | |
645 | 1572 | これはパターンマッチングのときに Unicode の規則を使うことを意味します。 |
646 | 1573 | ASCII プラットフォームでは、これは符号位置 128 から 255 は |
647 | 1574 | Latin-1 (ISO-8859-1) という意味になります (これは Unicode と同じです)。 |
648 | 1575 | (さもなければ Perl はこれらの意味は未定義として扱います。) |
649 | 1576 | 従って、この修飾子の基では、ASCII プラットフォームは |
650 | 1577 | 事実上 Unicode プラットフォームになります; 従って、 |
651 | 1578 | 例えば、C<\w> は Unicode の 100,000 以上の単語文字のどれにも |
652 | 1579 | マッチングします。 |
653 | 1580 | |
654 | 1581 | =begin original |
655 | 1582 | |
656 | 1583 | Unlike most locales, which are specific to a language and country pair, |
657 | 1584 | Unicode classifies all the characters that are letters I<somewhere> in |
658 | 1585 | the world as |
659 | 1586 | C<\w>. For example, your locale might not think that C<LATIN SMALL |
660 | 1587 | LETTER ETH> is a letter (unless you happen to speak Icelandic), but |
661 | 1588 | Unicode does. Similarly, all the characters that are decimal digits |
662 | 1589 | somewhere in the world will match C<\d>; this is hundreds, not 10, |
663 | 1590 | possible matches. And some of those digits look like some of the 10 |
664 | 1591 | ASCII digits, but mean a different number, so a human could easily think |
665 | 1592 | a number is a different quantity than it really is. For example, |
666 | 1593 | C<BENGALI DIGIT FOUR> (U+09EA) looks very much like an |
667 | 1594 | C<ASCII DIGIT EIGHT> (U+0038). And, C<\d+>, may match strings of digits |
668 | 1595 | that are a mixture from different writing systems, creating a security |
669 | 1596 | issue. L<Unicode::UCD/num()> can be used to sort |
670 | 1597 | this out. Or the C</a> modifier can be used to force C<\d> to match |
671 | 1598 | just the ASCII 0 through 9. |
672 | 1599 | |
673 | 1600 | =end original |
674 | 1601 | |
675 | 1602 | 特定の言語と国に固有であるほとんどのロケールと異なり、 |
676 | 1603 | Unicode は世界の I<どこか> で字(letter)として扱われている全ての |
677 | 1604 | 文字(character)を C<\w> に分類します。 |
678 | 1605 | 例えば、あなたのロケールは (あなたがたまたまアイスランド語を話さない限り) |
679 | 1606 | C<LATIN SMALL LETTER ETH> を字として考えないかもしれません。 |
680 | 1607 | 同様に、世界のどこかで数字である全ての文字は C<\d> にマッチングします; |
681 | 1608 | これは 10 ではなく 100 のマッチングの可能性があります。 |
682 | 1609 | さらにこれらの数字の一部は 10 の ASCII 数字と似ていますが、 |
683 | 1610 | 異なる数字を意味するため、人間はその数字が実際と異なる量であると |
684 | 1611 | 簡単に考えてしまいます。 |
685 | 1612 | 例えば、 C<BENGALI DIGIT FOUR> (U+09EA) は C<ASCII DIGIT EIGHT> (U+0038) に |
686 | 1613 | とてもよく似ています。 |
687 | 1614 | C<\d+> は、異なる記法から混ぜた数字の文字列にマッチングするので、 |
688 | 1615 | セキュリティ上の問題を作ります。 |
689 | 1616 | これを整理するために L<Unicode::UCD/num()> が使われます。 |
690 | 1617 | あるいは C</a> 修飾子は、C<\d> が単に ASCII の 0 から 9 に |
691 | 1618 | マッチングすることを強制するために使えます。 |
692 | 1619 | |
693 | 1620 | =begin original |
694 | 1621 | |
695 | 1622 | Also, under this modifier, case-insensitive matching works on the full |
696 | 1623 | set of Unicode |
697 | 1624 | characters. The C<KELVIN SIGN>, for example matches the letters "k" and |
698 | 1625 | "K"; and C<LATIN SMALL LIGATURE FF> matches the sequence "ff", which, |
699 | 1626 | if you're not prepared, might make it look like a hexadecimal constant, |
700 | 1627 | presenting another potential security issue. See |
701 | 1628 | L<http://unicode.org/reports/tr36> for a detailed discussion of Unicode |
702 | 1629 | security issues. |
703 | 1630 | |
704 | 1631 | =end original |
705 | 1632 | |
706 | 1633 | また、この修飾子の基では、大文字小文字を無視したマッチングは Unicode の |
707 | 1634 | 完全な集合で動作します。 |
708 | 1635 | 例えば C<KELVIN SIGN> は "k" と "K" にマッチングします; |
709 | 1636 | C<LATIN SMALL LIGATURE FF> は、準備していなければ 16 進数定数のように |
710 | 1637 | 見えるかもしれない並び "ff" にマッチングし、もう一つの潜在的な |
711 | 1638 | セキュリティ問題になります。 |
712 | 1639 | Unicode のセキュリティ問題に関する詳細な議論については |
713 | 1640 | L<http://unicode.org/reports/tr36> を参照してください。 |
714 | 1641 | |
715 | 1642 | =begin original |
716 | 1643 | |
717 | On the EBCDIC platforms that Perl handles, the native character set is | |
718 | equivalent to Latin-1. Thus this modifier changes behavior only when | |
719 | the C<"/i"> modifier is also specified, and it turns out it affects only | |
720 | two characters, giving them full Unicode semantics: the C<MICRO SIGN> | |
721 | will match the Greek capital and small letters C<MU>, otherwise not; and | |
722 | the C<LATIN CAPITAL LETTER SHARP S> will match any of C<SS>, C<Ss>, | |
723 | C<sS>, and C<ss>, otherwise not. | |
724 | ||
725 | =end original | |
726 | ||
727 | Perl が扱える EBCDIC プラットフォームでは、ネイティブな文字集合は | |
728 | Latin-1 と等価です。 | |
729 | 従ってこの修飾子は、C<"/i"> 修飾子も指定されたときにのみ | |
730 | 振る舞いを変え、結果として二つの文字にだけ影響を与え、 | |
731 | 完全な Unicode の意味論を与えます: | |
732 | C<MICRO SIGN> はギリシャ語の大文字と小文字の C<MU> にマッチングし、 | |
733 | それ以外はマッチングしません; | |
734 | また C<LATIN CAPITAL LETTER SHARP S> は C<SS>, C<Ss>, | |
735 | C<sS>, C<ss> のいずれかにはマッチングし、それ以外にはマッチングしません。 | |
736 | ||
737 | =begin original | |
738 | ||
739 | 1644 | This modifier may be specified to be the default by C<use feature |
740 | 1645 | 'unicode_strings>, C<use locale ':not_characters'>, or |
741 | 1646 | C<L<use 5.012|perlfunc/use VERSION>> (or higher), |
742 | 1647 | but see L</Which character set modifier is in effect?>. |
743 | 1648 | X</u> |
744 | 1649 | |
745 | 1650 | =end original |
746 | 1651 | |
747 | 1652 | この修飾子は C<use feature 'unicode_strings>, |
748 | 1653 | C<use locale ':not_characters'>, C<L<use 5.012|perlfunc/use VERSION>> |
749 | 1654 | (またはそれ以上) によってデフォルトに |
750 | 1655 | 設定されますが、L</Which character set modifier is in effect?> を |
751 | 1656 | 参照してください。 |
752 | 1657 | X</u> |
753 | 1658 | |
754 | 1659 | =head4 /d |
755 | 1660 | |
756 | 1661 | =begin original |
757 | 1662 | |
758 | 1663 | This modifier means to use the "Default" native rules of the platform |
759 | 1664 | except when there is cause to use Unicode rules instead, as follows: |
760 | 1665 | |
761 | 1666 | =end original |
762 | 1667 | |
763 | 1668 | この修飾子は、以下のように Unicode の規則が使われる場合を除いて、 |
764 | 1669 | プラットフォームの「デフォルトの」(Default) ネイティブな規則を使うことを |
765 | 1670 | 意味します: |
766 | 1671 | |
767 | 1672 | =over 4 |
768 | 1673 | |
769 | 1674 | =item 1 |
770 | 1675 | |
771 | 1676 | =begin original |
772 | 1677 | |
773 | 1678 | the target string is encoded in UTF-8; or |
774 | 1679 | |
775 | 1680 | =end original |
776 | 1681 | |
777 | 1682 | ターゲット文字列が UTF-8 でエンコードされている; または |
778 | 1683 | |
779 | 1684 | =item 2 |
780 | 1685 | |
781 | 1686 | =begin original |
782 | 1687 | |
783 | 1688 | the pattern is encoded in UTF-8; or |
784 | 1689 | |
785 | 1690 | =end original |
786 | 1691 | |
787 | 1692 | パターンが UTF-8 でエンコードされている; または |
788 | 1693 | |
789 | 1694 | =item 3 |
790 | 1695 | |
791 | 1696 | =begin original |
792 | 1697 | |
793 | 1698 | the pattern explicitly mentions a code point that is above 255 (say by |
794 | 1699 | C<\x{100}>); or |
795 | 1700 | |
796 | 1701 | =end original |
797 | 1702 | |
798 | 1703 | パターンが、(C<\x{100}> のような形で)255 を超える符号位置に明示的に |
799 | 1704 | 言及している; または |
800 | 1705 | |
801 | 1706 | =item 4 |
802 | 1707 | |
803 | 1708 | =begin original |
804 | 1709 | |
805 | 1710 | the pattern uses a Unicode name (C<\N{...}>); or |
806 | 1711 | |
807 | 1712 | =end original |
808 | 1713 | |
809 | 1714 | パターンが Unicode 名 (C<\N{...}>) を使っている; または |
810 | 1715 | |
811 | 1716 | =item 5 |
812 | 1717 | |
813 | 1718 | =begin original |
814 | 1719 | |
815 | the pattern uses a Unicode property (C<\p{...}>) | |
1720 | the pattern uses a Unicode property (C<\p{...}> or C<\P{...}>); or | |
816 | 1721 | |
817 | 1722 | =end original |
818 | 1723 | |
819 | パターンが Unicode 特性 (C<\p{...}>) を使っている | |
1724 | パターンが Unicode 特性 (C<\p{...}>) や C<\P{...}>) を使っている; または | |
820 | 1725 | |
1726 | =item 6 | |
1727 | ||
1728 | =begin original | |
1729 | ||
1730 | the pattern uses a Unicode break (C<\b{...}> or C<\B{...}>); or | |
1731 | ||
1732 | =end original | |
1733 | ||
1734 | パターンが Unicode 単語境界 (C<\b{...}> または C<\B{...}>) を使っている; | |
1735 | または | |
1736 | ||
1737 | =item 7 | |
1738 | ||
1739 | =begin original | |
1740 | ||
1741 | the pattern uses L</C<(?[ ])>> | |
1742 | ||
1743 | =end original | |
1744 | ||
1745 | パターンが L</C<(?[ ])>> を使っている | |
1746 | ||
1747 | =item 8 | |
1748 | ||
1749 | =begin original | |
1750 | ||
1751 | the pattern uses L<C<(*script_run: ...)>|/Script Runs> | |
1752 | ||
1753 | =end original | |
1754 | ||
1755 | パターンが L<C<(*script_run: ...)>|/Script Runs> を使っている | |
1756 | ||
821 | 1757 | =back |
822 | 1758 | |
823 | 1759 | =begin original |
824 | 1760 | |
825 | 1761 | Another mnemonic for this modifier is "Depends", as the rules actually |
826 | 1762 | used depend on various things, and as a result you can get unexpected |
827 | 1763 | results. See L<perlunicode/The "Unicode Bug">. The Unicode Bug has |
828 | 1764 | become rather infamous, leading to yet another (printable) name for this |
829 | 1765 | modifier, "Dodgy". |
830 | 1766 | |
831 | 1767 | =end original |
832 | 1768 | |
833 | 1769 | この修飾子のもう一つの記憶法は「依存」(Depends)です; 規則は実際には |
834 | 1770 | 様々なことに依存していること、また結果として予想外の |
835 | 1771 | 結果になるかもしれないからです。 |
836 | 1772 | L<perlunicode/The "Unicode Bug"> を参照してください。 |
837 | 1773 | Unicode バグは、悪名高くなり、この修飾子のもう一つの(表示可能な) |
838 | 1774 | 名前 "Dodgy" を引き起こしています。 |
839 | 1775 | |
840 | 1776 | =begin original |
841 | 1777 | |
842 | ||
1778 | Unless the pattern or string are encoded in UTF-8, only ASCII characters | |
843 | ||
1779 | can match positively. | |
844 | 1780 | |
845 | 1781 | =end original |
846 | 1782 | |
847 | ||
1783 | パターンや文字列が UTF-8 でエンコードされていない限り、 | |
848 | ||
1784 | ASCII 文字のみが肯定的にマッチングします。 | |
849 | 1785 | |
850 | 1786 | =begin original |
851 | 1787 | |
852 | 1788 | Here are some examples of how that works on an ASCII platform: |
853 | 1789 | |
854 | 1790 | =end original |
855 | 1791 | |
856 | 1792 | 以下は ASCII プラットフォームでどのように動作するかの例です: |
857 | 1793 | |
858 | 1794 | $str = "\xDF"; # $str is not in UTF-8 format. |
859 | 1795 | $str =~ /^\w/; # No match, as $str isn't in UTF-8 format. |
860 | 1796 | $str .= "\x{0e0b}"; # Now $str is in UTF-8 format. |
861 | 1797 | $str =~ /^\w/; # Match! $str is now in UTF-8 format. |
862 | 1798 | chop $str; |
863 | 1799 | $str =~ /^\w/; # Still a match! $str remains in UTF-8 format. |
864 | 1800 | |
865 | 1801 | =begin original |
866 | 1802 | |
867 | 1803 | This modifier is automatically selected by default when none of the |
868 | 1804 | others are, so yet another name for it is "Default". |
869 | 1805 | |
870 | 1806 | =end original |
871 | 1807 | |
872 | 1808 | この修飾子は他のものが指定されなかった場合にデフォルトとして自動的に |
873 | 1809 | 洗濯されるので、これのもう一つの名前は "Default" です。 |
874 | 1810 | |
875 | 1811 | =begin original |
876 | 1812 | |
877 | 1813 | Because of the unexpected behaviors associated with this modifier, you |
878 | probably should only use it to maintain weird backward | |
1814 | probably should only explicitly use it to maintain weird backward | |
1815 | compatibilities. | |
879 | 1816 | |
880 | 1817 | =end original |
881 | 1818 | |
882 | 1819 | この修飾子に関する想定外の振る舞いにより、おそらくおかしな後方互換性を |
883 | 維持するためだけにこれを使うべきでしょう。 | |
1820 | 維持するためだけにこれを明示的に使うべきでしょう。 | |
884 | 1821 | |
885 | 1822 | =head4 /a (and /aa) |
886 | 1823 | |
887 | 1824 | (/a (と /aa)) |
888 | 1825 | |
889 | 1826 | =begin original |
890 | 1827 | |
891 | This modifier stands for ASCII-restrict (or ASCII-safe). This modifier | |
1828 | This modifier stands for ASCII-restrict (or ASCII-safe). This modifier | |
892 | ||
1829 | may be doubled-up to increase its effect. | |
893 | 1830 | |
894 | 1831 | =end original |
895 | 1832 | |
896 | 1833 | この修飾子は ASCII 制限 (あるいは ASCII セーフ) を意味します。 |
897 | この修飾子は、 | |
1834 | この修飾子は、2 倍にすることで効果が増します。 | |
898 | 1835 | |
899 | 1836 | =begin original |
900 | 1837 | |
901 | 1838 | When it appears singly, it causes the sequences C<\d>, C<\s>, C<\w>, and |
902 | 1839 | the Posix character classes to match only in the ASCII range. They thus |
903 | 1840 | revert to their pre-5.6, pre-Unicode meanings. Under C</a>, C<\d> |
904 | 1841 | always means precisely the digits C<"0"> to C<"9">; C<\s> means the five |
905 | characters C<[ \f\n\r\t]> | |
1842 | characters C<[ \f\n\r\t]>, and starting in Perl v5.18, the vertical tab; | |
1843 | C<\w> means the 63 characters | |
906 | 1844 | C<[A-Za-z0-9_]>; and likewise, all the Posix classes such as |
907 | 1845 | C<[[:print:]]> match only the appropriate ASCII-range characters. |
908 | 1846 | |
909 | 1847 | =end original |
910 | 1848 | |
911 | 1849 | これが単体で使われると、C<\d>, C<\s>, C<\w>, Posix 文字クラスは |
912 | 1850 | ASCII の範囲のみにマッチングするようになります。 |
913 | 1851 | 従って、これらは 5.6 以前の、Unicode 以前の意味に戻します。 |
914 | 1852 | C</a> の基では、C<\d> は常に正確に数字 C<"0"> から C<"9"> を意味します; |
915 | C<\s> は C<[ \f\n\r\t]> の 5 文字 | |
1853 | C<\s> は C<[ \f\n\r\t]> の 5 文字、および Perl v5.18 から垂直タブ、 | |
1854 | を意味します; | |
916 | 1855 | C<\w> は C<[A-Za-z0-9_]> の 63 文字を意味します; |
917 | 1856 | 同様に、C<[[:print:]]> のような全ての Posix クラスは |
918 | 1857 | 適切な ASCII の範囲の文字にのみマッチングします。 |
919 | 1858 | |
920 | 1859 | =begin original |
921 | 1860 | |
922 | 1861 | This modifier is useful for people who only incidentally use Unicode, |
923 | 1862 | and who do not wish to be burdened with its complexities and security |
924 | 1863 | concerns. |
925 | 1864 | |
926 | 1865 | =end original |
927 | 1866 | |
928 | 1867 | この修飾子は、偶然 Unicode を使っている人々で、その複雑さと |
929 | 1868 | セキュリティの問題に関する重荷を背負いたくない人々にとっては有用です。 |
930 | 1869 | |
931 | 1870 | =begin original |
932 | 1871 | |
933 | 1872 | With C</a>, one can write C<\d> with confidence that it will only match |
934 | 1873 | ASCII characters, and should the need arise to match beyond ASCII, you |
935 | 1874 | can instead use C<\p{Digit}> (or C<\p{Word}> for C<\w>). There are |
936 | 1875 | similar C<\p{...}> constructs that can match beyond ASCII both white |
937 | 1876 | space (see L<perlrecharclass/Whitespace>), and Posix classes (see |
938 | 1877 | L<perlrecharclass/POSIX Character Classes>). Thus, this modifier |
939 | 1878 | doesn't mean you can't use Unicode, it means that to get Unicode |
940 | 1879 | matching you must explicitly use a construct (C<\p{}>, C<\P{}>) that |
941 | 1880 | signals Unicode. |
942 | 1881 | |
943 | 1882 | =end original |
944 | 1883 | |
945 | 1884 | C</a> を使うと、ASCII 文字だけにマッチングすることに自信を持って |
946 | 1885 | 書くことができ、ASCII を超えてマッチングする必要が発生したときには、 |
947 | 1886 | 代わりに C<\p{Digit}> (または C<\w> として C<\p{Word}>) が使えます。 |
948 | 1887 | ASCII を超えたスペース (L<perlrecharclass/Whitespace> を参照してください) |
949 | 1888 | と Posix クラス (L<perlrecharclass/POSIX Character Classes> を |
950 | 1889 | 参照してください) の両方にマッチングする似たような C<\p{...}> 構文があります。 |
951 | 1890 | 従って、この修飾子は Unicode が使えなくなるということではなく、 |
952 | 1891 | Unicode のマッチングには明示的に Unicode を意味する構文 |
953 | 1892 | (C<\p{}>, C<\P{}>) を使わないといけないということです。 |
954 | 1893 | |
955 | 1894 | =begin original |
956 | 1895 | |
957 | 1896 | As you would expect, this modifier causes, for example, C<\D> to mean |
958 | 1897 | the same thing as C<[^0-9]>; in fact, all non-ASCII characters match |
959 | 1898 | C<\D>, C<\S>, and C<\W>. C<\b> still means to match at the boundary |
960 | 1899 | between C<\w> and C<\W>, using the C</a> definitions of them (similarly |
961 | 1900 | for C<\B>). |
962 | 1901 | |
963 | 1902 | =end original |
964 | 1903 | |
965 | 1904 | 予想できるとおり、 この修飾子は、例えば、C<\D> を C<[^0-9]> と |
966 | 1905 | 同じことにします; |
967 | 1906 | 実際、全ての非 ASCII 文字は C<\D>, C<\S>, C<\W> にマッチングします。 |
968 | 1907 | C<\b> はまだ C<\w> と C<\W> の境界にマッチングします; |
969 | 1908 | これらのために (C<\B> と同様) C</a> の定義を使います。 |
970 | 1909 | |
971 | 1910 | =begin original |
972 | 1911 | |
973 | 1912 | Otherwise, C</a> behaves like the C</u> modifier, in that |
974 | case-insensitive matching uses Unicode | |
1913 | case-insensitive matching uses Unicode rules; for example, "k" will | |
975 | 1914 | match the Unicode C<\N{KELVIN SIGN}> under C</i> matching, and code |
976 | 1915 | points in the Latin1 range, above ASCII will have Unicode rules when it |
977 | 1916 | comes to case-insensitive matching. |
978 | 1917 | |
979 | 1918 | =end original |
980 | 1919 | |
981 | 1920 | さもなければ、C</a> は C</u> 修飾子のように振る舞います; |
982 | 大文字小文字を無視したマッチングには Unicode の | |
1921 | 大文字小文字を無視したマッチングには Unicode の規則を使います; | |
983 | 1922 | 例えば、"k" は C</i> の基では C<\N{KELVIN SIGN}> にマッチングし、 |
984 | 1923 | ASCII の範囲を超える Latin1 の範囲の符号位置は、大文字小文字を無視した |
985 | 1924 | マッチングで使われる場合は Unicode の規則を使います。 |
986 | 1925 | |
987 | 1926 | =begin original |
988 | 1927 | |
989 | 1928 | To forbid ASCII/non-ASCII matches (like "k" with C<\N{KELVIN SIGN}>), |
990 | specify the "a" twice, for example C</aai> or C</aia>. (The first | |
1929 | specify the C<"a"> twice, for example C</aai> or C</aia>. (The first | |
991 | occurrence of "a" restricts the C<\d>, etc., and the second occurrence | |
1930 | occurrence of C<"a"> restricts the C<\d>, I<etc>., and the second occurrence | |
992 | 1931 | adds the C</i> restrictions.) But, note that code points outside the |
993 | 1932 | ASCII range will use Unicode rules for C</i> matching, so the modifier |
994 | 1933 | doesn't really restrict things to just ASCII; it just forbids the |
995 | 1934 | intermixing of ASCII and non-ASCII. |
996 | 1935 | |
997 | 1936 | =end original |
998 | 1937 | |
999 | 1938 | ("k" と C<\N{KELVIN SIGN}> のような) ASCII/非-ASCII マッチングを禁止するには、 |
1000 | C</aai> や C</aia> のように "a" を 2 回指定します。 | |
1939 | C</aai> や C</aia> のように C<"a"> を 2 回指定します。 | |
1001 | (最初の "a" は C<\d> などを制限し、2 番目は C</i> の制限を追加します。) | |
1940 | (最初の C<"a"> は C<\d> などを制限し、2 番目は C</i> の制限を追加します。) | |
1002 | 1941 | しかし、 ASCII の範囲外の符号位置は C</i> マッチングに Unicode 規則を |
1003 | 1942 | 使うので、この修飾子は実際には単に ASCII に制限するものではないことに |
1004 | 1943 | 注意してください; これは単に ASCII と非 ASCII を混ぜることを禁止します。 |
1005 | 1944 | |
1006 | 1945 | =begin original |
1007 | 1946 | |
1008 | 1947 | To summarize, this modifier provides protection for applications that |
1009 | 1948 | don't wish to be exposed to all of Unicode. Specifying it twice |
1010 | 1949 | gives added protection. |
1011 | 1950 | |
1012 | 1951 | =end original |
1013 | 1952 | |
1014 | 1953 | まとめると、この修飾子は全ての Unicode に対して曝されることを望んでいない |
1015 | 1954 | アプリケーションに対する保護を提供します。 |
1016 | 1955 | 2 回指定することで追加の保護を提供します。 |
1017 | 1956 | |
1018 | 1957 | =begin original |
1019 | 1958 | |
1020 | 1959 | This modifier may be specified to be the default by C<use re '/a'> |
1021 | 1960 | or C<use re '/aa'>. If you do so, you may actually have occasion to use |
1022 | the C</u> modifier explictly if there are a few regular expressions | |
1961 | the C</u> modifier explicitly if there are a few regular expressions | |
1023 | 1962 | where you do want full Unicode rules (but even here, it's best if |
1024 | 1963 | everything were under feature C<"unicode_strings">, along with the |
1025 | 1964 | C<use re '/aa'>). Also see L</Which character set modifier is in |
1026 | 1965 | effect?>. |
1027 | 1966 | X</a> |
1028 | 1967 | X</aa> |
1029 | 1968 | |
1030 | 1969 | =end original |
1031 | 1970 | |
1032 | 1971 | この修飾子は C<use re '/a'> または C<use re '/aa'> でデフォルトに |
1033 | 1972 | 設定されます。 |
1034 | 1973 | そうすると、もし完全な Unicode 規則を使いたい正規表現がある場合は、 |
1035 | 1974 | C</u> 修飾子を明示的に使う機会があるかもしれません |
1036 | 1975 | (その場合でも、全てが C<"unicode_strings"> の基なら、 |
1037 | 1976 | C<use re '/aa'> と共にするのが最良です)。 |
1038 | 1977 | L</Which character set modifier is in effect?> も参照してください。 |
1039 | 1978 | X</a> |
1040 | 1979 | X</aa> |
1041 | 1980 | |
1042 | 1981 | =head4 Which character set modifier is in effect? |
1043 | 1982 | |
1044 | 1983 | (どの文字集合修飾子が有効?) |
1045 | 1984 | |
1046 | 1985 | =begin original |
1047 | 1986 | |
1048 | 1987 | Which of these modifiers is in effect at any given point in a regular |
1049 | 1988 | expression depends on a fairly complex set of interactions. These have |
1050 | 1989 | been designed so that in general you don't have to worry about it, but |
1051 | 1990 | this section gives the gory details. As |
1052 | 1991 | explained below in L</Extended Patterns> it is possible to explicitly |
1053 | 1992 | specify modifiers that apply only to portions of a regular expression. |
1054 | 1993 | The innermost always has priority over any outer ones, and one applying |
1055 | 1994 | to the whole expression has priority over any of the default settings that are |
1056 | 1995 | described in the remainder of this section. |
1057 | 1996 | |
1058 | 1997 | =end original |
1059 | 1998 | |
1060 | 1999 | ある正規表現のあるポイントでどの修飾子が有効かは、かなり複雑な相互作用に |
1061 | 2000 | 依存します。 |
1062 | 2001 | これらは、基本的にはあなたがこれらについて心配しなくて良いように |
1063 | 2002 | 設計されています。 |
1064 | 2003 | しかし、この節は詳細を記述します。 |
1065 | 2004 | L</Extended Patterns> で後述するとおり、正規表現の一部にだけ |
1066 | 2005 | 適用する修飾子を明示的に指定することが可能です。 |
1067 | 2006 | 一番内側のものは常により外側のものより優先され、式全体に適用されるものは |
1068 | 2007 | この節の残りで記述されるデフォルト設定より優先されます。 |
1069 | 2008 | |
1070 | 2009 | =begin original |
1071 | 2010 | |
1072 | 2011 | The C<L<use re 'E<sol>foo'|re/"'/flags' mode">> pragma can be used to set |
1073 | 2012 | default modifiers (including these) for regular expressions compiled |
1074 | 2013 | within its scope. This pragma has precedence over the other pragmas |
1075 | 2014 | listed below that also change the defaults. |
1076 | 2015 | |
1077 | 2016 | =end original |
1078 | 2017 | |
1079 | 2018 | C<L<use re 'E<sol>foo'|re/"'/flags' mode">> プラグマは、 |
1080 | 2019 | このスコープ内でコンパイルされる正規表現に対して(これらを含む) |
1081 | 2020 | デフォルトの修飾子を設定するのに使えます。 |
1082 | 2021 | このプラグマは、デフォルトを変更する後述するその他のプラグマに優先します。 |
1083 | 2022 | |
1084 | 2023 | =begin original |
1085 | 2024 | |
1086 | 2025 | Otherwise, C<L<use locale|perllocale>> sets the default modifier to C</l>; |
1087 | 2026 | and C<L<use feature 'unicode_strings|feature>>, or |
1088 | 2027 | C<L<use 5.012|perlfunc/use VERSION>> (or higher) set the default to |
1089 | 2028 | C</u> when not in the same scope as either C<L<use locale|perllocale>> |
1090 | 2029 | or C<L<use bytes|bytes>>. |
1091 | 2030 | (C<L<use locale ':not_characters'|perllocale/Unicode and UTF-8>> also |
1092 | 2031 | sets the default to C</u>, overriding any plain C<use locale>.) |
1093 | 2032 | Unlike the mechanisms mentioned above, these |
1094 | 2033 | affect operations besides regular expressions pattern matching, and so |
1095 | 2034 | give more consistent results with other operators, including using |
1096 | C<\U>, C<\l>, etc. in substitution replacements. | |
2035 | C<\U>, C<\l>, I<etc>. in substitution replacements. | |
1097 | 2036 | |
1098 | 2037 | =end original |
1099 | 2038 | |
1100 | 2039 | さもなければ、C<L<use locale|perllocale>> はデフォルト修飾子を C</l> に |
1101 | 2040 | 設定します; そして、C<L<use feature 'unicode_strings|feature>> か |
1102 | 2041 | C<L<use 5.012|perlfunc/use VERSION>> (またはそれ以上) は、 |
1103 | 2042 | 同じスコープに C<L<use locale|perllocale>> や C<L<use bytes|bytes>> が |
1104 | 2043 | なければ、デフォルトを C</u> に設定します。 |
1105 | 2044 | (C<L<use locale ':not_characters'|perllocale/Unicode and UTF-8>> はまた |
1106 | 2045 | デフォルトを C</u> に設定し、普通の C<use locale> を上書きします。) |
1107 | 2046 | 前述した機構と異なり、これらは正規表現パターンマッチング以外の操作に |
1108 | 2047 | 影響するので、置換での C<\U>, C<\l> を使うことを含むその他の操作と |
1109 | 2048 | より一貫性のある結果になります。 |
1110 | 2049 | |
1111 | 2050 | =begin original |
1112 | 2051 | |
1113 | 2052 | If none of the above apply, for backwards compatibility reasons, the |
1114 | 2053 | C</d> modifier is the one in effect by default. As this can lead to |
1115 | 2054 | unexpected results, it is best to specify which other rule set should be |
1116 | 2055 | used. |
1117 | 2056 | |
1118 | 2057 | =end original |
1119 | 2058 | |
1120 | 2059 | 前述のどれも適用されない場合、後方互換性のために、 |
1121 | 2060 | C</d> 修飾子がデフォルトで有効になります。 |
1122 | 2061 | これは想定外の結果になることがあるので、 |
1123 | 2062 | その他の規則集合が使われるように指定するのが最良です。 |
1124 | 2063 | |
1125 | 2064 | =head4 Character set modifier behavior prior to Perl 5.14 |
1126 | 2065 | |
1127 | 2066 | (Perl 5.14 より前の文字集合修飾子の振る舞い) |
1128 | 2067 | |
1129 | 2068 | =begin original |
1130 | 2069 | |
1131 | 2070 | Prior to 5.14, there were no explicit modifiers, but C</l> was implied |
1132 | 2071 | for regexes compiled within the scope of C<use locale>, and C</d> was |
1133 | 2072 | implied otherwise. However, interpolating a regex into a larger regex |
1134 | 2073 | would ignore the original compilation in favor of whatever was in effect |
1135 | 2074 | at the time of the second compilation. There were a number of |
1136 | 2075 | inconsistencies (bugs) with the C</d> modifier, where Unicode rules |
1137 | 2076 | would be used when inappropriate, and vice versa. C<\p{}> did not imply |
1138 | 2077 | Unicode rules, and neither did all occurrences of C<\N{}>, until 5.12. |
1139 | 2078 | |
1140 | 2079 | =end original |
1141 | 2080 | |
1142 | 2081 | 5.14 より前では、明示的な修飾子はありませんが、 |
1143 | 2082 | C<use locale> のスコープ内でコンパイルされた正規表現に関しては |
1144 | 2083 | C</l> が仮定され、さもなければ C</d> が仮定されます。 |
1145 | 2084 | しかし、ある正規表現をより大きな正規表現に展開した場合、元のコンパイル時の |
1146 | 2085 | 状況は、2 回目のコンパイル時点で有効なもので上書きされます。 |
1147 | 2086 | C</d> 演算子には、不適切なときに Unicode 規則が使われる、あるいはその逆の |
1148 | 2087 | 多くの非一貫性(バグ)があります。 |
1149 | 2088 | C<\p{}> および C<\N{}> は 5.12 まで Unicode 規則を仮定していません。 |
1150 | 2089 | |
1151 | 2090 | =head2 Regular Expressions |
1152 | 2091 | |
1153 | 2092 | (正規表現) |
1154 | 2093 | |
1155 | =head3 Metacharacters | |
1156 | ||
1157 | (メタ文字) | |
1158 | ||
1159 | =begin original | |
1160 | ||
1161 | The patterns used in Perl pattern matching evolved from those supplied in | |
1162 | the Version 8 regex routines. (The routines are derived | |
1163 | (distantly) from Henry Spencer's freely redistributable reimplementation | |
1164 | of the V8 routines.) See L<Version 8 Regular Expressions> for | |
1165 | details. | |
1166 | ||
1167 | =end original | |
1168 | ||
1169 | Perl のパターンマッチングで使われるパターンは Version 8 正規表現ルーチンで | |
1170 | 提供されているものからの派生です。 | |
1171 | (このルーチンは Henry Spencer の自由に再配布可能な V8 ルーチンの再実装から | |
1172 | (遠く)派生しています)。 | |
1173 | 詳細は L<Version 8 Regular Expressions> を参照してください。 | |
1174 | ||
1175 | =begin original | |
1176 | ||
1177 | In particular the following metacharacters have their standard I<egrep>-ish | |
1178 | meanings: | |
1179 | X<metacharacter> | |
1180 | X<\> X<^> X<.> X<$> X<|> X<(> X<()> X<[> X<[]> | |
1181 | ||
1182 | =end original | |
1183 | ||
1184 | 特に以下のメタ文字は標準の I<egrep> 風の意味を持っています: | |
1185 | X<metacharacter> | |
1186 | X<\> 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 | ||
1210 | By default, the "^" character is guaranteed to match only the | |
1211 | beginning of the string, the "$" character only the end (or before the | |
1212 | newline at the end), and Perl does certain optimizations with the | |
1213 | assumption that the string contains only one line. Embedded newlines | |
1214 | will not be matched by "^" or "$". You may, however, wish to treat a | |
1215 | string as a multi-line buffer, such that the "^" will match after any | |
1216 | newline within the string (except if the newline is the last character in | |
1217 | the string), and "$" will match before any newline. At the | |
1218 | cost of a little more overhead, you can do this by using the /m modifier | |
1219 | on the pattern match operator. (Older programs did this by setting C<$*>, | |
1220 | but this option was removed in perl 5.9.) | |
1221 | X<^> 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 では削除されています。) | |
1236 | X<^> X<$> X</m> | |
1237 | ||
1238 | =begin original | |
1239 | ||
1240 | To simplify multi-line substitutions, the "." character never matches a | |
1241 | newline unless you use the C</s> modifier, which in effect tells Perl to pretend | |
1242 | the string is a single line--even if it isn't. | |
1243 | X<.> X</s> | |
1244 | ||
1245 | =end original | |
1246 | ||
1247 | 複数行での利用を簡単にするために、文字 "." は C</s> 修飾子を | |
1248 | 使って Perl に文字列を 1 行として処理すると伝えない限り | |
1249 | 改行にはマッチングしません。 | |
1250 | X<.> X</s> | |
1251 | ||
1252 | 2094 | =head3 Quantifiers |
1253 | 2095 | |
1254 | 2096 | (量指定子) |
1255 | 2097 | |
1256 | 2098 | =begin original |
1257 | 2099 | |
1258 | ||
2100 | Quantifiers are used when a particular portion of a pattern needs to | |
2101 | match a certain number (or numbers) of times. If there isn't a | |
2102 | quantifier the number of times to match is exactly one. The following | |
2103 | standard quantifiers are recognized: | |
1259 | 2104 | X<metacharacter> X<quantifier> X<*> X<+> X<?> X<{n}> X<{n,}> X<{n,m}> |
1260 | 2105 | |
1261 | 2106 | =end original |
1262 | 2107 | |
2108 | Quantifiers are used when a particular portion of a pattern needs to | |
2109 | match a certain number (or numbers) of times. If there isn't a | |
2110 | quantifier the number of times to match is exactly one. | |
1263 | 2111 | 以下の標準的な量指定子を使えます: |
1264 | 2112 | X<metacharacter> X<quantifier> X<*> X<+> X<?> X<{n}> X<{n,}> X<{n,m}> |
1265 | 2113 | |
1266 | 2114 | =begin original |
1267 | 2115 | |
1268 | 2116 | * Match 0 or more times |
1269 | 2117 | + Match 1 or more times |
1270 | 2118 | ? Match 1 or 0 times |
1271 | 2119 | {n} Match exactly n times |
1272 | 2120 | {n,} Match at least n times |
1273 | 2121 | {n,m} Match at least n but not more than m times |
1274 | 2122 | |
1275 | 2123 | =end original |
1276 | 2124 | |
1277 | 2125 | * 0 回以上マッチング |
1278 | 2126 | + 1 回以上マッチング |
1279 | 2127 | ? 0 回または 1 回マッチング |
1280 | 2128 | {n} 正確に n 回マッチング |
1281 | 2129 | {n,} 最低 n 回マッチング |
1282 | 2130 | {n,m} n 回以上 m 回以下マッチング |
1283 | 2131 | |
1284 | 2132 | =begin original |
1285 | 2133 | |
1286 | (If a curly bracket occurs in a | |
2134 | (If a non-escaped curly bracket occurs in a context other than one of | |
1287 | ||
2135 | the quantifiers listed above, where it does not form part of a | |
1288 | as | |
2136 | backslashed sequence like C<\x{...}>, it is either a fatal syntax error, | |
1289 | ||
2137 | or treated as a regular character, generally with a deprecation warning | |
1290 | ||
2138 | raised. To escape it, you can precede it with a backslash (C<"\{">) or | |
1291 | ||
2139 | enclose it within square brackets (C<"[{]">). | |
1292 | ||
2140 | This change will allow for future syntax extensions (like making the | |
1293 | or | |
2141 | lower bound of a quantifier optional), and better error checking of | |
1294 | ||
2142 | quantifiers). | |
1295 | checking of quantifiers. Now, a typo in a quantifier silently causes | |
1296 | it to be treated as the literal characters. For example, | |
1297 | 2143 | |
1298 | ||
1299 | 2144 | =end original |
1300 | 2145 | |
1301 | ( | |
2146 | (前述した量指定子の一つ以外のコンテキストでエスケープされない中かっこが | |
1302 | ||
2147 | 使われて、C<\x{...}> のような逆スラッシュ付き並びの一部ではないときには、 | |
1303 | ||
2148 | 普通の文字として扱われるか、致命的エラーになり、どちらでも | |
1304 | ||
2149 | 一般的には廃止予定警告が発生します。 | |
1305 | ||
2150 | これをエスケープするには、逆スラッシュを前置したり (C<"\{">) | |
1306 | ||
2151 | 大かっこで囲んだり (C<"[{]">) できます。 | |
1307 | 大かっこで囲む (C<"\{"> または C<"[{]">) ことでエスケープすることが | |
1308 | 要求されるようになる予定です。 | |
1309 | 2152 | この変更により、(量指定子の加減をオプションにするような) 将来の |
1310 | 文法の拡張ができるようになり、量指定子 | |
2153 | 文法の拡張ができるようになり、量指定子のより良いエラーチェックが | |
1311 | 2154 | できるようになります。 |
1312 | 現在のところ、量指定子のタイプミスは警告なしにリテラルな文字の並びとして | |
1313 | 扱われます。 | |
1314 | 例えば: | |
1315 | 2155 | |
1316 | /o{4,3}/ | |
1317 | ||
1318 | 2156 | =begin original |
1319 | 2157 | |
1320 | ||
2158 | The C<"*"> quantifier is equivalent to C<{0,}>, the C<"+"> | |
1321 | ||
2159 | quantifier to C<{1,}>, and the C<"?"> quantifier to C<{0,1}>. I<n> and I<m> are limited | |
1322 | S<C<"o { 4 , 3 }">>.) | |
1323 | ||
1324 | =end original | |
1325 | ||
1326 | これは、4 は 3 より大きいので、0 回マッチングする量指定子に見えますが、 | |
1327 | これは実際には 6 文字並び | |
1328 | S<C<"o { 4 , 3 }">> にマッチングするという意味になります。) | |
1329 | ||
1330 | =begin original | |
1331 | ||
1332 | The "*" quantifier is equivalent to C<{0,}>, the "+" | |
1333 | quantifier to C<{1,}>, and the "?" quantifier to C<{0,1}>. n and m are limited | |
1334 | 2160 | to non-negative integral values less than a preset limit defined when perl is built. |
1335 | 2161 | This is usually 32766 on the most common platforms. The actual limit can |
1336 | 2162 | be seen in the error message generated by code such as this: |
1337 | 2163 | |
1338 | 2164 | =end original |
1339 | 2165 | |
1340 | "*" 量指定子は C<{0,}> と、"+" 量指定子は C<{1,}> と、 | |
2166 | C<"*"> 量指定子は C<{0,}> と、C<"+"> 量指定子は C<{1,}> と、 | |
1341 | "?" 量指定子は C<{0,1}> と等価です。 | |
2167 | C<"?"> 量指定子は C<{0,1}> と等価です。 | |
1342 | n | |
2168 | I<n> と I<m> は perl をビルドしたときに定義した既定の制限より小さな | |
1343 | 制限されます。 | |
2169 | 非負整数回に制限されます。 | |
1344 | 2170 | これは大抵のプラットフォームでは 32766 回になっています。 |
1345 | 2171 | 実際の制限は次のようなコードを実行すると生成されるエラーメッセージで |
1346 | 2172 | 見ることができます: |
1347 | 2173 | |
1348 | 2174 | $_ **= $_ , / {$_} / for 2 .. 42; |
1349 | 2175 | |
1350 | 2176 | =begin original |
1351 | 2177 | |
1352 | 2178 | By default, a quantified subpattern is "greedy", that is, it will match as |
1353 | 2179 | many times as possible (given a particular starting location) while still |
1354 | 2180 | allowing the rest of the pattern to match. If you want it to match the |
1355 | minimum number of times possible, follow the quantifier with a "?". Note | |
2181 | minimum number of times possible, follow the quantifier with a C<"?">. Note | |
1356 | 2182 | that the meanings don't change, just the "greediness": |
1357 | 2183 | X<metacharacter> X<greedy> X<greediness> |
1358 | 2184 | X<?> X<*?> X<+?> X<??> X<{n}?> X<{n,}?> X<{n,m}?> |
1359 | 2185 | |
1360 | 2186 | =end original |
1361 | 2187 | |
1362 | 2188 | デフォルトでは、パターンで行われる量指定は「貪欲」です; |
1363 | 2189 | つまりそれはパターンの残りの部分が可能な範囲で、 |
1364 | 2190 | (始めた地点から)可能な限り多くを先にあるパターンでマッチングさせます。 |
1365 | 2191 | もし最小回数でのマッチングを行いたいのであれば、量指定子の後ろに |
1366 | "?" を続けます。 | |
2192 | C<"?"> を続けます。 | |
1367 | 2193 | 意味は変更されずに「貪欲さ」だけを変更できます: |
1368 | 2194 | X<metacharacter> X<greedy> X<greediness> |
1369 | 2195 | X<?> X<*?> X<+?> X<??> X<{n}?> X<{n,}?> X<{n,m}?> |
1370 | 2196 | |
1371 | 2197 | =begin original |
1372 | 2198 | |
1373 | 2199 | *? Match 0 or more times, not greedily |
1374 | 2200 | +? Match 1 or more times, not greedily |
1375 | 2201 | ?? Match 0 or 1 time, not greedily |
1376 | 2202 | {n}? Match exactly n times, not greedily (redundant) |
1377 | 2203 | {n,}? Match at least n times, not greedily |
1378 | 2204 | {n,m}? Match at least n but not more than m times, not greedily |
1379 | 2205 | |
1380 | 2206 | =end original |
1381 | 2207 | |
1382 | 2208 | *? 0 回以上の貪欲でないマッチング |
1383 | 2209 | +? 1 回以上の貪欲でないマッチング |
1384 | 2210 | ?? 0 回または 1 回の貪欲でないマッチング |
1385 | 2211 | {n}? ちょうど n 回の貪欲でないマッチング (冗長) |
1386 | 2212 | {n,}? n 回以上の貪欲でないマッチング |
1387 | 2213 | {n,m}? n 回以上 m 回以下の貪欲でないマッチング |
1388 | 2214 | |
1389 | 2215 | =begin original |
1390 | 2216 | |
1391 | ||
2217 | Normally when a quantified subpattern does not allow the rest of the | |
1392 | 2218 | overall pattern to match, Perl will backtrack. However, this behaviour is |
1393 | 2219 | sometimes undesirable. Thus Perl provides the "possessive" quantifier form |
1394 | 2220 | as well. |
1395 | 2221 | |
1396 | 2222 | =end original |
1397 | 2223 | |
1398 | ||
2224 | 通常、パターンのうちの量指定された一部によってパターン全体が | |
1399 | 2225 | マッチングに失敗したとき、Perl はバックトラックを行います。 |
1400 | 2226 | しかしこの振る舞いは望まれないこともあります。 |
1401 | 2227 | そのため、Perl は「絶対最大量(possessive)」量指定形式も提供しています。 |
1402 | 2228 | |
1403 | 2229 | =begin original |
1404 | 2230 | |
1405 | 2231 | *+ Match 0 or more times and give nothing back |
1406 | 2232 | ++ Match 1 or more times and give nothing back |
1407 | 2233 | ?+ Match 0 or 1 time and give nothing back |
1408 | 2234 | {n}+ Match exactly n times and give nothing back (redundant) |
1409 | 2235 | {n,}+ Match at least n times and give nothing back |
1410 | 2236 | {n,m}+ Match at least n but not more than m times and give nothing back |
1411 | 2237 | |
1412 | 2238 | =end original |
1413 | 2239 | |
1414 | 2240 | *+ 0 回以上マッチングして何も返さない |
1415 | 2241 | ++ 1 回以上マッチングして何も返さない |
1416 | 2242 | ?+ 0 回または 1 回マッチングして何も返さない |
1417 | 2243 | {n}+ ちょうど n 回のマッチングして何も返さない (冗長) |
1418 | 2244 | {n,}+ n 回以上のマッチングして何も返さない |
1419 | 2245 | {n,m}+ n 回以上 m 回以下マッチングして何も返さない |
1420 | 2246 | |
1421 | 2247 | =begin original |
1422 | 2248 | |
1423 | 2249 | For instance, |
1424 | 2250 | |
1425 | 2251 | =end original |
1426 | 2252 | |
1427 | 2253 | 例えば、 |
1428 | 2254 | |
1429 | 2255 | 'aaaa' =~ /a++a/ |
1430 | 2256 | |
1431 | 2257 | =begin original |
1432 | 2258 | |
1433 | will never match, as the C<a++> will gobble up all the C<a>'s in the | |
2259 | will never match, as the C<a++> will gobble up all the C<"a">'s in the | |
1434 | 2260 | string and won't leave any for the remaining part of the pattern. This |
1435 | 2261 | feature can be extremely useful to give perl hints about where it |
1436 | 2262 | shouldn't backtrack. For instance, the typical "match a double-quoted |
1437 | 2263 | string" problem can be most efficiently performed when written as: |
1438 | 2264 | |
1439 | 2265 | =end original |
1440 | 2266 | |
1441 | は、C<a++> が文字列中の全ての C<a> を飲み込んでしまい、 | |
2267 | は、C<a++> が文字列中の全ての C<"a"> を飲み込んでしまい、 | |
1442 | 2268 | 後に何も残さないためマッチングしません。 |
1443 | 2269 | この機能はバックトラックするべきでない場所のヒントを perl に |
1444 | 2270 | 与えるのに非常に便利です。 |
1445 | 2271 | 例えば、典型的な「ダブルクォート文字列のマッチング」問題で次のように |
1446 | 2272 | 書くととても効率的になります: |
1447 | 2273 | |
1448 | 2274 | /"(?:[^"\\]++|\\.)*+"/ |
1449 | 2275 | |
1450 | 2276 | =begin original |
1451 | 2277 | |
1452 | 2278 | as we know that if the final quote does not match, backtracking will not |
1453 | 2279 | help. See the independent subexpression |
1454 | 2280 | L</C<< (?>pattern) >>> for more details; |
1455 | 2281 | possessive quantifiers are just syntactic sugar for that construct. For |
1456 | 2282 | instance the above example could also be written as follows: |
1457 | 2283 | |
1458 | 2284 | =end original |
1459 | 2285 | |
1460 | 2286 | 見ての通り最後のクォートがマッチングしなかったとき、バックトラックは |
1461 | 2287 | 役に立ちません。 |
1462 | 2288 | 詳細は独立したサブパターン L</C<< (?>pattern) >>> を参照してください; |
1463 | 2289 | 絶対最大量指定子はまさにその構文糖です。 |
1464 | 2290 | 例えばこの例は次のようにも書けます: |
1465 | 2291 | |
1466 | 2292 | /"(?>(?:(?>[^"\\]+)|\\.)*)"/ |
1467 | 2293 | |
2294 | =begin original | |
2295 | ||
2296 | Note that the possessive quantifier modifier can not be be combined | |
2297 | with the non-greedy modifier. This is because it would make no sense. | |
2298 | Consider the follow equivalency table: | |
2299 | ||
2300 | =end original | |
2301 | ||
2302 | 絶対最大量指定修飾子は非貪欲修飾子と結合できないことに注意してください。 | |
2303 | これは無意味だからです。 | |
2304 | 次の等価性表を考慮してください: | |
2305 | ||
2306 | Illegal Legal | |
2307 | ------------ ------ | |
2308 | X??+ X{0} | |
2309 | X+?+ X{1} | |
2310 | X{min,max}?+ X{min} | |
2311 | ||
1468 | 2312 | =head3 Escape sequences |
1469 | 2313 | |
1470 | 2314 | (エスケープシーケンス) |
1471 | 2315 | |
1472 | 2316 | =begin original |
1473 | 2317 | |
1474 | 2318 | Because patterns are processed as double-quoted strings, the following |
1475 | 2319 | also work: |
1476 | 2320 | |
1477 | 2321 | =end original |
1478 | 2322 | |
1479 | 2323 | パターンはダブルクォート文字列として処理されるため、 |
1480 | 2324 | 以下のエスケープ文字も動作します: |
1481 | 2325 | X<\t> X<\n> X<\r> X<\f> X<\e> X<\a> X<\l> X<\u> X<\L> X<\U> X<\E> X<\Q> |
1482 | 2326 | X<\0> X<\c> X<\N{}> X<\x> |
1483 | 2327 | |
1484 | 2328 | =begin original |
1485 | 2329 | |
1486 | 2330 | \t tab (HT, TAB) |
1487 | 2331 | \n newline (LF, NL) |
1488 | 2332 | \r return (CR) |
1489 | 2333 | \f form feed (FF) |
1490 | 2334 | \a alarm (bell) (BEL) |
1491 | 2335 | \e escape (think troff) (ESC) |
1492 | 2336 | \cK control char (example: VT) |
1493 | 2337 | \x{}, \x00 character whose ordinal is the given hexadecimal number |
1494 | 2338 | \N{name} named Unicode character or character sequence |
1495 | 2339 | \N{U+263D} Unicode character (example: FIRST QUARTER MOON) |
1496 | 2340 | \o{}, \000 character whose ordinal is the given octal number |
1497 | 2341 | \l lowercase next char (think vi) |
1498 | 2342 | \u uppercase next char (think vi) |
1499 | \L lowercase ti | |
2343 | \L lowercase until \E (think vi) | |
1500 | \U uppercase ti | |
2344 | \U uppercase until \E (think vi) | |
1501 | \Q quote (disable) pattern metacharacters ti | |
2345 | \Q quote (disable) pattern metacharacters until \E | |
1502 | 2346 | \E end either case modification or quoted section, think vi |
1503 | 2347 | |
1504 | 2348 | =end original |
1505 | 2349 | |
1506 | 2350 | \t タブ (水平タブ;HT、TAB) |
1507 | 2351 | \n 改行 (LF、NL) |
1508 | 2352 | \r 復帰 (CR) |
1509 | 2353 | \f フォームフィード (FF) |
1510 | 2354 | \a アラーム (ベル) (BEL) |
1511 | 2355 | \e エスケープ (troff 的) (ESC) |
1512 | 2356 | \cK 制御文字 (例: VT) |
1513 | 2357 | \x{}, \x00 16 進数で番号指定された文字 |
1514 | 2358 | \N{name} 名前付きユニコード文字または文字並び |
1515 | 2359 | \N{U+263D} Unicode 文字 (例: FIRST QUARTER MOON) |
1516 | 2360 | \o{}, \000 8 進数で番号指定された文字 |
1517 | 2361 | \l 次の文字を小文字に (vi 的) |
1518 | 2362 | \u 次の文字を大文字に (vi 的) |
1519 | 2363 | \L \E まで小文字に (vi 的) |
1520 | 2364 | \U \E まで大文字に (vi 的) |
1521 | 2365 | \Q \E までパターンメタ文字の無効化(Quote) |
1522 | 2366 | \E 大文字小文字変更またはクォートの終端 (vi 的) |
1523 | 2367 | |
1524 | 2368 | =begin original |
1525 | 2369 | |
1526 | 2370 | Details are in L<perlop/Quote and Quote-like Operators>. |
1527 | 2371 | |
1528 | 2372 | =end original |
1529 | 2373 | |
1530 | 2374 | 詳細は L<perlop/Quote and Quote-like Operators> にあります。 |
1531 | 2375 | |
1532 | 2376 | =head3 Character Classes and other Special Escapes |
1533 | 2377 | |
1534 | 2378 | (文字クラス及び他の特殊なエスケープ) |
1535 | 2379 | |
1536 | 2380 | =begin original |
1537 | 2381 | |
1538 | 2382 | In addition, Perl defines the following: |
1539 | 2383 | X<\g> X<\k> X<\K> X<backreference> |
1540 | 2384 | |
1541 | 2385 | =end original |
1542 | 2386 | |
1543 | 2387 | さらに、Perl は以下のものを定義します: |
1544 | 2388 | X<\g> X<\k> X<\K> X<backreference> |
1545 | 2389 | |
1546 | 2390 | =begin original |
1547 | 2391 | |
1548 | 2392 | Sequence Note Description |
1549 | 2393 | [...] [1] Match a character according to the rules of the |
1550 | 2394 | bracketed character class defined by the "...". |
1551 | 2395 | Example: [a-z] matches "a" or "b" or "c" ... or "z" |
1552 | 2396 | [[:...:]] [2] Match a character according to the rules of the POSIX |
1553 | 2397 | character class "..." within the outer bracketed |
1554 | 2398 | character class. Example: [[:upper:]] matches any |
1555 | 2399 | uppercase character. |
2400 | (?[...]) [8] Extended bracketed character class | |
1556 | 2401 | \w [3] Match a "word" character (alphanumeric plus "_", plus |
1557 | 2402 | other connector punctuation chars plus Unicode |
1558 | 2403 | marks) |
1559 | 2404 | \W [3] Match a non-"word" character |
1560 | 2405 | \s [3] Match a whitespace character |
1561 | 2406 | \S [3] Match a non-whitespace character |
1562 | 2407 | \d [3] Match a decimal digit character |
1563 | 2408 | \D [3] Match a non-digit character |
1564 | 2409 | \pP [3] Match P, named property. Use \p{Prop} for longer names |
1565 | 2410 | \PP [3] Match non-P |
1566 | 2411 | \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. | |
1572 | 2412 | \1 [5] Backreference to a specific capture group or buffer. |
1573 | 2413 | '1' may actually be any positive integer. |
1574 | 2414 | \g1 [5] Backreference to a specific or previous group, |
1575 | 2415 | \g{-1} [5] The number may be negative indicating a relative |
1576 | 2416 | previous group and may optionally be wrapped in |
1577 | 2417 | curly brackets for safer parsing. |
1578 | 2418 | \g{name} [5] Named backreference |
1579 | 2419 | \k<name> [5] Named backreference |
1580 | 2420 | \K [6] Keep the stuff left of the \K, don't include it in $& |
1581 | \N [7] Any character but \n | |
2421 | \N [7] Any character but \n. Not affected by /s modifier | |
1582 | /s modifier | |
1583 | 2422 | \v [3] Vertical whitespace |
1584 | 2423 | \V [3] Not vertical whitespace |
1585 | 2424 | \h [3] Horizontal whitespace |
1586 | 2425 | \H [3] Not horizontal whitespace |
1587 | 2426 | \R [4] Linebreak |
1588 | 2427 | |
1589 | 2428 | =end original |
1590 | 2429 | |
1591 | 2430 | Sequence Note Description |
1592 | 2431 | [...] [1] "..." で定義された大かっこ文字クラスのルールに従う文字に |
1593 | 2432 | マッチング。 |
1594 | 2433 | 例: [a-z] は "a", "b", "c", ... "z" にマッチング。 |
1595 | 2434 | [[:...:]] [2] 外側の大かっこ文字クラスの内側の POSIX 文字クラスに |
1596 | 2435 | 従う文字にマッチング。 |
1597 | 2436 | 例: [[:upper:]] は任意の大文字にマッチング。 |
2437 | (?[...]) [8] 拡張大かっこ文字クラス | |
1598 | 2438 | \w [3] "単語" 文字にマッチング (英数字及び "_" に加えて、 |
1599 | 2439 | その他の接続句読点文字と Unicode マークにマッチング) |
1600 | 2440 | \W [3] 非"単語"文字にマッチング |
1601 | 2441 | \s [3] 空白文字にマッチング |
1602 | 2442 | \S [3] 非空白文字にマッチング |
1603 | 2443 | \d [3] 10 進数字にマッチング |
1604 | 2444 | \D [3] 非数字にマッチング |
1605 | 2445 | \pP [3] 名前属性 P にマッチング. 長い名前であれば \p{Prop} |
1606 | 2446 | \PP [3] P以外にマッチング |
1607 | 2447 | \X [4] Unicode 拡張書記素クラスタ("eXtended grapheme cluster")にマッチング |
1608 | \C より大きな UTF-8 文字の一部であっても、1つの C 言語の文字 (オクテット)にマッチング | |
1609 | 従って文字をUTF-8バイト列へと変換するので、壊れた | |
1610 | UTF-8 片となるかもしれません; 後読みは対応していません | |
1611 | 2448 | \1 [5] 指定した捕捉グループやバッファへの後方参照。 |
1612 | 2449 | '1' には正の整数を指定できます。 |
1613 | 2450 | \g1 [5] 指定したまたは前のグループへの後方参照 |
1614 | 2451 | \g{-1} [5] 数値は相対的に前のグループを示す負の値にもできます、また |
1615 | 2452 | 任意で安全にパースするために波かっこで括ることもできます |
1616 | 2453 | \g{name} [5] 名前指定の後方参照 |
1617 | 2454 | \k<name> [5] 名前指定の後方参照 |
1618 | 2455 | \K [6] \K の左にある物を保持、$& に含めない |
1619 | \N [7] \n 以外の任意の文字 | |
2456 | \N [7] \n 以外の任意の文字; /s 修飾子の影響は受けない | |
1620 | 2457 | \v [3] 垂直空白 |
1621 | 2458 | \V [3] 垂直空白以外 |
1622 | 2459 | \h [3] 水平空白 |
1623 | 2460 | \H [3] 水平空白以外 |
1624 | 2461 | \R [4] 行区切り |
1625 | 2462 | |
1626 | 2463 | =over 4 |
1627 | 2464 | |
1628 | 2465 | =item [1] |
1629 | 2466 | |
1630 | 2467 | =begin original |
1631 | 2468 | |
1632 | 2469 | See L<perlrecharclass/Bracketed Character Classes> for details. |
1633 | 2470 | |
1634 | 2471 | =end original |
1635 | 2472 | |
1636 | 2473 | 詳しくは L<perlrecharclass/Bracketed Character Classes> を参照してください。 |
1637 | 2474 | |
1638 | 2475 | =item [2] |
1639 | 2476 | |
1640 | 2477 | =begin original |
1641 | 2478 | |
1642 | 2479 | See L<perlrecharclass/POSIX Character Classes> for details. |
1643 | 2480 | |
1644 | 2481 | =end original |
1645 | 2482 | |
1646 | 2483 | 詳しくは L<perlrecharclass/POSIX Character Classes> を参照してください。 |
1647 | 2484 | |
1648 | 2485 | =item [3] |
1649 | 2486 | |
1650 | 2487 | =begin original |
1651 | 2488 | |
1652 | 2489 | See L<perlrecharclass/Backslash sequences> for details. |
1653 | 2490 | |
1654 | 2491 | =end original |
1655 | 2492 | |
1656 | 2493 | 詳しくは L<perlrecharclass/Backslash sequences> を参照してください。 |
1657 | 2494 | |
1658 | 2495 | =item [4] |
1659 | 2496 | |
1660 | 2497 | =begin original |
1661 | 2498 | |
1662 | 2499 | See L<perlrebackslash/Misc> for details. |
1663 | 2500 | |
1664 | 2501 | =end original |
1665 | 2502 | |
1666 | 2503 | 詳しくは L<perlrebackslash/Misc> を参照してください。 |
1667 | 2504 | |
1668 | 2505 | =item [5] |
1669 | 2506 | |
1670 | 2507 | =begin original |
1671 | 2508 | |
1672 | 2509 | See L</Capture groups> below for details. |
1673 | 2510 | |
1674 | 2511 | =end original |
1675 | 2512 | |
1676 | 2513 | 詳しくは以下の L</Capture groups> を参照してください。 |
1677 | 2514 | |
1678 | 2515 | =item [6] |
1679 | 2516 | |
1680 | 2517 | =begin original |
1681 | 2518 | |
1682 | 2519 | See L</Extended Patterns> below for details. |
1683 | 2520 | |
1684 | 2521 | =end original |
1685 | 2522 | |
1686 | 2523 | 詳しくは以下のSee L</Extended Patterns> を参照してください。 |
1687 | 2524 | |
1688 | 2525 | =item [7] |
1689 | 2526 | |
1690 | 2527 | =begin original |
1691 | 2528 | |
1692 | 2529 | Note that C<\N> has two meanings. When of the form C<\N{NAME}>, it matches the |
1693 | 2530 | character or character sequence whose name is C<NAME>; and similarly |
1694 | 2531 | when of the form C<\N{U+I<hex>}>, it matches the character whose Unicode |
1695 | 2532 | code point is I<hex>. Otherwise it matches any character but C<\n>. |
1696 | 2533 | |
1697 | 2534 | =end original |
1698 | 2535 | |
1699 | 2536 | C<\N> には二つの意味があることに注意してください。 |
1700 | 2537 | C<\N{NAME}> の形式では、これは名前が C<NAME> の文字または文字の並びに |
1701 | 2538 | マッチングします; |
1702 | 2539 | 同様に、C<\N{U+I<wide hex char>}> の形式では、Unicode 符号位置が |
1703 | 2540 | I<hex> の文字にマッチングします。 |
1704 | 2541 | そうでなければ、C<\n> 以外の任意の文字にマッチングします。 |
1705 | 2542 | |
2543 | =item [8] | |
2544 | ||
2545 | =begin original | |
2546 | ||
2547 | See L<perlrecharclass/Extended Bracketed Character Classes> for details. | |
2548 | ||
2549 | =end original | |
2550 | ||
2551 | 詳しくは L<perlrecharclass/Extended Bracketed Character Classes> を | |
2552 | 参照してください。 | |
2553 | ||
1706 | 2554 | =back |
1707 | 2555 | |
1708 | 2556 | =head3 Assertions |
1709 | 2557 | |
1710 | 2558 | (言明) |
1711 | 2559 | |
1712 | 2560 | =begin original |
1713 | 2561 | |
1714 | Perl defines the following | |
2562 | Besides L<C<"^"> and C<"$">|/Metacharacters>, Perl defines the following | |
2563 | zero-width assertions: | |
1715 | 2564 | X<zero-width assertion> X<assertion> X<regex, zero-width assertion> |
1716 | 2565 | X<regexp, zero-width assertion> |
1717 | 2566 | X<regular expression, zero-width assertion> |
1718 | 2567 | X<\b> X<\B> X<\A> X<\Z> X<\z> X<\G> |
1719 | 2568 | |
1720 | 2569 | =end original |
1721 | 2570 | |
2571 | L<C<"^"> と C<"$">|/Metacharacters> の他に、 | |
1722 | 2572 | Perl は以下のゼロ幅のアサーションを定義しています: |
1723 | 2573 | X<zero-width assertion> X<assertion> X<regex, zero-width assertion> |
1724 | 2574 | X<regexp, zero-width assertion> |
1725 | 2575 | X<regular expression, zero-width assertion> |
1726 | 2576 | X<\b> X<\B> X<\A> X<\Z> X<\z> X<\G> |
1727 | 2577 | |
1728 | | |
2578 | \b{} Match at Unicode boundary of specified type | |
1729 | | |
2579 | \B{} Match where corresponding \b{} doesn't match | |
1730 | | |
2580 | \b Match a \w\W or \W\w boundary | |
1731 | | |
2581 | \B Match except at a \w\W or \W\w boundary | |
1732 | | |
2582 | \A Match only at beginning of string | |
1733 | | |
2583 | \Z Match only at end of string, or before newline at the end | |
2584 | \z Match only at end of string | |
2585 | \G Match only at pos() (e.g. at the end-of-match position | |
1734 | 2586 | of prior m//g) |
1735 | 2587 | |
1736 | 2588 | =begin original |
1737 | 2589 | |
2590 | A Unicode boundary (C<\b{}>), available starting in v5.22, is a spot | |
2591 | between two characters, or before the first character in the string, or | |
2592 | after the final character in the string where certain criteria defined | |
2593 | by Unicode are met. See L<perlrebackslash/\b{}, \b, \B{}, \B> for | |
2594 | details. | |
2595 | ||
2596 | =end original | |
2597 | ||
2598 | v5.22 から利用可能である Unicode 境界 (C<\b{}>) は、 | |
2599 | Unicode で定義されたある種の基準に一致した、 | |
2600 | 二つの文字の間か、文字列の最初の文字の前か、 | |
2601 | 文字列の最後の文字の後の地点です。 | |
2602 | 詳しくは L<perlrebackslash/\b{}, \b, \B{}, \B> を参照してください。 | |
2603 | ||
2604 | =begin original | |
2605 | ||
1738 | 2606 | A word boundary (C<\b>) is a spot between two characters |
1739 | 2607 | that has a C<\w> on one side of it and a C<\W> on the other side |
1740 | 2608 | of it (in either order), counting the imaginary characters off the |
1741 | 2609 | beginning and end of the string as matching a C<\W>. (Within |
1742 | 2610 | character classes C<\b> represents backspace rather than a word |
1743 | 2611 | boundary, just as it normally does in any double-quoted string.) |
1744 | The C<\A> and C<\Z> are just like "^" and "$", except that they | |
2612 | The C<\A> and C<\Z> are just like C<"^"> and C<"$">, except that they | |
1745 | 2613 | won't match multiple times when the C</m> modifier is used, while |
1746 | "^" and "$" will match at every internal line boundary. To match | |
2614 | C<"^"> and C<"$"> will match at every internal line boundary. To match | |
1747 | 2615 | the actual end of the string and not ignore an optional trailing |
1748 | 2616 | newline, use C<\z>. |
1749 | 2617 | X<\b> X<\A> X<\Z> X<\z> X</m> |
1750 | 2618 | |
1751 | 2619 | =end original |
1752 | 2620 | |
1753 | 2621 | 単語境界(C<\b>)はC<\W> にマッチングする文字列の始まりと終わりを |
1754 | 2622 | 連想するような、片側を C<\w>、もう片側を C<\W> で挟まれている点です。 |
1755 | 2623 | (文字クラスにおいては C<\b> は単語境界ではなくバックスペースを表します, |
1756 | 2624 | ちょうどダブルクォート文字列と同じように。) |
1757 | C<\A> 及び C<\Z> は "^" 及び "$" と同様ですが、C</m> 修飾子が | |
2625 | C<\A> 及び C<\Z> は C<"^"> 及び C<"$"> と同様ですが、C</m> 修飾子が | |
1758 | 指定されているときに "^" 及び "$" は全ての内部的な行境界に | |
2626 | 指定されているときに C<"^"> 及び C<"$"> は全ての内部的な行境界に | |
1759 | 2627 | マッチングするのに対して C<\A> 及び C<\Z> は複数回のマッチングには |
1760 | 2628 | なりません。 |
1761 | 2629 | 文字列の本当の末尾にマッチングさせ、省略可能である末尾の改行を |
1762 | 2630 | 無視しないようにする C<\z> を使います。 |
1763 | 2631 | X<\b> X<\A> X<\Z> X<\z> X</m> |
1764 | 2632 | |
1765 | 2633 | =begin original |
1766 | 2634 | |
1767 | 2635 | The C<\G> assertion can be used to chain global matches (using |
1768 | 2636 | C<m//g>), as described in L<perlop/"Regexp Quote-Like Operators">. |
1769 | 2637 | It is also useful when writing C<lex>-like scanners, when you have |
1770 | 2638 | several patterns that you want to match against consequent substrings |
1771 | 2639 | of your string; see the previous reference. The actual location |
1772 | 2640 | where C<\G> will match can also be influenced by using C<pos()> as |
1773 | 2641 | an lvalue: see L<perlfunc/pos>. Note that the rule for zero-length |
1774 | 2642 | matches (see L</"Repeated Patterns Matching a Zero-length Substring">) |
1775 | 2643 | is modified somewhat, in that contents to the left of C<\G> are |
1776 | 2644 | not counted when determining the length of the match. Thus the following |
1777 | 2645 | will not match forever: |
1778 | 2646 | X<\G> |
1779 | 2647 | |
1780 | 2648 | =end original |
1781 | 2649 | |
1782 | 2650 | C<\G> アサーションはグローバルなマッチング(C<m//g>)を連結するために |
1783 | 2651 | 使います; これは L<perlop/"Regexp Quote-Like Operators"> にも説明されています。 |
1784 | 2652 | これは文字列に対していくつかのパターンを次々にマッチングさせたいといった、 |
1785 | 2653 | C<lex> 風のスキャナを書きたいときにも便利です; 以前のリファレンスを |
1786 | 2654 | 参照してください。 |
1787 | 2655 | C<\G> が実際にマッチングできる位置は C<pos()> を左辺値として |
1788 | 2656 | 使うことで変更できます: L<perlfunc/pos> を参照してください。 |
1789 | 2657 | ゼロ幅マッチング |
1790 | 2658 | (L</"Repeated Patterns Matching a Zero-length Substring"> を参照してください) |
1791 | 2659 | のルールは少し変化することに注意してください、 |
1792 | 2660 | C<\G> の左にある内容はマッチングの長さを決定するときに |
1793 | 2661 | 数えられません。 |
1794 | 2662 | 従って次のコードは永遠にマッチングしません: |
1795 | 2663 | X<\G> |
1796 | 2664 | |
1797 | 2665 | my $string = 'ABC'; |
1798 | 2666 | pos($string) = 1; |
1799 | 2667 | while ($string =~ /(.\G)/g) { |
1800 | 2668 | print $1; |
1801 | 2669 | } |
1802 | 2670 | |
1803 | 2671 | =begin original |
1804 | 2672 | |
1805 | 2673 | It will print 'A' and then terminate, as it considers the match to |
1806 | 2674 | be zero-width, and thus will not match at the same position twice in a |
1807 | 2675 | row. |
1808 | 2676 | |
1809 | 2677 | =end original |
1810 | 2678 | |
1811 | 2679 | これはゼロ幅へのマッチングと見なされ、'A' を出力し終了するので、 |
1812 | 2680 | 行の中で同じ場所に二度はマッチングしません。 |
1813 | 2681 | |
1814 | 2682 | =begin original |
1815 | 2683 | |
1816 | 2684 | It is worth noting that C<\G> improperly used can result in an infinite |
1817 | 2685 | loop. Take care when using patterns that include C<\G> in an alternation. |
1818 | 2686 | |
1819 | 2687 | =end original |
1820 | 2688 | |
1821 | 2689 | 適切に使われていない C<\G> は無限ループとなり何の価値もありません。 |
1822 | 2690 | 代替(alternation; C<|>)の中に C<\G> を含んでいるパターンを使う際には |
1823 | 2691 | 十分注意してください。 |
1824 | 2692 | |
2693 | =begin original | |
2694 | ||
2695 | Note also that C<s///> will refuse to overwrite part of a substitution | |
2696 | that has already been replaced; so for example this will stop after the | |
2697 | first iteration, rather than iterating its way backwards through the | |
2698 | string: | |
2699 | ||
2700 | =end original | |
2701 | ||
2702 | C<s///> は置換部の既に置き換えられた部分を上書きすることを拒否することにも | |
2703 | 注意してください; 従って例えばこれは文字列の後ろ向きの反復中ではなく、 | |
2704 | 最初の反復の後に停止します: | |
2705 | ||
2706 | $_ = "123456789"; | |
2707 | pos = 6; | |
2708 | s/.(?=.\G)/X/g; | |
2709 | print; # prints 1234X6789, not XXXXX6789 | |
2710 | ||
1825 | 2711 | =head3 Capture groups |
1826 | 2712 | |
1827 | 2713 | (捕捉グループ) |
1828 | 2714 | |
1829 | 2715 | =begin original |
1830 | 2716 | |
1831 | The | |
2717 | The grouping construct C<( ... )> creates capture groups (also referred to as | |
1832 | 2718 | capture buffers). To refer to the current contents of a group later on, within |
1833 | 2719 | the same pattern, use C<\g1> (or C<\g{1}>) for the first, C<\g2> (or C<\g{2}>) |
1834 | 2720 | for the second, and so on. |
1835 | 2721 | This is called a I<backreference>. |
1836 | 2722 | X<regex, capture buffer> X<regexp, capture buffer> |
1837 | 2723 | X<regex, capture group> X<regexp, capture group> |
1838 | 2724 | X<regular expression, capture buffer> X<backreference> |
1839 | 2725 | X<regular expression, capture group> X<backreference> |
1840 | 2726 | X<\g{1}> X<\g{-1}> X<\g{name}> X<relative backreference> X<named backreference> |
1841 | 2727 | X<named capture buffer> X<regular expression, named capture buffer> |
1842 | 2728 | X<named capture group> X<regular expression, named capture group> |
1843 | 2729 | X<%+> X<$+{name}> X<< \k<name> >> |
1844 | 2730 | There is no limit to the number of captured substrings that you may use. |
1845 | Groups are numbered with the leftmost open parenthesis being number 1, etc. If | |
2731 | Groups are numbered with the leftmost open parenthesis being number 1, I<etc>. If | |
1846 | 2732 | a group did not match, the associated backreference won't match either. (This |
1847 | 2733 | can happen if the group is optional, or in a different branch of an |
1848 | 2734 | alternation.) |
1849 | You can omit the C<"g">, and write C<"\1">, etc, but there are some issues with | |
2735 | You can omit the C<"g">, and write C<"\1">, I<etc>, but there are some issues with | |
1850 | 2736 | this form, described below. |
1851 | 2737 | |
1852 | 2738 | =end original |
1853 | 2739 | |
1854 | ||
2740 | グループ化構文 C<( ... )> は捕捉グループを作成します (そして捕捉バッファとして | |
1855 | 2741 | 参照します)。 |
1856 | 2742 | 同じパターンの中で、あるグループの現在の内容を後で参照するには、 |
1857 | 2743 | 最初のものには C<\g1> (または C<\g{1}>) を、2 番目には C<\g2> (または |
1858 | 2744 | C<\g{2}>) を、以下同様のものを使います。 |
1859 | 2745 | これを I<後方参照> (backreference) と呼びます。 |
1860 | 2746 | X<regex, capture buffer> X<regexp, capture buffer> |
1861 | 2747 | X<regex, capture group> X<regexp, capture group> |
1862 | 2748 | X<regular expression, capture buffer> X<backreference> |
1863 | 2749 | X<regular expression, capture group> X<backreference> |
1864 | 2750 | X<\g{1}> X<\g{-1}> X<\g{name}> X<relative backreference> X<named backreference> |
1865 | 2751 | X<named capture buffer> X<regular expression, named capture buffer> |
1866 | 2752 | X<named capture group> X<regular expression, named capture group> |
1867 | 2753 | X<%+> X<$+{name}> X<< \k<name> >> |
1868 | 2754 | 使う捕捉部分文字列の数に制限はありません。 |
1869 | 2755 | グループはいちばん左の開きかっこを 1 番として番号付けされます。 |
1870 | 2756 | グループがマッチングしなかった場合、対応する後方参照もマッチングしません。 |
1871 | 2757 | (これはグループがオプションか、選択の異なる枝の場合に怒ることがあります。) |
1872 | 2758 | C<"g"> を省略して C<"\1"> などと書くこともできますが、後で述べるように、 |
1873 | 2759 | この形式にはいくらかの問題があります。 |
1874 | 2760 | |
1875 | 2761 | =begin original |
1876 | 2762 | |
1877 | 2763 | You can also refer to capture groups relatively, by using a negative number, so |
1878 | 2764 | that C<\g-1> and C<\g{-1}> both refer to the immediately preceding capture |
1879 | 2765 | group, and C<\g-2> and C<\g{-2}> both refer to the group before it. For |
1880 | 2766 | example: |
1881 | 2767 | |
1882 | 2768 | =end original |
1883 | 2769 | |
1884 | 2770 | 負数を使うことで捕捉グループを相対的に参照することもできます; C<\g-1> と |
1885 | 2771 | C<\g{-1}> は両方とも直前の捕捉グループを参照し、C<\g-2> と C<\g{-2}> は |
1886 | 2772 | 両方ともその前のグループを参照します。 |
1887 | 2773 | 例えば: |
1888 | 2774 | |
1889 | 2775 | / |
1890 | 2776 | (Y) # group 1 |
1891 | 2777 | ( # group 2 |
1892 | 2778 | (X) # group 3 |
1893 | 2779 | \g{-1} # backref to group 3 |
1894 | 2780 | \g{-3} # backref to group 1 |
1895 | 2781 | ) |
1896 | 2782 | /x |
1897 | 2783 | |
1898 | 2784 | =begin original |
1899 | 2785 | |
1900 | 2786 | would match the same as C</(Y) ( (X) \g3 \g1 )/x>. This allows you to |
1901 | 2787 | interpolate regexes into larger regexes and not have to worry about the |
1902 | 2788 | capture groups being renumbered. |
1903 | 2789 | |
1904 | 2790 | =end original |
1905 | 2791 | |
1906 | 2792 | は C</(Y) ( (X) \g3 \g1 )/x> と同じマッチングとなります。 |
1907 | 2793 | これにより、正規表現をより大きな正規表現に挿入したときに、捕捉グループの |
1908 | 2794 | 番号を振り直す心配をする必要がなくなります。 |
1909 | 2795 | |
1910 | 2796 | =begin original |
1911 | 2797 | |
1912 | 2798 | You can dispense with numbers altogether and create named capture groups. |
1913 | 2799 | The notation is C<(?E<lt>I<name>E<gt>...)> to declare and C<\g{I<name>}> to |
1914 | 2800 | reference. (To be compatible with .Net regular expressions, C<\g{I<name>}> may |
1915 | 2801 | also be written as C<\k{I<name>}>, C<\kE<lt>I<name>E<gt>> or C<\k'I<name>'>.) |
1916 | 2802 | I<name> must not begin with a number, nor contain hyphens. |
1917 | 2803 | When different groups within the same pattern have the same name, any reference |
1918 | 2804 | to that name assumes the leftmost defined group. Named groups count in |
1919 | 2805 | absolute and relative numbering, and so can also be referred to by those |
1920 | 2806 | numbers. |
1921 | 2807 | (It's possible to do things with named capture groups that would otherwise |
1922 | 2808 | require C<(??{})>.) |
1923 | 2809 | |
1924 | 2810 | =end original |
1925 | 2811 | |
1926 | 2812 | 数値を全く使わずに、名前付き捕捉グループを作ることが出来ます。 |
1927 | 2813 | 記法は、宣言が C<(?E<lt>I<name>E<gt>...)>、参照が C<\g{I<name>}> です。 |
1928 | 2814 | (.Net 正規表現との互換性のために、C<\g{I<name>}> は C<\k{I<name>}>, |
1929 | 2815 | C<\kE<lt>I<name>E<gt>>, C<\k'I<name>'> とも書けます。) |
1930 | 2816 | I<name> は数字で始まってはならず、ハイフンを含んではなりません。 |
1931 | 2817 | 同じパターンの中に同じ名前の違うグループがある場合、 |
1932 | 2818 | この名前での参照は一番左で定義されたものを仮定します。 |
1933 | 2819 | 名前付きグループも絶対や相対番号付けに含まれるので、 |
1934 | 2820 | 番号で参照することも出来ます。 |
1935 | 2821 | (C<(??{})> が必要な場合でも名前付き捕捉グループを使うことが出来ます。) |
1936 | 2822 | |
1937 | 2823 | =begin original |
1938 | 2824 | |
1939 | 2825 | Capture group contents are dynamically scoped and available to you outside the |
1940 | 2826 | pattern until the end of the enclosing block or until the next successful |
1941 | 2827 | match, whichever comes first. (See L<perlsyn/"Compound Statements">.) |
1942 | 2828 | You can refer to them by absolute number (using C<"$1"> instead of C<"\g1">, |
1943 | etc); or by name via the C<%+> hash, using C<"$+{I<name>}">. | |
2829 | I<etc>); or by name via the C<%+> hash, using C<"$+{I<name>}">. | |
1944 | 2830 | |
1945 | 2831 | =end original |
1946 | 2832 | |
1947 | 2833 | 捕捉グループの内容は動的スコープを持ち、パターンの外側でも現在のブロックの |
1948 | 2834 | 末尾か次のマッチングの成功のどちらか早いほうまで利用可能です。 |
1949 | 2835 | (L<perlsyn/"Compound Statements"> を参照してください。) |
1950 | 2836 | これらに対して (C<"\g1"> などの代わりに C<"$1"> を使って) 絶対値で |
1951 | 2837 | 参照するか、C<"$+{I<name>}"> を使って C<%+> 経由で名前で参照できます。 |
1952 | 2838 | |
1953 | 2839 | =begin original |
1954 | 2840 | |
1955 | 2841 | Braces are required in referring to named capture groups, but are optional for |
1956 | 2842 | absolute or relative numbered ones. Braces are safer when creating a regex by |
1957 | 2843 | concatenating smaller strings. For example if you have C<qr/$a$b/>, and C<$a> |
1958 | 2844 | contained C<"\g1">, and C<$b> contained C<"37">, you would get C</\g137/> which |
1959 | 2845 | is probably not what you intended. |
1960 | 2846 | |
1961 | 2847 | =end original |
1962 | 2848 | |
1963 | 2849 | 名前付き捕捉グループを参照するには中かっこが必要です; |
1964 | 2850 | しかし、絶対数値や相対数値の場合はオプションです。 |
1965 | 2851 | より小さい文字列を結合して正規表現を作る場合は中かっこを使う方が安全です。 |
1966 | 2852 | 例えば C<qr/$a$b/> で C<$a> に C<"\g1"> を含み、 |
1967 | 2853 | C<$b> に C<"37"> を含んでいるとき、 |
1968 | 2854 | 結果は C</\g137/> となりますが、おそらく望んでいたものではないでしょう。 |
1969 | 2855 | |
1970 | 2856 | =begin original |
1971 | 2857 | |
1972 | 2858 | The C<\g> and C<\k> notations were introduced in Perl 5.10.0. Prior to that |
1973 | 2859 | there were no named nor relative numbered capture groups. Absolute numbered |
1974 | 2860 | groups were referred to using C<\1>, |
1975 | C<\2>, etc., and this notation is still | |
2861 | C<\2>, I<etc>., and this notation is still | |
1976 | 2862 | accepted (and likely always will be). But it leads to some ambiguities if |
1977 | 2863 | there are more than 9 capture groups, as C<\10> could mean either the tenth |
1978 | 2864 | capture group, or the character whose ordinal in octal is 010 (a backspace in |
1979 | 2865 | ASCII). Perl resolves this ambiguity by interpreting C<\10> as a backreference |
1980 | 2866 | only if at least 10 left parentheses have opened before it. Likewise C<\11> is |
1981 | 2867 | a backreference only if at least 11 left parentheses have opened before it. |
1982 | 2868 | And so on. C<\1> through C<\9> are always interpreted as backreferences. |
1983 | 2869 | There are several examples below that illustrate these perils. You can avoid |
1984 | 2870 | the ambiguity by always using C<\g{}> or C<\g> if you mean capturing groups; |
1985 | 2871 | and for octal constants always using C<\o{}>, or for C<\077> and below, using 3 |
1986 | 2872 | digits padded with leading zeros, since a leading zero implies an octal |
1987 | 2873 | constant. |
1988 | 2874 | |
1989 | 2875 | =end original |
1990 | 2876 | |
1991 | 2877 | C<\g> と C<\k> の記法は Perl 5.10.0 で導入されました。 |
1992 | 2878 | それより前には名前付きや相対数値指定の捕捉グループはありませんでした。 |
1993 | 2879 | 絶対数値指定のグループは C<\1>, C<\2> などとして参照でき、この記法はまだ |
1994 | 2880 | 受け付けられます (そしておそらくいつも受け付けられます)。 |
1995 | 2881 | しかし、これは 9 を越える捕捉グループがあるとあいまいさがあります; |
1996 | 2882 | C<\10> は 10 番目の捕捉グループとも、8 進数で 010 の文字(ASCII で |
1997 | 2883 | バックスペース)とも解釈できます。 |
1998 | 2884 | Perl はこのあいまいさを以下のように解決します; |
1999 | 2885 | C<\10> の場合、これの前に少なくとも 10 の左かっこがある場合にのみ |
2000 | 2886 | これを後方参照として解釈します。 |
2001 | 2887 | 同様に、C<\11> はその前に少なくとも 11 の左かっこがある場合にのみ |
2002 | 2888 | これを後方参照として解釈します。 |
2003 | 2889 | 以下同様です。 |
2004 | 2890 | C<\1> から C<\9> は常に後方参照として解釈されます。 |
2005 | 2891 | これを図示するいくつかの例が後にあります。 |
2006 | 2892 | 捕捉グループを意味する場合は常に C<\g{}> や C<\g> を使うことで |
2007 | 2893 | あいまいさを避けられます; |
2008 | 2894 | そして 8 進定数については常に C<\o{}> を使うか、C<\077> 以下の場合は、 |
2009 | 2895 | 先頭に 0 を付けて 3 桁にします; なぜなら先頭に 0 が付くと |
2010 | 2896 | 8 進定数を仮定するからです。 |
2011 | 2897 | |
2012 | 2898 | =begin original |
2013 | 2899 | |
2014 | 2900 | The C<\I<digit>> notation also works in certain circumstances outside |
2015 | 2901 | the pattern. See L</Warning on \1 Instead of $1> below for details. |
2016 | 2902 | |
2017 | 2903 | =end original |
2018 | 2904 | |
2019 | 2905 | C<\I<digit>> 記法は、ある種の状況ではパターンの外側でも動作します。 |
2020 | 2906 | 詳しくは後述する L</Warning on \1 Instead of $1> を参照して下さい。 |
2021 | 2907 | |
2022 | 2908 | =begin original |
2023 | 2909 | |
2024 | 2910 | Examples: |
2025 | 2911 | |
2026 | 2912 | =end original |
2027 | 2913 | |
2028 | 2914 | 例: |
2029 | 2915 | |
2030 | 2916 | s/^([^ ]*) *([^ ]*)/$2 $1/; # swap first two words |
2031 | 2917 | |
2032 | 2918 | /(.)\g1/ # find first doubled char |
2033 | 2919 | and print "'$1' is the first doubled character\n"; |
2034 | 2920 | |
2035 | 2921 | /(?<char>.)\k<char>/ # ... a different way |
2036 | 2922 | and print "'$+{char}' is the first doubled character\n"; |
2037 | 2923 | |
2038 | 2924 | /(?'char'.)\g1/ # ... mix and match |
2039 | 2925 | and print "'$1' is the first doubled character\n"; |
2040 | 2926 | |
2041 | 2927 | if (/Time: (..):(..):(..)/) { # parse out values |
2042 | 2928 | $hours = $1; |
2043 | 2929 | $minutes = $2; |
2044 | 2930 | $seconds = $3; |
2045 | 2931 | } |
2046 | 2932 | |
2047 | 2933 | /(.)(.)(.)(.)(.)(.)(.)(.)(.)\g10/ # \g10 is a backreference |
2048 | 2934 | /(.)(.)(.)(.)(.)(.)(.)(.)(.)\10/ # \10 is octal |
2049 | 2935 | /((.)(.)(.)(.)(.)(.)(.)(.)(.))\10/ # \10 is a backreference |
2050 | 2936 | /((.)(.)(.)(.)(.)(.)(.)(.)(.))\010/ # \010 is octal |
2051 | 2937 | |
2052 | 2938 | $a = '(.)\1'; # Creates problems when concatenated. |
2053 | 2939 | $b = '(.)\g{1}'; # Avoids the problems. |
2054 | 2940 | "aa" =~ /${a}/; # True |
2055 | 2941 | "aa" =~ /${b}/; # True |
2056 | 2942 | "aa0" =~ /${a}0/; # False! |
2057 | 2943 | "aa0" =~ /${b}0/; # True |
2058 | 2944 | "aa\x08" =~ /${a}0/; # True! |
2059 | 2945 | "aa\x08" =~ /${b}0/; # False |
2060 | 2946 | |
2061 | 2947 | =begin original |
2062 | 2948 | |
2063 | 2949 | Several special variables also refer back to portions of the previous |
2064 | 2950 | match. C<$+> returns whatever the last bracket match matched. |
2065 | 2951 | C<$&> returns the entire matched string. (At one point C<$0> did |
2066 | 2952 | also, but now it returns the name of the program.) C<$`> returns |
2067 | 2953 | everything before the matched string. C<$'> returns everything |
2068 | 2954 | after the matched string. And C<$^N> contains whatever was matched by |
2069 | 2955 | the most-recently closed group (submatch). C<$^N> can be used in |
2070 | 2956 | extended patterns (see below), for example to assign a submatch to a |
2071 | 2957 | variable. |
2072 | 2958 | X<$+> X<$^N> X<$&> X<$`> X<$'> |
2073 | 2959 | |
2074 | 2960 | =end original |
2075 | 2961 | |
2076 | 2962 | いくつかの特殊変数もまた以前のマッチングの一部を参照しています。 |
2077 | 2963 | C<$+> は最後のマッチングしたブラケットマッチングを返します。 |
2078 | 2964 | C<$&> はマッチングした文字列全体を返します。 |
2079 | 2965 | (一頃は C<$0> もそうでしたが、現在ではこれはプログラム名を返します。) |
2080 | 2966 | C<$`> はマッチングした文字列の前の全てを返します。 |
2081 | 2967 | C<$'> はマッチングした文字列の後の全てを返します。 |
2082 | 2968 | そして C<$^N> には一番最後に閉じたグループ(サブマッチング)に |
2083 | 2969 | マッチングしたものを含んでいます。 |
2084 | 2970 | C<$^N> は例えばサブマッチングを変数に格納するため等に拡張パターンの中でも |
2085 | 2971 | 利用できます(後述)。 |
2086 | 2972 | X<$+> X<$^N> X<$&> X<$`> X<$'> |
2087 | 2973 | |
2088 | 2974 | =begin original |
2089 | 2975 | |
2090 | 2976 | These special variables, like the C<%+> hash and the numbered match variables |
2091 | (C<$1>, C<$2>, C<$3>, etc.) are dynamically scoped | |
2977 | (C<$1>, C<$2>, C<$3>, I<etc>.) are dynamically scoped | |
2092 | 2978 | until the end of the enclosing block or until the next successful |
2093 | 2979 | match, whichever comes first. (See L<perlsyn/"Compound Statements">.) |
2094 | 2980 | X<$+> X<$^N> X<$&> X<$`> X<$'> |
2095 | 2981 | X<$1> X<$2> X<$3> X<$4> X<$5> X<$6> X<$7> X<$8> X<$9> |
2096 | 2982 | |
2097 | 2983 | =end original |
2098 | 2984 | |
2099 | 2985 | C<%+> ハッシュのような特殊変数と、数値によるマッチング変数 |
2100 | 2986 | (C<$1>, C<$2>, C<$3> など)はブロックの終端または次のマッチング |
2101 | 2987 | 成功までのどちらか先に満たした方の、動的なスコープを持ちます。 |
2102 | 2988 | (L<perlsyn/"Compound Statements"> を参照してください。) |
2103 | 2989 | X<$+> X<$^N> X<$&> X<$`> X<$'> |
2104 | 2990 | X<$1> X<$2> X<$3> X<$4> X<$5> X<$6> X<$7> X<$8> X<$9> |
2105 | 2991 | |
2106 | 2992 | =begin original |
2107 | 2993 | |
2108 | 2994 | B<NOTE>: Failed matches in Perl do not reset the match variables, |
2109 | 2995 | which makes it easier to write code that tests for a series of more |
2110 | 2996 | specific cases and remembers the best match. |
2111 | 2997 | |
2112 | 2998 | =end original |
2113 | 2999 | |
2114 | 3000 | B<補足>: Perl において失敗したマッチングはマッチング変数をリセットしません; |
2115 | 3001 | これはより特殊化させる一連のテストを書くことや、 |
2116 | 3002 | 最善のマッチングを書くことを容易にします。 |
2117 | 3003 | |
2118 | 3004 | =begin original |
2119 | 3005 | |
2120 | B<WARNING>: | |
3006 | B<WARNING>: If your code is to run on Perl 5.16 or earlier, | |
3007 | beware that once Perl sees that you need one of C<$&>, C<$`>, or | |
2121 | 3008 | C<$'> anywhere in the program, it has to provide them for every |
2122 | pattern match. This may substantially slow your program. | |
3009 | pattern match. This may substantially slow your program. | |
2123 | ||
2124 | ||
3011 | =end original | |
2125 | ||
3013 | B<警告>: あなたのコードが Perl 5.16 以前で実行されるものの場合、 | |
3014 | Perl は、一旦プログラム中のどこかで C<$&>, C<$`>, C<$'> の | |
3015 | いずれかを必要としていることを見つけると、全てのパターンマッチングで | |
3016 | それらを提供しなければなあらないことに注意してください。 | |
3017 | これはあなたのプログラムを大幅に遅くさせるでしょう。 | |
3018 | ||
3019 | =begin original | |
3020 | ||
3021 | Perl uses the same mechanism to produce C<$1>, C<$2>, I<etc>, so you also | |
3022 | pay a price for each pattern that contains capturing parentheses. | |
3023 | (To avoid this cost while retaining the grouping behaviour, use the | |
2126 | 3024 | extended regular expression C<(?: ... )> instead.) But if you never |
2127 | 3025 | use C<$&>, C<$`> or C<$'>, then patterns I<without> capturing |
2128 | 3026 | parentheses will not be penalized. So avoid C<$&>, C<$'>, and C<$`> |
2129 | 3027 | if you can, but if you can't (and some algorithms really appreciate |
2130 | 3028 | them), once you've used them once, use them at will, because you've |
2131 | already paid the price. | |
3029 | already paid the price. | |
2132 | other two. | |
2133 | 3030 | X<$&> X<$`> X<$'> |
2134 | 3031 | |
2135 | 3032 | =end original |
2136 | 3033 | |
2137 | B<警告>: Perl は、一旦プログラム中のどこかで C<$&>, C<$`>, C<$'> の | |
2138 | いずれかを必要としていることを見つけると、全てのパターンマッチングで | |
2139 | それらを提供しなければなりません。 | |
2140 | これはあなたのプログラムを大幅に遅くさせるでしょう。 | |
2141 | 3034 | Perl は C<$1>, C<$2> 等の生成にも同じメカニズムを使っているので、 |
2142 | 3035 | キャプチャのかっこに含まれるそれぞれのパターンにも |
2143 | 3036 | 同じ料金を払っています。 |
2144 | 3037 | (グループ化の振る舞いを維持しつつこのコストを削減するには |
2145 | 3038 | 拡張正規表現 C<(?: ... )> を代わりに使います |
2146 | 3039 | (訳注:Perl拡張というだけで C</x> 修飾子は不要)。) |
2147 | 3040 | ですが C<$&>, C<$`> または C<$'> を一度も使わなければ、 |
2148 | 3041 | キャプチャのかっこをI<もたない>パターンではこの不利益はなくなります。 |
2149 | 3042 | この為、可能であれば C<$&>, C<$'>, 及び C<$`> を削除しましょう: |
2150 | 3043 | しかしそれができなかった(そしてそれらを |
2151 | 3044 | 本当に理解しているアルゴリズムがあるのであれば)、一旦 |
2152 | 3045 | それらを使った時点でそれ以降は自由にそれらを使うことができます; |
2153 | 3046 | なぜならあなたは(一度使った時点で)既に代価を払っているので。 |
2154 | 5.005 であれば C<$&> は他の2つほど高価ではありません。 | |
2155 | 3047 | X<$&> X<$`> X<$'> |
2156 | 3048 | |
2157 | 3049 | =begin original |
2158 | 3050 | |
2159 | ||
3051 | Perl 5.16 introduced a slightly more efficient mechanism that notes | |
3052 | separately whether each of C<$`>, C<$&>, and C<$'> have been seen, and | |
3053 | thus may only need to copy part of the string. Perl 5.20 introduced a | |
3054 | much more efficient copy-on-write mechanism which eliminates any slowdown. | |
3055 | ||
3056 | =end original | |
3057 | ||
3058 | Perl 5.16 では、C<$`>, C<$&>, C<$'> のそれぞれが現れるかどうかを | |
3059 | 個別に記録するという少し効率的な機構が導入され、 | |
3060 | 従って文字列の一部分だけコピーするようになりました。 | |
3061 | Perl 5.20 では、全く遅くならない遙かに効率的なコピーオンライト機構を | |
3062 | 導入しました。 | |
3063 | ||
3064 | =begin original | |
3065 | ||
3066 | As another workaround for this problem, Perl 5.10.0 introduced C<${^PREMATCH}>, | |
2160 | 3067 | C<${^MATCH}> and C<${^POSTMATCH}>, which are equivalent to C<$`>, C<$&> |
2161 | 3068 | and C<$'>, B<except> that they are only guaranteed to be defined after a |
2162 | 3069 | successful match that was executed with the C</p> (preserve) modifier. |
2163 | 3070 | The use of these variables incurs no global performance penalty, unlike |
2164 | their punctuation char equivalents, however at the trade-off that you | |
3071 | their punctuation character equivalents, however at the trade-off that you | |
2165 | have to tell perl when you want to use them. | |
3072 | have to tell perl when you want to use them. As of Perl 5.20, these three | |
3073 | variables are equivalent to C<$`>, C<$&> and C<$'>, and C</p> is ignored. | |
2166 | 3074 | X</p> X<p modifier> |
2167 | 3075 | |
2168 | 3076 | =end original |
2169 | 3077 | |
2170 | この問題に対する解決策として、Perl 5.10.0 からは | |
3078 | この問題に対するもう一つの解決策として、Perl 5.10.0 からは | |
3079 | C<$`>, C<$&>, C<$'> と | |
2171 | 3080 | 等価だけれども C</p> (preseve) 修飾子を伴って実行されたマッチングが |
2172 | 3081 | 成功した後でのみ定義されることが保証される C<${^PREMATCH}>、 |
2173 | 3082 | C<${^MATCH}> 及び C<${^POSTMATCH}> を導入しました。 |
2174 | 3083 | これらの変数の使用は利用したいときに perl に伝える必要がある代わりに、 |
2175 | 3084 | 等価な記号変数とは違い全体的なパフォーマンスの低下を引き起こしません。 |
3085 | Perl 5.20 からこれら三つの変数は C<$`>, C<$&>, C<$'> と等価となり、 | |
3086 | C</p> は無視されます。 | |
2176 | 3087 | X</p> X<p modifier> |
2177 | 3088 | |
2178 | 3089 | =head2 Quoting metacharacters |
2179 | 3090 | |
2180 | 3091 | (メタ文字のクォート) |
2181 | 3092 | |
2182 | 3093 | =begin original |
2183 | 3094 | |
2184 | 3095 | Backslashed metacharacters in Perl are alphanumeric, such as C<\b>, |
2185 | 3096 | C<\w>, C<\n>. Unlike some other regular expression languages, there |
2186 | 3097 | are no backslashed symbols that aren't alphanumeric. So anything |
2187 | that looks like \\, \(, \), | |
3098 | that looks like C<\\>, C<\(>, C<\)>, C<\[>, C<\]>, C<\{>, or C<\}> is | |
3099 | always | |
2188 | 3100 | interpreted as a literal character, not a metacharacter. This was |
2189 | 3101 | once used in a common idiom to disable or quote the special meanings |
2190 | 3102 | of regular expression metacharacters in a string that you want to |
2191 | 3103 | use for a pattern. Simply quote all non-"word" characters: |
2192 | 3104 | |
2193 | 3105 | =end original |
2194 | 3106 | |
2195 | Perl において | |
3107 | Perl において逆スラッシュで表現されるメタ文字は C<\b>, C<\w>, | |
2196 | 3108 | C<\n> のように英数字です。 |
2197 | 他の正規表現言語とは異なり、英数字でない | |
3109 | 他の正規表現言語とは異なり、英数字でない逆スラッシュ付きシンボルは | |
2198 | 3110 | ありません。 |
2199 | なので \\, \(, \), | |
3111 | なので C<\\>, C<\(>, C<\)>, C<\[>, C<\]>, C<\{>, or C<\}> といったものは | |
2200 | リテラル文字です。 | |
3112 | 全てメタ文字ではなくリテラル文字です。 | |
2201 | 3113 | これはパターンで使いたい文字列の中で正規表現のメタ文字としての特殊な意味を |
2202 | 3114 | 無効化またはクォートするための一般的な指標として使われてきました。 |
2203 | 3115 | 「単語」でない全ての文字は単にクォートします: |
2204 | 3116 | |
2205 | 3117 | $pattern =~ s/(\W)/\\$1/g; |
2206 | 3118 | |
2207 | 3119 | =begin original |
2208 | 3120 | |
2209 | 3121 | (If C<use locale> is set, then this depends on the current locale.) |
2210 | Today it is more common to use the quotemeta() | |
3122 | Today it is more common to use the C<L<quotemeta()|perlfunc/quotemeta>> | |
2211 | metaquoting escape sequence to disable al | |
3123 | function or the C<\Q> metaquoting escape sequence to disable all | |
2212 | meanings like this: | |
3124 | metacharacters' special meanings like this: | |
2213 | 3125 | |
2214 | 3126 | =end original |
2215 | 3127 | |
2216 | 3128 | (もし C<use locale> が有効であれば、これは現在のロケールに依存します。) |
2217 | 3129 | 今日では特殊な意味を持つメタ文字を全て無効にするためには次のように |
2218 | quotemeta() 関数か C<\Q> メタクォート | |
3130 | C<L<quotemeta()|perlfunc/quotemeta>> 関数か C<\Q> メタクォート | |
2219 | より一般的です: | |
3131 | エスケープシーケンスを使うのがより一般的です: | |
2220 | 3132 | |
2221 | 3133 | /$unquoted\Q$quoted\E$unquoted/ |
2222 | 3134 | |
2223 | 3135 | =begin original |
2224 | 3136 | |
2225 | 3137 | Beware that if you put literal backslashes (those not inside |
2226 | 3138 | interpolated variables) between C<\Q> and C<\E>, double-quotish |
2227 | 3139 | backslash interpolation may lead to confusing results. If you |
2228 | 3140 | I<need> to use literal backslashes within C<\Q...\E>, |
2229 | 3141 | consult L<perlop/"Gory details of parsing quoted constructs">. |
2230 | 3142 | |
2231 | 3143 | =end original |
2232 | 3144 | |
2233 | C<\Q> 及び C<\E> の間でリテラルとして | |
3145 | C<\Q> 及び C<\E> の間でリテラルとして逆スラッシュをおくとき | |
2234 | (埋め込んだ変数の中でではない)には、二重にクォートした | |
3146 | (埋め込んだ変数の中でではない)には、二重にクォートした逆スラッシュの | |
2235 | 3147 | 埋め込みは困惑した結果となるでしょう。 |
2236 | もし C<\Q...\E> でリテラルとしての | |
3148 | もし C<\Q...\E> でリテラルとしての逆スラッシュを使う | |
2237 | 3149 | I<必要がある> のなら、 |
2238 | 3150 | L<perlop/"Gory details of parsing quoted constructs"> を参照してください。 |
2239 | 3151 | |
2240 | 3152 | =begin original |
2241 | 3153 | |
2242 | 3154 | C<quotemeta()> and C<\Q> are fully described in L<perlfunc/quotemeta>. |
2243 | 3155 | |
2244 | 3156 | =end original |
2245 | 3157 | |
2246 | 3158 | C<quotemeta()> と C<\Q> は L<perlfunc/quotemeta> に完全に記述されています。 |
2247 | 3159 | |
2248 | 3160 | =head2 Extended Patterns |
2249 | 3161 | |
2250 | 3162 | (拡張パターン) |
2251 | 3163 | |
2252 | 3164 | =begin original |
2253 | 3165 | |
2254 | 3166 | Perl also defines a consistent extension syntax for features not |
2255 | 3167 | found in standard tools like B<awk> and |
2256 | 3168 | B<lex>. The syntax for most of these is a |
2257 | 3169 | pair of parentheses with a question mark as the first thing within |
2258 | 3170 | the parentheses. The character after the question mark indicates |
2259 | 3171 | the extension. |
2260 | 3172 | |
2261 | 3173 | =end original |
2262 | 3174 | |
2263 | 3175 | Perl は B<awk> や B<lex> といった標準的なツールでは見られない機能のための |
2264 | 3176 | 拡張構文も定義しています。 |
2265 | 3177 | これらのほとんどの構文は対のかっことかっこ内の最初に疑問符の形をとります。 |
2266 | 3178 | 疑問符の後の文字で拡張を区別します。 |
2267 | 3179 | |
2268 | 3180 | =begin original |
2269 | 3181 | |
2270 | The stability of these extensions varies widely. Some have been | |
2271 | part of the core language for many years. Others are experimental | |
2272 | and may change without warning or be completely removed. Check | |
2273 | the documentation on an individual feature to verify its current | |
2274 | status. | |
2275 | ||
2276 | =end original | |
2277 | ||
2278 | 拡張構文の安定度は様々です。 | |
2279 | 中には長年言語コアの一部となっている物もあります。 | |
2280 | そうでなく実験的に追加され警告なしに変更されたり削除されるものも | |
2281 | 中にはあります。 | |
2282 | それぞれのステータスに関しては個々の機能のドキュメントを確認してください。 | |
2283 | ||
2284 | =begin original | |
2285 | ||
2286 | 3182 | A question mark was chosen for this and for the minimal-matching |
2287 | 3183 | construct because 1) question marks are rare in older regular |
2288 | 3184 | expressions, and 2) whenever you see one, you should stop and |
2289 | 3185 | "question" exactly what is going on. That's psychology.... |
2290 | 3186 | |
2291 | 3187 | =end original |
2292 | 3188 | |
2293 | 3189 | 疑問符は 1) それが古い正規表現で使われることは稀であること、そして |
2294 | 3190 | 2) それを見かけると何が行われるのか本当に「疑問に」思って止まることから、 |
2295 | 3191 | これのためと最小マッチング構成子のために選ばれました。 |
2296 | 3192 | これが心理学です…。 |
2297 | 3193 | |
2298 | 3194 | =over 4 |
2299 | 3195 | |
2300 | 3196 | =item C<(?#text)> |
2301 | 3197 | X<(?#)> |
2302 | 3198 | |
2303 | 3199 | =begin original |
2304 | 3200 | |
2305 | A comment. The text is ignored. | |
3201 | A comment. The text is ignored. | |
2306 | ||
3202 | Note that Perl closes | |
2307 | the comment as soon as it sees a C<)>, so there is no way to put a literal | |
3203 | the comment as soon as it sees a C<")">, so there is no way to put a literal | |
2308 | C<)> in the comment. | |
3204 | C<")"> in the comment. The pattern's closing delimiter must be escaped by | |
3205 | a backslash if it appears in the comment. | |
2309 | 3206 | |
2310 | 3207 | =end original |
2311 | 3208 | |
2312 | 3209 | コメント。 |
2313 | 3210 | テキストは無視されます。 |
2314 | C< | |
3211 | Perl は C<")"> を見つけると直ぐにコメントを閉じる点に注意してください; | |
2315 | ||
3212 | この為リテラル C<")"> をコメント中におくことはできません。 | |
2316 | ||
3213 | パターンの閉じ区切り文字がコメントに見えるようなものなら、 | |
3214 | 逆スラッシュでエスケープしなければなりません。 | |
2317 | 3215 | |
2318 | = | |
3216 | =begin original | |
2319 | 3217 | |
2320 | ||
3218 | See L</E<sol>x> for another way to have comments in patterns. | |
3219 | ||
3220 | =end original | |
3221 | ||
3222 | パターンの中にコメントを入れるもう一つの方法については | |
3223 | L</E<sol>x> を参照してください。 | |
3224 | ||
3225 | =begin original | |
3226 | ||
3227 | Note that a comment can go just about anywhere, except in the middle of | |
3228 | an escape sequence. Examples: | |
3229 | ||
3230 | =end original | |
3231 | ||
3232 | コメントは、エスケープシーケンスの途中を除いて、 | |
3233 | どこにでも入れることができることに注意してください。 | |
3234 | 例: | |
3235 | ||
3236 | qr/foo(?#comment)bar/' # Matches 'foobar' | |
3237 | ||
3238 | # The pattern below matches 'abcd', 'abccd', or 'abcccd' | |
3239 | qr/abc(?#comment between literal and its quantifier){1,3}d/ | |
3240 | ||
3241 | # The pattern below generates a syntax error, because the '\p' must | |
3242 | # be followed immediately by a '{'. | |
3243 | qr/\p(?#comment between \p and its property name){Any}/ | |
3244 | ||
3245 | # The pattern below generates a syntax error, because the initial | |
3246 | # '\(' is a literal opening parenthesis, and so there is nothing | |
3247 | # for the closing ')' to match | |
3248 | qr/\(?#the backslash means this isn't a comment)p{Any}/ | |
3249 | ||
3250 | # Comments can be used to fold long patterns into multiple lines | |
3251 | qr/First part of a long regex(?# | |
3252 | )remaining part/ | |
3253 | ||
3254 | =item C<(?adlupimnsx-imnsx)> | |
3255 | ||
3256 | =item C<(?^alupimnsx)> | |
2321 | 3257 | X<(?)> X<(?^)> |
2322 | 3258 | |
2323 | 3259 | =begin original |
2324 | 3260 | |
2325 | 3261 | One or more embedded pattern-match modifiers, to be turned on (or |
2326 | turned off | |
3262 | turned off if preceded by C<"-">) for the remainder of the pattern or | |
2327 | 3263 | the remainder of the enclosing pattern group (if any). |
2328 | 3264 | |
2329 | 3265 | =end original |
2330 | 3266 | |
2331 | 3267 | 一つもしくは複数のパターンマッチング修飾子; |
2332 | 3268 | パターンの残りまたは(もしあれば)包含しているパターングループの残りで |
2333 | 有効にする(または C<-> が前置されていれば解除する)。 | |
3269 | 有効にする(または C<"-"> が前置されていれば解除する)。 | |
2334 | 3270 | |
2335 | 3271 | =begin original |
2336 | 3272 | |
2337 | This is particularly useful for dynamic patterns, | |
3273 | This is particularly useful for dynamically-generated patterns, | |
3274 | such as those read in from a | |
2338 | 3275 | configuration file, taken from an argument, or specified in a table |
2339 | 3276 | somewhere. Consider the case where some patterns want to be |
2340 | 3277 | case-sensitive and some do not: The case-insensitive ones merely need to |
2341 | 3278 | include C<(?i)> at the front of the pattern. For example: |
2342 | 3279 | |
2343 | 3280 | =end original |
2344 | 3281 | |
2345 | 3282 | これは設定ファイルから読む、引数から取る、どこかのテーブルで |
2346 | 指定されている箇所から | |
3283 | 指定されている箇所から動的生成されたパターンを使うときに特に便利です。 | |
2347 | 3284 | パターンの一部では大文字小文字を区別したいけれども別の箇所では |
2348 | 3285 | 区別しないといったケースを考えてみます: 区別をしない場所では |
2349 | 3286 | 単にパターンの先頭に C<(?i)> を含めるだけです。 |
2350 | 3287 | 例えば: |
2351 | 3288 | |
2352 | 3289 | $pattern = "foobar"; |
2353 | 3290 | if ( /$pattern/i ) { } |
2354 | 3291 | |
2355 | 3292 | # more flexible: |
2356 | 3293 | |
2357 | 3294 | $pattern = "(?i)foobar"; |
2358 | 3295 | if ( /$pattern/ ) { } |
2359 | 3296 | |
2360 | 3297 | =begin original |
2361 | 3298 | |
2362 | 3299 | These modifiers are restored at the end of the enclosing group. For example, |
2363 | 3300 | |
2364 | 3301 | =end original |
2365 | 3302 | |
2366 | 3303 | これらの修飾子は包含しているグループの最後で復元(restore)されます。 |
2367 | 3304 | 例えば、 |
2368 | 3305 | |
2369 | 3306 | ( (?i) blah ) \s+ \g1 |
2370 | 3307 | |
2371 | 3308 | =begin original |
2372 | 3309 | |
2373 | 3310 | will match C<blah> in any case, some spaces, and an exact (I<including the case>!) |
2374 | 3311 | repetition of the previous word, assuming the C</x> modifier, and no C</i> |
2375 | 3312 | modifier outside this group. |
2376 | 3313 | |
2377 | 3314 | =end original |
2378 | 3315 | |
2379 | 3316 | は C<blah> に大文字小文字の区別なくマッチングし、 |
2380 | 3317 | いくつかの空白、そして前の単語その物(I<大文字小文字の区別まで含めて>!)に |
2381 | 3318 | 再度マッチングします; ここではこのグループの外側で C</x> 修飾子を持ち、 |
2382 | 3319 | C</i> 修飾子を持たないものとします。 |
2383 | 3320 | |
2384 | 3321 | =begin original |
2385 | 3322 | |
2386 | 3323 | These modifiers do not carry over into named subpatterns called in the |
2387 | 3324 | enclosing group. In other words, a pattern such as C<((?i)(?&NAME))> does not |
2388 | change the case-sensitivity of the "NAME" pattern. | |
3325 | change the case-sensitivity of the C<"NAME"> pattern. | |
2389 | 3326 | |
2390 | 3327 | =end original |
2391 | 3328 | |
2392 | 3329 | これらの修飾子は囲まれたグループで呼び出された名前付き部分パターンには |
2393 | 3330 | 持ち越されません。 |
2394 | 言い換えると、C<((?i)(?&NAME))> のようなパターンは、 | |
3331 | 言い換えると、C<((?i)(?&NAME))> のようなパターンは、 | |
2395 | 大文字小文字を認識するのを変更しません。 | |
3332 | C<"NAME"> パターンが大文字小文字を認識するのを変更しません。 | |
2396 | 3333 | |
2397 | 3334 | =begin original |
2398 | 3335 | |
3336 | A modifier is overridden by later occurrences of this construct in the | |
3337 | same scope containing the same modifier, so that | |
3338 | ||
3339 | =end original | |
3340 | ||
3341 | 修飾子は、同じスコープ内で同じ修飾子を含む | |
3342 | この構文が後に出現すると上書きされるので: | |
3343 | ||
3344 | /((?im)foo(?-m)bar)/ | |
3345 | ||
3346 | =begin original | |
3347 | ||
3348 | matches all of C<foobar> case insensitively, but uses C</m> rules for | |
3349 | only the C<foo> portion. The C<"a"> flag overrides C<aa> as well; | |
3350 | likewise C<aa> overrides C<"a">. The same goes for C<"x"> and C<xx>. | |
3351 | Hence, in | |
3352 | ||
3353 | =end original | |
3354 | ||
3355 | は、C<foobar> のすべてに大文字小文字を区別せずにマッチングしますが、 | |
3356 | C<foo> 部分のみに C</m> の規則を使います。 | |
3357 | C<"a"> フラグは C<aa> も上書きします; | |
3358 | 同様に、C<aa> も C<"a">を上書きします。 | |
3359 | 同じことが C<"x"> と C<xx> についても当てはまります。 | |
3360 | したがって: | |
3361 | ||
3362 | /(?-x)foo/xx | |
3363 | ||
3364 | =begin original | |
3365 | ||
3366 | both C</x> and C</xx> are turned off during matching C<foo>. And in | |
3367 | ||
3368 | =end original | |
3369 | ||
3370 | C</x> と C</xx> は両方とも、C<foo> のマッチング中はオフになります。 | |
3371 | そして: | |
3372 | ||
3373 | /(?x)foo/x | |
3374 | ||
3375 | =begin original | |
3376 | ||
3377 | C</x> but NOT C</xx> is turned on for matching C<foo>. (One might | |
3378 | mistakenly think that since the inner C<(?x)> is already in the scope of | |
3379 | C</x>, that the result would effectively be the sum of them, yielding | |
3380 | C</xx>. It doesn't work that way.) Similarly, doing something like | |
3381 | C<(?xx-x)foo> turns off all C<"x"> behavior for matching C<foo>, it is not | |
3382 | that you subtract 1 C<"x"> from 2 to get 1 C<"x"> remaining. | |
3383 | ||
3384 | =end original | |
3385 | ||
3386 | C<foo> にマッチングする間、C</xx> ではなく C</x> がオンになります。 | |
3387 | (内部 C<(?x)> はすでに C</x> の範囲内にあるので、結果は実質的に | |
3388 | それらの和になり、C</xx> が生成されると誤って考えるかもしれません。 | |
3389 | そうはなりません。) | |
3390 | 同様に、C<(?xx-x)foo> のようなことをすると、C<foo> とマッチングする間 | |
3391 | C<"x"> の動作がすべてオフになります; | |
3392 | 二つの C<"x"> から一つ引いて一つの C<"x"> が残るということにはなりません。 | |
3393 | ||
3394 | =begin original | |
3395 | ||
2399 | 3396 | Any of these modifiers can be set to apply globally to all regular |
2400 | 3397 | expressions compiled within the scope of a C<use re>. See |
2401 | 3398 | L<re/"'/flags' mode">. |
2402 | 3399 | |
2403 | 3400 | =end original |
2404 | 3401 | |
2405 | 3402 | それらの変更のどれもセットでき、C<use re>のスコープ内でグローバルに |
2406 | 3403 | 全てのコンパイルされた正規表現に適用されます。L<re/"'/flags' mode"> |
2407 | 3404 | を見てください。 |
2408 | 3405 | |
2409 | 3406 | =begin original |
2410 | 3407 | |
2411 | 3408 | Starting in Perl 5.14, a C<"^"> (caret or circumflex accent) immediately |
2412 | after the C<"?"> is a shorthand equivalent to C<d-imsx>. Flags (except | |
3409 | after the C<"?"> is a shorthand equivalent to C<d-imnsx>. Flags (except | |
2413 | 3410 | C<"d">) may follow the caret to override it. |
2414 | 3411 | But a minus sign is not legal with it. |
2415 | 3412 | |
2416 | 3413 | =end original |
2417 | 3414 | |
2418 | 3415 | Perl 5.14から、C<"^">(キャレットか曲折アクセント)がC<"?">のすぐ |
2419 | 後ろにつくと、C<d-imsx>と同じになります。フラグ(C<"d">以外の) | |
3416 | 後ろにつくと、C<d-imnsx> と同じになります。フラグ(C<"d">以外の) | |
2420 | 3417 | をキャレットに続けることで、上書きできます。 |
2421 | 3418 | ですが、マイナス記号は一緒に使えません。 |
2422 | 3419 | |
2423 | 3420 | =begin original |
2424 | 3421 | |
2425 | Note that the C<a>, C<d>, C<l>, C<p>, and C<u> modifiers are special in | |
3422 | Note that the C<"a">, C<"d">, C<"l">, C<"p">, and C<"u"> modifiers are special in | |
2426 | that they can only be enabled, not disabled, and the C<a>, C<d>, C<l>, and | |
3423 | that they can only be enabled, not disabled, and the C<"a">, C<"d">, C<"l">, and | |
2427 | C<u> modifiers are mutually exclusive: specifying one de-specifies the | |
3424 | C<"u"> modifiers are mutually exclusive: specifying one de-specifies the | |
2428 | others, and a maximum of one (or two C<a>'s) may appear in the | |
3425 | others, and a maximum of one (or two C<"a">'s) may appear in the | |
2429 | 3426 | construct. Thus, for |
2430 | 3427 | example, C<(?-p)> will warn when compiled under C<use warnings>; |
2431 | 3428 | C<(?-d:...)> and C<(?dl:...)> are fatal errors. |
2432 | 3429 | |
2433 | 3430 | =end original |
2434 | 3431 | |
2435 | C<a>, C<d>, C<l>, C<p>, C<u> 修飾子は有効にできるのみで | |
3432 | C<"a">, C<"d">, C<"l">, C<"p">, C<"u"> 修飾子は有効にできるのみで、 | |
2436 | そして C<a>, C<d>, C<l>, C<u> 修飾子は | |
3433 | 無効にはできない点、 そして C<"a">, C<"d">, C<"l">, C<"u"> 修飾子は | |
3434 | 互いに排他であるという点で特別です: | |
2437 | 3435 | 一つを指定すると他のものの指定を解除し、構文中に最大で一つ (または二つの |
2438 | C<a>) だけが現れます。 | |
3436 | C<"a">) だけが現れます。 | |
2439 | 3437 | 従って 例えば C<(?-p)> は C<use warnings> の下でコンパイルされると |
2440 | 3438 | 警告を発します; C<(?-d:...)> と C<(?dl:...)> は致命的エラーです。 |
2441 | 3439 | |
2442 | 3440 | =begin original |
2443 | 3441 | |
2444 | Note also that the C<p> modifier is special in that its presence | |
3442 | Note also that the C<"p"> modifier is special in that its presence | |
2445 | 3443 | anywhere in a pattern has a global effect. |
2446 | 3444 | |
2447 | 3445 | =end original |
2448 | 3446 | |
2449 | 3447 | パターン中のどこにあってもグローバルな影響があるという意味で |
2450 | C<p> 修飾子が特別であることにも注意してください。 | |
3448 | C<"p"> 修飾子が特別であることにも注意してください。 | |
2451 | 3449 | |
2452 | 3450 | =item C<(?:pattern)> |
2453 | 3451 | X<(?:)> |
2454 | 3452 | |
2455 | =item C<(?adluimsx-imsx:pattern)> | |
3453 | =item C<(?adluimnsx-imnsx:pattern)> | |
2456 | 3454 | |
2457 | =item C<(?^aluimsx:pattern)> | |
3455 | =item C<(?^aluimnsx:pattern)> | |
2458 | 3456 | X<(?^:)> |
2459 | 3457 | |
2460 | 3458 | =begin original |
2461 | 3459 | |
2462 | 3460 | This is for clustering, not capturing; it groups subexpressions like |
2463 | "()", but doesn't make backreferences as "()" does. So | |
3461 | C<"()">, but doesn't make backreferences as C<"()"> does. So | |
2464 | 3462 | |
2465 | 3463 | =end original |
2466 | 3464 | |
2467 | これはキャプチャではなくクラスタです; これは "()" のように部分式を | |
3465 | これはキャプチャではなくクラスタです; これは C<"()"> のように部分式を | |
2468 | グループ化しますが "()" が行うような後方参照は行いません。 | |
3466 | グループ化しますが C<"()"> が行うような後方参照は行いません。 | |
2469 | 3467 | つまり、 |
2470 | 3468 | |
2471 | 3469 | @fields = split(/\b(?:a|b|c)\b/) |
2472 | 3470 | |
2473 | 3471 | =begin original |
2474 | 3472 | |
2475 | ||
3473 | matches the same field delimiters as | |
2476 | 3474 | |
2477 | 3475 | =end original |
2478 | 3476 | |
2479 | ||
3477 | 次と同じフィールド区切り文字にマッチングしますが: | |
2480 | 3478 | |
2481 | 3479 | @fields = split(/\b(a|b|c)\b/) |
2482 | 3480 | |
2483 | 3481 | =begin original |
2484 | 3482 | |
2485 | but doesn't spit out | |
3483 | but doesn't spit out the delimiters themselves as extra fields (even though | |
3484 | that's the behaviour of L<perlfunc/split> when its pattern contains capturing | |
3485 | groups). It's also cheaper not to capture | |
2486 | 3486 | characters if you don't need to. |
2487 | 3487 | |
2488 | 3488 | =end original |
2489 | 3489 | |
2490 | ||
3490 | (例えそれが捕捉グループを含むときの L<perlfunc/split> の振る舞いで | |
3491 | あったとしても) 区切り文字自身を余計なフィールドとして引き出しません。 | |
2491 | 3492 | また不要であれば文字のキャプチャを行わないため低コストです。 |
2492 | 3493 | |
2493 | 3494 | =begin original |
2494 | 3495 | |
2495 | Any letters between C<?> and C<:> act as flags modifiers as with | |
3496 | Any letters between C<"?"> and C<":"> act as flags modifiers as with | |
2496 | C<(?adluimsx-imsx)>. For example, | |
3497 | C<(?adluimnsx-imnsx)>. For example, | |
2497 | 3498 | |
2498 | 3499 | =end original |
2499 | 3500 | |
2500 | C<?> 及び C<:> の間の文字は C<(?adluimsx-imsx)> のような | |
3501 | C<"?"> 及び C<":"> の間の文字は C<(?adluimnsx-imnsx)> のような | |
2501 | 動作します。 | |
3502 | フラグ修飾子として動作します。 | |
2502 | 3503 | 例えば、 |
2503 | 3504 | |
2504 | 3505 | /(?s-i:more.*than).*million/i |
2505 | 3506 | |
2506 | 3507 | =begin original |
2507 | 3508 | |
2508 | 3509 | is equivalent to the more verbose |
2509 | 3510 | |
2510 | 3511 | =end original |
2511 | 3512 | |
2512 | 3513 | はより冗長に書けば以下と等価です |
2513 | 3514 | |
2514 | 3515 | /(?:(?s-i)more.*than).*million/i |
2515 | 3516 | |
2516 | 3517 | =begin original |
2517 | 3518 | |
3519 | Note that any C<()> constructs enclosed within this one will still | |
3520 | capture unless the C</n> modifier is in effect. | |
3521 | ||
3522 | =end original | |
3523 | ||
3524 | これの中の C<()> 構文は、C</n> が有効でない限りまだ捕捉することに | |
3525 | 注意してください。 | |
3526 | ||
3527 | =begin original | |
3528 | ||
3529 | Like the L</(?adlupimnsx-imnsx)> construct, C<aa> and C<"a"> override each | |
3530 | other, as do C<xx> and C<"x">. They are not additive. So, doing | |
3531 | something like C<(?xx-x:foo)> turns off all C<"x"> behavior for matching | |
3532 | C<foo>. | |
3533 | ||
3534 | =end original | |
3535 | ||
3536 | L</(?adlupimnsx-imnsx)> 構文と同様、C<xx> と | |
3537 | C<"x">と同様に、C<aa> と C<"a"> は互いに上書きします。 | |
3538 | これらは加法的ではありません。 | |
3539 | したがって、C<(?xx-x:foo)> のようなことを行うと、 | |
3540 | C<foo> にマッチングする間、C<"x"> の動作がすべてオフになります。 | |
3541 | ||
3542 | =begin original | |
3543 | ||
2518 | 3544 | Starting in Perl 5.14, a C<"^"> (caret or circumflex accent) immediately |
2519 | after the C<"?"> is a shorthand equivalent to C<d-imsx>. Any positive | |
3545 | after the C<"?"> is a shorthand equivalent to C<d-imnsx>. Any positive | |
2520 | 3546 | flags (except C<"d">) may follow the caret, so |
2521 | 3547 | |
2522 | 3548 | =end original |
2523 | 3549 | |
2524 | Perl 5.14から、C<"^">(キャレット | |
3550 | Perl 5.14 から、C<"^">(キャレットあるいは曲折アクセント)が C<"?"> のすぐ | |
2525 | 後ろにつくと、C<d-imsx>と同じになります。 | |
3551 | 後ろにつくと、C<d-imnsx> と同じになります。 | |
2526 | どのような肯定のフラグ(C<"d">以外の)もキャレットに続けることができます; | |
3552 | どのような肯定のフラグ(C<"d"> 以外の)もキャレットに続けることができます; | |
2527 | 3553 | そのため、 |
2528 | 3554 | |
2529 | 3555 | (?^x:foo) |
2530 | 3556 | |
2531 | 3557 | =begin original |
2532 | 3558 | |
2533 | 3559 | is equivalent to |
2534 | 3560 | |
2535 | 3561 | =end original |
2536 | 3562 | |
2537 | 3563 | は、以下と同じになります。 |
2538 | 3564 | |
2539 | (?x-ims:foo) | |
3565 | (?x-imns:foo) | |
2540 | 3566 | |
2541 | 3567 | =begin original |
2542 | 3568 | |
2543 | 3569 | The caret tells Perl that this cluster doesn't inherit the flags of any |
2544 | surrounding pattern, but uses the system defaults (C<d-imsx>), | |
3570 | surrounding pattern, but uses the system defaults (C<d-imnsx>), | |
2545 | 3571 | modified by any flags specified. |
2546 | 3572 | |
2547 | 3573 | =end original |
2548 | 3574 | |
2549 | 3575 | キャレットは、Perlにこのクラスターはパターンの周りのどのフラグも |
2550 | 引き継がずに、代わりに、システムのデフォルトのフラグ(C<d-imsx>) | |
3576 | 引き継がずに、代わりに、システムのデフォルトのフラグ (C<d-imnsx>) | |
2551 | 3577 | を使うことを教えます; 指定されている他のフラグによって変更されます。 |
2552 | 3578 | |
2553 | 3579 | =begin original |
2554 | 3580 | |
2555 | 3581 | The caret allows for simpler stringification of compiled regular |
2556 | 3582 | expressions. These look like |
2557 | 3583 | |
2558 | 3584 | =end original |
2559 | 3585 | |
2560 | 3586 | キャレットはより簡単なコンパイルされた正規表現の文字列化をすることが |
2561 | 3587 | できます。次のものは |
2562 | 3588 | |
2563 | 3589 | (?^:pattern) |
2564 | 3590 | |
2565 | 3591 | =begin original |
2566 | 3592 | |
2567 | 3593 | with any non-default flags appearing between the caret and the colon. |
2568 | 3594 | A test that looks at such stringification thus doesn't need to have the |
2569 | 3595 | system default flags hard-coded in it, just the caret. If new flags are |
2570 | 3596 | added to Perl, the meaning of the caret's expansion will change to include |
2571 | 3597 | the default for those flags, so the test will still work, unchanged. |
2572 | 3598 | |
2573 | 3599 | =end original |
2574 | 3600 | |
2575 | 3601 | キャレットとコロンの間には、デフォルトでないフラグがありません。 |
2576 | 3602 | このような文字列化を見るテストには、したがって、システムのデフォルトのフラグを |
2577 | 3603 | その中にハードコードする必要はなく、ただキャレットを使います。新しいフラグが |
2578 | 3604 | Perlに追加されたら、キャレットを展開した意味はそれらのフラグのためのデフォルトを |
2579 | 3605 | 含むように変更されます; そのために、このテストは、それでも何も変えずに |
2580 | 3606 | 動くでしょう。 |
2581 | 3607 | |
2582 | 3608 | =begin original |
2583 | 3609 | |
2584 | 3610 | Specifying a negative flag after the caret is an error, as the flag is |
2585 | 3611 | redundant. |
2586 | 3612 | |
2587 | 3613 | =end original |
2588 | 3614 | |
2589 | 3615 | キャレットの後ろに否定のフラグを付けると、余計なフラグであるとして、 |
2590 | 3616 | エラーになります。 |
2591 | 3617 | |
2592 | 3618 | =begin original |
2593 | 3619 | |
2594 | 3620 | Mnemonic for C<(?^...)>: A fresh beginning since the usual use of a caret is |
2595 | 3621 | to match at the beginning. |
2596 | 3622 | |
2597 | 3623 | =end original |
2598 | 3624 | |
2599 | 3625 | C<(?^...)>の覚え方: 新鮮な開始; 通常のキャレットの使い方は文字列の開始の |
2600 | 3626 | マッチなので。 |
2601 | 3627 | |
2602 | 3628 | =item C<(?|pattern)> |
2603 | 3629 | X<(?|)> X<Branch reset> |
2604 | 3630 | |
2605 | 3631 | =begin original |
2606 | 3632 | |
2607 | 3633 | This is the "branch reset" pattern, which has the special property |
2608 | 3634 | that the capture groups are numbered from the same starting point |
2609 | 3635 | in each alternation branch. It is available starting from perl 5.10.0. |
2610 | 3636 | |
2611 | 3637 | =end original |
2612 | 3638 | |
2613 | 3639 | これは各代替分岐において捕捉グループを同じ番号から始める特殊な |
2614 | 3640 | 属性を持っている、「ブランチリセット(branch reset)」パターンです。 |
2615 | 3641 | これは perl 5.10.0 から提供されています。 |
2616 | 3642 | |
2617 | 3643 | =begin original |
2618 | 3644 | |
2619 | 3645 | Capture groups are numbered from left to right, but inside this |
2620 | 3646 | construct the numbering is restarted for each branch. |
2621 | 3647 | |
2622 | 3648 | =end original |
2623 | 3649 | |
2624 | 3650 | 捕捉グループは左から右へと番号が振られますが、この構成子の内側では |
2625 | 3651 | 各分岐毎に番号はリセットされます。 |
2626 | 3652 | |
2627 | 3653 | =begin original |
2628 | 3654 | |
2629 | 3655 | The numbering within each branch will be as normal, and any groups |
2630 | 3656 | following this construct will be numbered as though the construct |
2631 | 3657 | contained only one branch, that being the one with the most capture |
2632 | 3658 | groups in it. |
2633 | 3659 | |
2634 | 3660 | =end original |
2635 | 3661 | |
2636 | 3662 | 各分岐内での番号付けは通常通りに行われ、この構成子の後に続くグループは |
2637 | 3663 | その中で捕捉グループが一番多かった分岐のみが |
2638 | 3664 | 格納されていたかのように番号付けされていきます。 |
2639 | 3665 | |
2640 | 3666 | =begin original |
2641 | 3667 | |
2642 | 3668 | This construct is useful when you want to capture one of a |
2643 | 3669 | number of alternative matches. |
2644 | 3670 | |
2645 | 3671 | =end original |
2646 | 3672 | |
2647 | 3673 | この構成子はいくつかの代替マッチングの1つを捕捉したいときに便利です。 |
2648 | 3674 | |
2649 | 3675 | =begin original |
2650 | 3676 | |
2651 | 3677 | Consider the following pattern. The numbers underneath show in |
2652 | 3678 | which group the captured content will be stored. |
2653 | 3679 | |
2654 | 3680 | =end original |
2655 | 3681 | |
2656 | 3682 | 以下のパターンを想像してみてください。 |
2657 | 3683 | 下側の番号は内容の格納されるグループを示します。 |
2658 | 3684 | |
2659 | # before ---------------branch-reset----------- after | |
3685 | # before ---------------branch-reset----------- after | |
2660 | 3686 | / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
2661 | # 1 2 2 3 2 3 4 | |
3687 | # 1 2 2 3 2 3 4 | |
2662 | 3688 | |
2663 | 3689 | =begin original |
2664 | 3690 | |
2665 | Be careful when using the branch reset pattern in combination with | |
3691 | Be careful when using the branch reset pattern in combination with | |
2666 | named captures. Named captures are implemented as being aliases to | |
3692 | named captures. Named captures are implemented as being aliases to | |
2667 | 3693 | numbered groups holding the captures, and that interferes with the |
2668 | 3694 | implementation of the branch reset pattern. If you are using named |
2669 | 3695 | captures in a branch reset pattern, it's best to use the same names, |
2670 | 3696 | in the same order, in each of the alternations: |
2671 | 3697 | |
2672 | 3698 | =end original |
2673 | 3699 | |
2674 | 3700 | 名前付き捕捉と枝リセットパターンを組み合わせて使うときには注意してください。 |
2675 | 3701 | 名前付き捕捉は捕捉を保持している番号付きグループへの別名として |
2676 | 3702 | 実装されていて、枝リセットパターンの実装を妨害します。 |
2677 | 3703 | 枝リセットパターンで名前付き捕捉を使うときは、それぞれの代替で |
2678 | 3704 | 同じ名前を同じ順番で使うのが最良です: |
2679 | 3705 | |
2680 | 3706 | /(?| (?<a> x ) (?<b> y ) |
2681 | 3707 | | (?<a> z ) (?<b> w )) /x |
2682 | 3708 | |
2683 | 3709 | =begin original |
2684 | 3710 | |
2685 | 3711 | Not doing so may lead to surprises: |
2686 | 3712 | |
2687 | 3713 | =end original |
2688 | 3714 | |
2689 | 3715 | そうしないと驚くことになります: |
2690 | 3716 | |
2691 | 3717 | "12" =~ /(?| (?<a> \d+ ) | (?<b> \D+))/x; |
2692 | say $+ | |
3718 | say $+{a}; # Prints '12' | |
2693 | say $+ | |
3719 | say $+{b}; # *Also* prints '12'. | |
2694 | 3720 | |
2695 | 3721 | =begin original |
2696 | 3722 | |
2697 | 3723 | The problem here is that both the group named C<< a >> and the group |
2698 | 3724 | named C<< b >> are aliases for the group belonging to C<< $1 >>. |
2699 | 3725 | |
2700 | 3726 | =end original |
2701 | 3727 | |
2702 | 3728 | ここでの問題は、C<< a >> という名前のグループと C<< b >> という名前の |
2703 | 3729 | が両方ともグループ C<< $1 >> への別名であることです。 |
2704 | 3730 | |
2705 | =item Look | |
3731 | =item Lookaround Assertions | |
2706 | 3732 | X<look-around assertion> X<lookaround assertion> X<look-around> X<lookaround> |
2707 | 3733 | |
2708 | 3734 | =begin original |
2709 | 3735 | |
2710 | Look | |
3736 | Lookaround assertions are zero-width patterns which match a specific | |
2711 | 3737 | pattern without including it in C<$&>. Positive assertions match when |
2712 | 3738 | their subpattern matches, negative assertions match when their subpattern |
2713 | fails. Look | |
3739 | fails. Lookbehind matches text up to the current match position, | |
2714 | look | |
3740 | lookahead matches text following the current match position. | |
2715 | 3741 | |
2716 | 3742 | =end original |
2717 | 3743 | |
2718 | 3744 | 先読み及び後読みの言明(assertion)は C<$&> の中に |
2719 | 3745 | 含めない特定のパターンにマッチングするゼロ幅のパターンです。 |
2720 | 3746 | 正の言明はその部分パターンがマッチングしたときにマッチングし、 |
2721 | 3747 | 負の言明はその部分パターンが失敗したときにマッチングします。 |
2722 | 3748 | 後読みのマッチングは今のマッチング位置までのテキストにマッチングし、 |
2723 | 3749 | 先読みの言明は今のマッチング位置の先にあるテキストにマッチングします。 |
2724 | 3750 | |
2725 | 3751 | =over 4 |
2726 | 3752 | |
2727 | 3753 | =item C<(?=pattern)> |
2728 | X<(?=)> X<look-ahead, positive> X<lookahead, positive> | |
2729 | 3754 | |
3755 | =item C<(*pla:pattern)> | |
3756 | ||
3757 | =item C<(*positive_lookahead:pattern)> | |
3758 | X<(?=)> | |
3759 | X<(*pla> | |
3760 | X<(*positive_lookahead> | |
3761 | X<look-ahead, positive> X<lookahead, positive> | |
3762 | ||
2730 | 3763 | =begin original |
2731 | 3764 | |
2732 | A zero-width positive look | |
3765 | A zero-width positive lookahead assertion. For example, C</\w+(?=\t)/> | |
2733 | 3766 | matches a word followed by a tab, without including the tab in C<$&>. |
2734 | 3767 | |
2735 | 3768 | =end original |
2736 | 3769 | |
2737 | 3770 | ゼロ幅の正の先読み言明。 |
2738 | 3771 | 例えば、C</\w+(?=\t)/> はタブが続く単語にマッチングしますが、タブは |
2739 | 3772 | C<$&> に含まれません。 |
2740 | 3773 | |
3774 | =begin original | |
3775 | ||
3776 | The alphabetic forms are experimental; using them yields a warning in the | |
3777 | C<experimental::alpha_assertions> category. | |
3778 | ||
3779 | =end original | |
3780 | ||
3781 | 英字形式は実験的です; これらを使うと | |
3782 | C<experimental::alpha_assertions> カテゴリの警告が出ます。 | |
3783 | ||
2741 | 3784 | =item C<(?!pattern)> |
2742 | X<(?!)> X<look-ahead, negative> X<lookahead, negative> | |
2743 | 3785 | |
3786 | =item C<(*nla:pattern)> | |
3787 | ||
3788 | =item C<(*negative_lookahead:pattern)> | |
3789 | X<(?!)> | |
3790 | X<(*nla> | |
3791 | X<(*negative_lookahead> | |
3792 | X<look-ahead, negative> X<lookahead, negative> | |
3793 | ||
2744 | 3794 | =begin original |
2745 | 3795 | |
2746 | A zero-width negative look | |
3796 | A zero-width negative lookahead assertion. For example C</foo(?!bar)/> | |
2747 | 3797 | matches any occurrence of "foo" that isn't followed by "bar". Note |
2748 | however that look | |
3798 | however that lookahead and lookbehind are NOT the same thing. You cannot | |
2749 | use this for look | |
3799 | use this for lookbehind. | |
2750 | 3800 | |
2751 | 3801 | =end original |
2752 | 3802 | |
2753 | 3803 | ゼロ幅の負の先読み言明。 |
2754 | 3804 | 例えば C</foo(?!bar)/> は "bar" が続かない全ての "foo" にマッチングします。 |
2755 | 3805 | しかしながら先読みと後読みは同じ物では ない点に注意してください。 |
2756 | 3806 | これを後読みに使うことはできません。 |
2757 | 3807 | |
2758 | 3808 | =begin original |
2759 | 3809 | |
2760 | 3810 | If you are looking for a "bar" that isn't preceded by a "foo", C</(?!foo)bar/> |
2761 | 3811 | will not do what you want. That's because the C<(?!foo)> is just saying that |
2762 | 3812 | the next thing cannot be "foo"--and it's not, it's a "bar", so "foobar" will |
2763 | match. Use look | |
3813 | match. Use lookbehind instead (see below). | |
2764 | 3814 | |
2765 | 3815 | =end original |
2766 | 3816 | |
2767 | 3817 | もし "foo" が前にない "bar" を探しているのなら、 |
2768 | 3818 | C</(?!foo)bar/> では欲しい物にはなりません。 |
2769 | 3819 | なぜなら C<(?!foo)> は次のものが "foo" ではないとだけいっているのです -- |
2770 | 3820 | そしてそうではなく、そこには "bar" があるので、"foobar" はマッチングします。 |
2771 | 3821 | (後述の) 後方参照を使ってください。 |
2772 | 3822 | |
2773 | = | |
3823 | =begin original | |
2774 | X<(?<=)> X<look-behind, positive> X<lookbehind, positive> X<\K> | |
2775 | 3824 | |
3825 | The alphabetic forms are experimental; using them yields a warning in the | |
3826 | C<experimental::alpha_assertions> category. | |
3827 | ||
3828 | =end original | |
3829 | ||
3830 | 英字形式は実験的です; これらを使うと | |
3831 | C<experimental::alpha_assertions> カテゴリの警告が出ます。 | |
3832 | ||
3833 | =item C<(?<=pattern)> | |
3834 | ||
3835 | =item C<\K> | |
3836 | ||
3837 | =item C<(*plb:pattern)> | |
3838 | ||
3839 | =item C<(*positive_lookbehind:pattern)> | |
3840 | X<(?<=)> | |
3841 | X<(*plb> | |
3842 | X<(*positive_lookbehind> | |
3843 | X<look-behind, positive> X<lookbehind, positive> X<\K> | |
3844 | ||
2776 | 3845 | =begin original |
2777 | 3846 | |
2778 | A zero-width positive look | |
3847 | A zero-width positive lookbehind assertion. For example, C</(?<=\t)\w+/> | |
2779 | 3848 | matches a word that follows a tab, without including the tab in C<$&>. |
2780 | Works only for fixed-width look | |
3849 | Works only for fixed-width lookbehind. | |
2781 | 3850 | |
2782 | 3851 | =end original |
2783 | 3852 | |
2784 | 3853 | ゼロ幅の正の後読みの言明。 |
2785 | 3854 | 例えば、C</(?<=\t)\w+/> は |
2786 | 3855 | タブに続く単語にマッチングしますが、タブは $& に 含まれません。 |
2787 | 3856 | 固定幅の後読みのみが動作します。 |
2788 | 3857 | |
2789 | 3858 | =begin original |
2790 | 3859 | |
2791 | There is a special form of this construct, called C<\K> | |
3860 | There is a special form of this construct, called C<\K> (available since | |
3861 | Perl 5.10.0), which causes the | |
2792 | 3862 | regex engine to "keep" everything it had matched prior to the C<\K> and |
2793 | 3863 | not include it in C<$&>. This effectively provides variable-length |
2794 | look | |
3864 | lookbehind. The use of C<\K> inside of another lookaround assertion | |
2795 | 3865 | is allowed, but the behaviour is currently not well defined. |
2796 | 3866 | |
2797 | 3867 | =end original |
2798 | 3868 | |
2799 | C<\K> というこの構成子の特殊な形式もあります | |
3869 | (Perl 5.10.0 から利用可能な) C<\K> というこの構成子の特殊な形式もあります; | |
2800 | 3870 | これは正規表現エンジンに対してそれが C<\K> までにマッチングした |
2801 | 3871 | すべてのものを"取っておいて"、C<$&> には含めないようにさせます。 |
2802 | 3872 | これは事実上可変長の後読みを提供します。 |
2803 | 3873 | 他の先読み及び後読みの言明の中での利用も可能ですが、その振る舞いは |
2804 | 3874 | 今のところあまり定義されていません。 |
2805 | 3875 | |
2806 | 3876 | =begin original |
2807 | 3877 | |
2808 | 3878 | For various reasons C<\K> may be significantly more efficient than the |
2809 | 3879 | equivalent C<< (?<=...) >> construct, and it is especially useful in |
2810 | 3880 | situations where you want to efficiently remove something following |
2811 | 3881 | something else in a string. For instance |
2812 | 3882 | |
2813 | 3883 | =end original |
2814 | 3884 | |
2815 | 3885 | いくつかの理由から、C<\K> は等価な C<< (?<=...) >> |
2816 | 3886 | 構成子より非常に効率的で、文字列の中で何かに続いている何かを効率的に |
2817 | 3887 | 取り除きたいようなシチュエーションで効果的に役立ちます。 |
2818 | 3888 | 例えば |
2819 | 3889 | |
2820 | 3890 | s/(foo)bar/$1/g; |
2821 | 3891 | |
2822 | 3892 | =begin original |
2823 | 3893 | |
2824 | 3894 | can be rewritten as the much more efficient |
2825 | 3895 | |
2826 | 3896 | =end original |
2827 | 3897 | |
2828 | 3898 | 次のようにより効率的に書き直せます |
2829 | 3899 | |
2830 | 3900 | s/foo\Kbar//g; |
2831 | 3901 | |
3902 | =begin original | |
3903 | ||
3904 | The alphabetic forms (not including C<\K> are experimental; using them | |
3905 | yields a warning in the C<experimental::alpha_assertions> category. | |
3906 | ||
3907 | =end original | |
3908 | ||
3909 | (C<\K> を除く) 英字形式は実験的です; これらを使うと | |
3910 | C<experimental::alpha_assertions> カテゴリの警告が出ます。 | |
3911 | ||
2832 | 3912 | =item C<(?<!pattern)> |
2833 | X<(?<!)> X<look-behind, negative> X<lookbehind, negative> | |
2834 | 3913 | |
3914 | =item C<(*nlb:pattern)> | |
3915 | ||
3916 | =item C<(*negative_lookbehind:pattern)> | |
3917 | X<(?<!)> | |
3918 | X<(*nlb> | |
3919 | X<(*negative_lookbehind> | |
3920 | X<look-behind, negative> X<lookbehind, negative> | |
3921 | ||
2835 | 3922 | =begin original |
2836 | 3923 | |
2837 | A zero-width negative look | |
3924 | A zero-width negative lookbehind assertion. For example C</(?<!bar)foo/> | |
2838 | 3925 | matches any occurrence of "foo" that does not follow "bar". Works |
2839 | only for fixed-width look | |
3926 | only for fixed-width lookbehind. | |
2840 | 3927 | |
2841 | 3928 | =end original |
2842 | 3929 | |
2843 | 3930 | ゼロ幅の負の後読みの言明。 |
2844 | 3931 | 例えば C</(?<!bar)foo/> は "bar" に続いていない任意の "foo" に |
2845 | 3932 | マッチングします。 |
2846 | 3933 | 固定幅の後読みのみが動作します。 |
2847 | 3934 | |
2848 | =ba | |
3935 | =begin original | |
2849 | 3936 | |
2850 | ||
3937 | The alphabetic forms are experimental; using them yields a warning in the | |
3938 | C<experimental::alpha_assertions> category. | |
2851 | 3939 | |
3940 | =end original | |
3941 | ||
3942 | 英字形式は実験的です; これらを使うと | |
3943 | C<experimental::alpha_assertions> カテゴリの警告が出ます。 | |
3944 | ||
3945 | =back | |
3946 | ||
2852 | 3947 | =item C<< (?<NAME>pattern) >> |
3948 | ||
3949 | =item C<(?'NAME'pattern)> | |
2853 | 3950 | X<< (?<NAME>) >> X<(?'NAME')> X<named capture> X<capture> |
2854 | 3951 | |
2855 | 3952 | =begin original |
2856 | 3953 | |
2857 | 3954 | A named capture group. Identical in every respect to normal capturing |
2858 | 3955 | parentheses C<()> but for the additional fact that the group |
2859 | 3956 | can be referred to by name in various regular expression |
2860 | 3957 | constructs (like C<\g{NAME}>) and can be accessed by name |
2861 | 3958 | after a successful match via C<%+> or C<%->. See L<perlvar> |
2862 | 3959 | for more details on the C<%+> and C<%-> hashes. |
2863 | 3960 | |
2864 | 3961 | =end original |
2865 | 3962 | |
2866 | 3963 | 名前付の捕捉グループ。 |
2867 | 3964 | 通常のキャプチャかっこ C<()> と同様ですがそれに加えて、 |
2868 | 3965 | グループは(C<\g{NAME}> のように) 様々な正規表現構文で名前で参照でき、 |
2869 | 3966 | マッチングに成功したあと C<%+> を C<%-> を使って名前によって |
2870 | 3967 | アクセスできます。 |
2871 | 3968 | C<%+> 及び C<%-> ハッシュに関する詳細は L<perlvar> を |
2872 | 3969 | 参照してください。 |
2873 | 3970 | |
2874 | 3971 | =begin original |
2875 | 3972 | |
2876 | If multiple distinct capture groups have the same name then | |
3973 | If multiple distinct capture groups have the same name, then | |
2877 | $+{NAME} will refer to the leftmost defined group in the match. | |
3974 | C<$+{NAME}> will refer to the leftmost defined group in the match. | |
2878 | 3975 | |
2879 | 3976 | =end original |
2880 | 3977 | |
2881 | 3978 | 複数の異なる捕捉グループが同じ名前を持っていたときには |
2882 | $+{NAME} はマッチングの中で一番左で定義されたグループを参照します。 | |
3979 | C<$+{NAME}> はマッチングの中で一番左で定義されたグループを参照します。 | |
2883 | 3980 | |
2884 | 3981 | =begin original |
2885 | 3982 | |
2886 | 3983 | The forms C<(?'NAME'pattern)> and C<< (?<NAME>pattern) >> are equivalent. |
2887 | 3984 | |
2888 | 3985 | =end original |
2889 | 3986 | |
2890 | 3987 | 二つの形式 C<(?'NAME'pattern)> 及び C<< (?<NAME>pattern) >> は等価です。 |
2891 | 3988 | |
2892 | 3989 | =begin original |
2893 | 3990 | |
2894 | 3991 | B<NOTE:> While the notation of this construct is the same as the similar |
2895 | 3992 | function in .NET regexes, the behavior is not. In Perl the groups are |
2896 | 3993 | numbered sequentially regardless of being named or not. Thus in the |
2897 | 3994 | pattern |
2898 | 3995 | |
2899 | 3996 | =end original |
2900 | 3997 | |
2901 | 3998 | B<補足:> これを構成する記法は 類似していている .NET での正規表現と |
2902 | 3999 | 同じですが、振る舞いは異なります。 |
2903 | 4000 | Perl ではグループは名前がついているかどうかにかかわらず順番に番号が |
2904 | 4001 | 振られます。 |
2905 | 4002 | 従って次のパターンにおいて |
2906 | 4003 | |
2907 | 4004 | /(x)(?<foo>y)(z)/ |
2908 | 4005 | |
2909 | 4006 | =begin original |
2910 | 4007 | |
2911 | $+{foo} will be the same as $2, and $3 will contain 'z' instead of | |
4008 | C<$+{I<foo>}> will be the same as C<$2>, and C<$3> will contain 'z' instead of | |
2912 | 4009 | the opposite which is what a .NET regex hacker might expect. |
2913 | 4010 | |
2914 | 4011 | =end original |
2915 | 4012 | |
2916 | $+{foo} は $2 と同じであり、$3 には .NET 正規表現に | |
4013 | C<$+{I<foo>}> は C<$2> と同じであり、C<$3> には .NET 正規表現に | |
2917 | 4014 | 慣れた人が予測するのとは異なり 'z' が含まれます。 |
2918 | 4015 | |
2919 | 4016 | =begin original |
2920 | 4017 | |
2921 | Currently NAME is restricted to simple identifiers only. | |
4018 | Currently I<NAME> is restricted to simple identifiers only. | |
2922 | 4019 | In other words, it must match C</^[_A-Za-z][_A-Za-z0-9]*\z/> or |
2923 | 4020 | its Unicode extension (see L<utf8>), |
2924 | 4021 | though it isn't extended by the locale (see L<perllocale>). |
2925 | 4022 | |
2926 | 4023 | =end original |
2927 | 4024 | |
2928 | 現在のところ NAME はシンプルな識別子のみに制限されています。 | |
4025 | 現在のところ I<NAME> はシンプルな識別子のみに制限されています。 | |
2929 | 4026 | 言い換えると、C</^[_A-Za-z][_A-Za-z0-9]*\z/> または |
2930 | 4027 | その Unicode 拡張にマッチングしなければなりません |
2931 | 4028 | (L<utf8> も参照); しかしロケールでは拡張されません |
2932 | 4029 | (L<perllocale> 参照)。 |
2933 | 4030 | |
2934 | 4031 | =begin original |
2935 | 4032 | |
2936 | 4033 | B<NOTE:> In order to make things easier for programmers with experience |
2937 | 4034 | with the Python or PCRE regex engines, the pattern C<< (?PE<lt>NAMEE<gt>pattern) >> |
2938 | 4035 | may be used instead of C<< (?<NAME>pattern) >>; however this form does not |
2939 | 4036 | support the use of single quotes as a delimiter for the name. |
2940 | 4037 | |
2941 | 4038 | =end original |
2942 | 4039 | |
2943 | 4040 | B<補足:> Python や PCRE 正規表現エンジンになれたプログラマが |
2944 | 4041 | 楽になるように、C<< (?<NAME>pattern) >> の代わりに |
2945 | 4042 | C<< (?PE<lt>NAMEE<gt>pattern) >> のパターンを使うことも |
2946 | 4043 | できます; しかしこの形式は名前のデリミタとして |
2947 | 4044 | シングルクォートの使用はサポートされていません。 |
2948 | 4045 | |
2949 | 4046 | =item C<< \k<NAME> >> |
2950 | 4047 | |
2951 | 4048 | =item C<< \k'NAME' >> |
2952 | 4049 | |
2953 | 4050 | =begin original |
2954 | 4051 | |
2955 | 4052 | Named backreference. Similar to numeric backreferences, except that |
2956 | 4053 | the group is designated by name and not number. If multiple groups |
2957 | 4054 | have the same name then it refers to the leftmost defined group in |
2958 | 4055 | the current match. |
2959 | 4056 | |
2960 | 4057 | =end original |
2961 | 4058 | |
2962 | 4059 | 名前による後方参照。 |
2963 | 4060 | 数値によってではなく名前によってグループを指定する点を除いて、名前による |
2964 | 4061 | 後方参照と似ています。 |
2965 | 4062 | もし同じ名前の複数のグループがあったときには現在のマッチングで |
2966 | 4063 | 一番左に定義されているグループを参照します。 |
2967 | 4064 | |
2968 | 4065 | =begin original |
2969 | 4066 | |
2970 | 4067 | It is an error to refer to a name not defined by a C<< (?<NAME>) >> |
2971 | 4068 | earlier in the pattern. |
2972 | 4069 | |
2973 | 4070 | =end original |
2974 | 4071 | |
2975 | 4072 | パターン内で C<< (?<NAME>) >> によって定義されていない名前を |
2976 | 4073 | 参照するとエラーになります。 |
2977 | 4074 | |
2978 | 4075 | =begin original |
2979 | 4076 | |
2980 | 4077 | Both forms are equivalent. |
2981 | 4078 | |
2982 | 4079 | =end original |
2983 | 4080 | |
2984 | 4081 | 両方の形式とも等価です。 |
2985 | 4082 | |
2986 | 4083 | =begin original |
2987 | 4084 | |
2988 | 4085 | B<NOTE:> In order to make things easier for programmers with experience |
2989 | 4086 | with the Python or PCRE regex engines, the pattern C<< (?P=NAME) >> |
2990 | 4087 | may be used instead of C<< \k<NAME> >>. |
2991 | 4088 | |
2992 | 4089 | =end original |
2993 | 4090 | |
2994 | 4091 | B<補足:> Python や PCRE 正規表現エンジンになれたプログラマが楽に |
2995 | 4092 | なるように、C<< \k<NAME> >> の代わりに |
2996 | 4093 | C<< (?P=NAME) >> のパターンを使うこともできます。 |
2997 | 4094 | |
2998 | 4095 | =item C<(?{ code })> |
2999 | 4096 | X<(?{})> X<regex, code in> X<regexp, code in> X<regular expression, code in> |
3000 | 4097 | |
3001 | 4098 | =begin original |
3002 | 4099 | |
3003 | B<WARNING>: | |
4100 | B<WARNING>: Using this feature safely requires that you understand its | |
3004 | ||
4101 | limitations. Code executed that has side effects may not perform identically | |
3005 | ||
4102 | from version to version due to the effect of future optimisations in the regex | |
3006 | ||
4103 | engine. For more information on this, see L</Embedded Code Execution | |
4104 | Frequency>. | |
3007 | 4105 | |
3008 | 4106 | =end original |
3009 | 4107 | |
3010 | B<警告>: この | |
4108 | B<警告>: この機能を安全に使うには、その制限について理解することが必要です。 | |
3011 | また通知なしに変更されるかもしれません。 | |
3012 | 4109 | 副作用を持つコードの実行は今後の正規表現エンジンの最適化の影響で |
3013 | 4110 | バージョン間で必ずしも同じになるとは限らないでしょう。 |
4111 | これに関するさらなる情報については、L</Embedded Code Execution Frequency> を | |
4112 | 参照してください。 | |
3014 | 4113 | |
3015 | 4114 | =begin original |
3016 | 4115 | |
3017 | This zero-width assertion e | |
4116 | This zero-width assertion executes any embedded Perl code. It always | |
3018 | ||
4117 | succeeds, and its return value is set as C<$^R>. | |
3019 | the rules to determine where the C<code> ends are somewhat convoluted. | |
3020 | 4118 | |
3021 | 4119 | =end original |
3022 | 4120 | |
3023 | このゼロ幅アサーションは埋め込まれた任意の Perl コードを | |
4121 | このゼロ幅アサーションは埋め込まれた任意の Perl コードを実行します。 | |
3024 | これは常に(正規表現として)成功し、 | |
4122 | これは常に(正規表現として)成功し、返り値は C<$^R> に設定されます。 | |
3025 | 今のところ、C<code> が終わる場所を認識するルールは少々複雑です。 | |
3026 | 4123 | |
3027 | 4124 | =begin original |
3028 | 4125 | |
3029 | ||
4126 | In literal patterns, the code is parsed at the same time as the | |
3030 | ||
4127 | surrounding code. While within the pattern, control is passed temporarily | |
3031 | ||
4128 | back to the perl parser, until the logically-balancing closing brace is | |
4129 | encountered. This is similar to the way that an array index expression in | |
4130 | a literal string is handled, for example | |
3032 | 4131 | |
3033 | 4132 | =end original |
3034 | 4133 | |
3035 | ||
4134 | リテラルなパターンでは、このコードは周りのコードと同時にパースされます。 | |
3036 | ||
4135 | このパターンの中では、 | |
3037 | ||
4136 | 論理的にバランスが取れる閉じかっこが現れるまで、 | |
4137 | 制御文字は一時的に perl パーサーに渡されます。 | |
4138 | これはリテラル文字列の中の配列添字表現の扱われ方と似ています; 例えば: | |
3038 | 4139 | |
3039 | | |
4140 | "abc$array[ 1 + f('[') + g()]def" | |
3040 | /the (\S+)(?{ $color = $^N }) (\S+)(?{ $animal = $^N })/i; | |
3041 | print "color = $color, animal = $animal\n"; | |
3042 | 4141 | |
3043 | 4142 | =begin original |
3044 | 4143 | |
3045 | In | |
4144 | In particular, braces do not need to be balanced: | |
4145 | ||
4146 | =end original | |
4147 | ||
4148 | 特に、中かっこはバランスが取れている必要はありません: | |
4149 | ||
4150 | s/abc(?{ f('{'); })/def/ | |
4151 | ||
4152 | =begin original | |
4153 | ||
4154 | Even in a pattern that is interpolated and compiled at run-time, literal | |
4155 | code blocks will be compiled once, at perl compile time; the following | |
4156 | prints "ABCD": | |
4157 | ||
4158 | =end original | |
4159 | ||
4160 | 実行時に展開されてコンパイルされるパターンの中であっても、 | |
4161 | リテラルなコードブロックは perl のコンパイル時に一度だけコンパイルされます; | |
4162 | 次のものは "ABCD" と表示されます: | |
4163 | ||
4164 | print "D"; | |
4165 | my $qr = qr/(?{ BEGIN { print "A" } })/; | |
4166 | my $foo = "foo"; | |
4167 | /$foo$qr(?{ BEGIN { print "B" } })/; | |
4168 | BEGIN { print "C" } | |
4169 | ||
4170 | =begin original | |
4171 | ||
4172 | In patterns where the text of the code is derived from run-time | |
4173 | information rather than appearing literally in a source code /pattern/, | |
4174 | the code is compiled at the same time that the pattern is compiled, and | |
4175 | for reasons of security, C<use re 'eval'> must be in scope. This is to | |
4176 | stop user-supplied patterns containing code snippets from being | |
4177 | executable. | |
4178 | ||
4179 | =end original | |
4180 | ||
4181 | コードのテキストがソースコードの /pattern/ としてリテラルに現れるのではなく、 | |
4182 | 実行時の情報から派生したものであるパターンでは、 | |
4183 | コードはパターンがコンパイルされるのと同時にコンパイルされ、 | |
4184 | セキュリティー上の理由により、C<use re 'eval'> が | |
4185 | スコープ内になければなりません。 | |
4186 | これは、実行可能なコード片を含む、ユーザーが提供したパターンを止めるためです。 | |
4187 | ||
4188 | =begin original | |
4189 | ||
4190 | In situations where you need to enable this with C<use re 'eval'>, you should | |
4191 | also have taint checking enabled. Better yet, use the carefully | |
4192 | constrained evaluation within a Safe compartment. See L<perlsec> for | |
4193 | details about both these mechanisms. | |
4194 | ||
4195 | =end original | |
4196 | ||
4197 | C<use re 'eval'> でこれを有効にする必要がある場合、 | |
4198 | 汚染チェックも有効にするべきです。 | |
4199 | 出来れば、Safe 区画の中で注意深く制限された評価を使ってください。 | |
4200 | これら両方の機構に関する詳細については L<perlsec> を参照してください。 | |
4201 | ||
4202 | =begin original | |
4203 | ||
4204 | From the viewpoint of parsing, lexical variable scope and closures, | |
4205 | ||
4206 | =end original | |
4207 | ||
4208 | パース、レキシカル変数スコープ、クロージャの観点から、 | |
4209 | ||
4210 | /AAA(?{ BBB })CCC/ | |
4211 | ||
4212 | =begin original | |
4213 | ||
4214 | behaves approximately like | |
4215 | ||
4216 | =end original | |
4217 | ||
4218 | はおよそ次のように振る舞います: | |
4219 | ||
4220 | /AAA/ && do { BBB } && /CCC/ | |
4221 | ||
4222 | =begin original | |
4223 | ||
4224 | Similarly, | |
4225 | ||
4226 | =end original | |
4227 | ||
4228 | 同様に、 | |
4229 | ||
4230 | qr/AAA(?{ BBB })CCC/ | |
4231 | ||
4232 | =begin original | |
4233 | ||
4234 | behaves approximately like | |
4235 | ||
4236 | =end original | |
4237 | ||
4238 | はおよそ次のように振る舞います: | |
4239 | ||
4240 | sub { /AAA/ && do { BBB } && /CCC/ } | |
4241 | ||
4242 | =begin original | |
4243 | ||
4244 | In particular: | |
4245 | ||
4246 | =end original | |
4247 | ||
4248 | 特に: | |
4249 | ||
4250 | { my $i = 1; $r = qr/(?{ print $i })/ } | |
4251 | my $i = 2; | |
4252 | /$r/; # prints "1" | |
4253 | ||
4254 | =begin original | |
4255 | ||
4256 | Inside a C<(?{...})> block, C<$_> refers to the string the regular | |
3046 | 4257 | expression is matching against. You can also use C<pos()> to know what is |
3047 | 4258 | the current position of matching within this string. |
3048 | 4259 | |
3049 | 4260 | =end original |
3050 | 4261 | |
3051 | 4262 | C<(?{...})> ブロックの中では C<$_> は正規表現をマッチングさせている文字列を |
3052 | 4263 | 参照します。 |
3053 | 4264 | C<pos()> を使ってこの文字列で現在のマッチング位置を知ることもできます。 |
3054 | 4265 | |
3055 | 4266 | =begin original |
3056 | 4267 | |
3057 | The | |
4268 | The code block introduces a new scope from the perspective of lexical | |
3058 | ||
4269 | variable declarations, but B<not> from the perspective of C<local> and | |
3059 | ||
4270 | similar localizing behaviours. So later code blocks within the same | |
4271 | pattern will still see the values which were localized in earlier blocks. | |
4272 | These accumulated localizations are undone either at the end of a | |
4273 | successful match, or if the assertion is backtracked (compare | |
4274 | L</"Backtracking">). For example, | |
3060 | 4275 | |
3061 | 4276 | =end original |
3062 | 4277 | |
3063 | C< | |
4278 | コードブロックは、C<local> や同様のローカル化の振る舞いの観点 B<ではなく> | |
3064 | ||
4279 | レキシカル変数宣言の観点での新しいスコープを導入します。 | |
3065 | ||
4280 | 従って、同じパターンで後に出てくるコードブロックは | |
4281 | 前に出てきたローカル化された値が見えるままです。 | |
4282 | これらの蓄積されたローカル化は、マッチングが成功するか、 | |
4283 | アサートがバックトラックした時点で巻き戻されます | |
4284 | (L</"Backtracking"> と比較してください)。 | |
4285 | 例えば: | |
3066 | 4286 | |
3067 | 4287 | $_ = 'a' x 8; |
3068 | 4288 | m< |
3069 | 4289 | (?{ $cnt = 0 }) # Initialize $cnt. |
3070 | 4290 | ( |
3071 | 4291 | a |
3072 | 4292 | (?{ |
3073 | 4293 | local $cnt = $cnt + 1; # Update $cnt, |
3074 | 4294 | # backtracking-safe. |
3075 | 4295 | }) |
3076 | 4296 | )* |
3077 | 4297 | aaaa |
3078 | 4298 | (?{ $res = $cnt }) # On success copy to |
3079 | 4299 | # non-localized location. |
3080 | 4300 | >x; |
3081 | 4301 | |
3082 | 4302 | =begin original |
3083 | 4303 | |
3084 | will | |
4304 | will initially increment C<$cnt> up to 8; then during backtracking, its | |
3085 | ||
4305 | value will be unwound back to 4, which is the value assigned to C<$res>. | |
3086 | ||
4306 | At the end of the regex execution, C<$cnt> will be wound back to its initial | |
4307 | value of 0. | |
3087 | 4308 | |
3088 | 4309 | =end original |
3089 | 4310 | |
3090 | は C<$ | |
4311 | これはまず C<$cnt> は 8 までインクリメントされます; | |
3091 | ||
4312 | それからバックトラックの間に、この値は 4 まで巻き戻され、 | |
3092 | C< | |
4313 | その値が C<$res> に代入されます。 | |
4314 | 正規表現実行の最後では、$cnt は初期値である 0 に巻き戻ります。 | |
3093 | 4315 | |
3094 | 4316 | =begin original |
3095 | 4317 | |
3096 | This assertion may be used as | |
4318 | This assertion may be used as the condition in a | |
3097 | switch. If I<not> used in this way, the result of evaluation of | |
3098 | C<code> is put into the special variable C<$^R>. This happens | |
3099 | immediately, so C<$^R> can be used from other C<(?{ code })> assertions | |
3100 | inside the same regular expression. | |
3101 | 4319 | |
3102 | 4320 | =end original |
3103 | 4321 | |
3104 | このアサーションは | |
4322 | このアサーションは条件として: | |
3105 | ||
4324 | (?(condition)yes-pattern|no-pattern) | |
4325 | ||
4326 | =begin original | |
4327 | ||
4328 | switch. If I<not> used in this way, the result of evaluation of C<code> | |
4329 | is put into the special variable C<$^R>. This happens immediately, so | |
4330 | C<$^R> can be used from other C<(?{ code })> assertions inside the same | |
4331 | regular expression. | |
4332 | ||
4333 | =end original | |
4334 | ||
4335 | スイッチとして使われるかもしれません。 | |
3106 | 4336 | この方法で使われI<なかった>のなら、C<code> の評価結果は特殊変数 C<$^R> に |
3107 | 4337 | おかれます。 |
3108 | 4338 | これはすぐに行われるので C<$^R> は同じ正規表現内の他の C<?{ code })> |
3109 | 4339 | アサーションで使うことができます。 |
3110 | 4340 | |
3111 | 4341 | =begin original |
3112 | 4342 | |
3113 | 4343 | The assignment to C<$^R> above is properly localized, so the old |
3114 | 4344 | value of C<$^R> is restored if the assertion is backtracked; compare |
3115 | L<"Backtracking">. | |
4345 | L</"Backtracking">. | |
3116 | 4346 | |
3117 | 4347 | =end original |
3118 | 4348 | |
3119 | 4349 | この C<$^R> への設定は適切にlocal化されるため、C<$^R> の古い値は |
3120 | バックトラックしたときには復元されます; L<"Backtracking"> を | |
4350 | バックトラックしたときには復元されます; L</"Backtracking"> を | |
3121 | 4351 | 見てください。 |
3122 | 4352 | |
3123 | 4353 | =begin original |
3124 | 4354 | |
3125 | ||
4355 | Note that the special variable C<$^N> is particularly useful with code | |
3126 | ||
4356 | blocks to capture the results of submatches in variables without having to | |
3127 | ||
4357 | keep track of the number of nested parentheses. For example: | |
3128 | variables contain results of the C<qr//> operator (see | |
3129 | L<perlop/"qr/STRINGE<sol>msixpodual">). | |
3130 | 4358 | |
3131 | 4359 | =end original |
3132 | 4360 | |
3133 | ||
4361 | 特殊変数 C<$^N> は、一緒にネストしたかっこの数を数えずに一つ前の | |
3134 | ||
4362 | マッチング結果を捕捉するコードブロックで特に有用です。 | |
3135 | 変数が C<qr//> 演算子(L<perlop/"qr/STRINGE<sol>msixpodual"> 参照)の結果を | |
3136 | 含んでいる時以外は拒否されます。 | |
3137 | ||
3138 | =begin original | |
3139 | ||
3140 | This restriction is due to the wide-spread and remarkably convenient | |
3141 | custom of using run-time determined strings as patterns. For example: | |
3142 | ||
3143 | =end original | |
3144 | ||
3145 | この制限は、実行時に決まる文字列をパターンとして使う、とても広まっていて | |
3146 | とても便利な風習のためのものです。 | |
3147 | 4363 | 例えば: |
3148 | 4364 | |
3149 | | |
4365 | $_ = "The brown fox jumps over the lazy dog"; | |
3150 | c | |
4366 | /the (\S+)(?{ $color = $^N }) (\S+)(?{ $animal = $^N })/i; | |
3151 | $ | |
4367 | print "color = $color, animal = $animal\n"; | |
3152 | 4368 | |
4369 | =item C<(??{ code })> | |
4370 | X<(??{})> | |
4371 | X<regex, postponed> X<regexp, postponed> X<regular expression, postponed> | |
4372 | ||
3153 | 4373 | =begin original |
3154 | 4374 | |
3155 | B | |
4375 | B<WARNING>: Using this feature safely requires that you understand its | |
3156 | ||
4376 | limitations. Code executed that has side effects may not perform | |
3157 | ||
4377 | identically from version to version due to the effect of future | |
3158 | ||
4378 | optimisations in the regex engine. For more information on this, see | |
3159 | ||
4379 | L</Embedded Code Execution Frequency>. | |
3160 | Better yet, use the carefully constrained evaluation within a Safe | |
3161 | compartment. See L<perlsec> for details about both these mechanisms. | |
3162 | 4380 | |
3163 | 4381 | =end original |
3164 | 4382 | |
3165 | ||
4383 | B<警告>: この機能を安全に使うには、その制限について理解することが必要です。 | |
3166 | ||
4384 | 副作用を持つコードの実行は今後の正規表現エンジンの最適化の影響で | |
3167 | ||
4385 | バージョン間で必ずしも同じになるとは限らないでしょう。 | |
3168 | ||
4386 | これに関するさらなる情報については、L</Embedded Code Execution Frequency> を | |
3169 | ||
4387 | 参照してください。 | |
3170 | 使うべきでしょう。 | |
3171 | この双方のメカニズムについての詳細は L<perlsec> を参照してください。 | |
3172 | 4388 | |
3173 | 4389 | =begin original |
3174 | 4390 | |
3175 | ||
4391 | This is a "postponed" regular subexpression. It behaves in I<exactly> the | |
3176 | ||
4392 | same way as a C<(?{ code })> code block as described above, except that | |
3177 | ||
4393 | its return value, rather than being assigned to C<$^R>, is treated as a | |
4394 | pattern, compiled if it's a string (or used as-is if its a qr// object), | |
4395 | then matched as if it were inserted instead of this construct. | |
3178 | 4396 | |
3179 | 4397 | =end original |
3180 | 4398 | |
3181 | ||
4399 | これは「先送りされた」正規部分表現です。 | |
3182 | ||
4400 | これは上述の C<(?{ code })> コードブロックと I<正確に> 同じように | |
3183 | ||
4401 | 振る舞いますが、その返り値は、C<$^R> に代入されるのではなく、 | |
4402 | パターンとして扱われ、 | |
4403 | それが文字列の場合はコンパイルされ(あるいは qr// オブジェクトの場合は | |
4404 | そのまま使われ)、それからこの構文の代わりに挿入されていたかのように | |
4405 | マッチングします。 | |
3184 | 4406 | |
3185 | 4407 | =begin original |
3186 | 4408 | |
3187 | ||
4409 | During the matching of this sub-pattern, it has its own set of | |
3188 | ||
4410 | captures which are valid during the sub-match, but are discarded once | |
3189 | ||
4411 | control returns to the main pattern. For example, the following matches, | |
3190 | ||
4412 | with the inner pattern capturing "B" and matching "BB", while the outer | |
3191 | ||
4413 | pattern captures "A"; | |
3192 | unstable. | |
3193 | 4414 | |
3194 | 4415 | =end original |
3195 | 4416 | |
3196 | ||
4417 | 副パターンのマッチングの間、副マッチングの間有効な独自の捕捉グループを | |
3197 | ||
4418 | 持ちますが、一旦制御が主パターンに戻ると捨てられます。 | |
3198 | ||
4419 | 例えば、次のマッチングは、内側のパターンで "B" と "BB" にマッチングし、 | |
3199 | ||
4420 | 一方外側のパターンは "A" を捕捉します; | |
3200 | 4421 | |
3201 | ||
4422 | my $inner = '(.)\1'; | |
3202 | ||
4423 | "ABBA" =~ /^(.)(??{ $inner })\1/; | |
3203 | ||
4424 | print $1; # prints "A"; | |
3204 | 4425 | |
3205 | 4426 | =begin original |
3206 | 4427 | |
3207 | ||
4428 | Note that this means that there is no way for the inner pattern to refer | |
3208 | ||
4429 | to a capture group defined outside. (The code block itself can use C<$1>, | |
3209 | ||
4430 | I<etc>., to refer to the enclosing pattern's capture groups.) Thus, although | |
3210 | due to the effect of future optimisations in the regex engine. | |
3211 | 4431 | |
3212 | 4432 | =end original |
3213 | 4433 | |
3214 | ||
4434 | 内側のパターンが外側で定義された捕捉グループを参照する方法は | |
3215 | ||
4435 | ないことに注意してください。 | |
3216 | 副作用を持つコードの実行は今後の正規表現エンジンの最適化の影響で | |
3217 | バージョン間で必ずしも同じになるとは限らないでしょう。 | |
3218 | ||
3219 | =begin original | |
3220 | ||
3221 | This is a "postponed" regular subexpression. The C<code> is evaluated | |
3222 | at run time, at the moment this subexpression may match. The result | |
3223 | of evaluation is considered a regular expression and matched as | |
3224 | if it were inserted instead of this construct. Note that this means | |
3225 | that the contents of capture groups defined inside an eval'ed pattern | |
3226 | are not available outside of the pattern, and vice versa, there is no | |
3227 | way for the inner pattern returned from the code block to refer to a | |
3228 | capture group defined outside. (The code block itself can use C<$1>, etc., | |
3229 | to refer to the enclosing pattern's capture groups.) Thus, | |
3230 | ||
3231 | =end original | |
3232 | ||
3233 | これは「先送りされた」正規部分表現です。 | |
3234 | C<code> は実行時に評価され、そのときにこの部分表現にマッチングさせます。 | |
3235 | 評価の結果は正規表現として受け取られ、この構成子の代わりに | |
3236 | 入れられていたかのようにマッチングされます。 | |
3237 | これは eval されたパターン内部で定義された捕捉グループの内容はパターンの | |
3238 | 外側では提供されず、そしてその逆も同様になる点に注意してください; | |
3239 | コードブロックから返された内側のパターンが | |
3240 | 外側で定義された捕捉グループを参照する方法はありません。 | |
3241 | 4436 | (コードブロック自体は、内側のパターンの捕捉グループを参照するために |
3242 | 4437 | C<$1> などを使えます。) |
3243 | 4438 | 従って: |
3244 | 4439 | |
3245 | 4440 | ('a' x 100)=~/(??{'(.)' x 100})/ |
3246 | 4441 | |
3247 | 4442 | =begin original |
3248 | 4443 | |
3249 | ||
4444 | I<will> match, it will I<not> set C<$1> on exit. | |
3250 | 4445 | |
3251 | 4446 | =end original |
3252 | 4447 | |
3253 | これはマッチング | |
4448 | これはマッチング I<します> が、終了時に C<$1> は設定 I<されません>。 | |
3254 | 4449 | |
3255 | 4450 | =begin original |
3256 | 4451 | |
3257 | The C<code> is not interpolated. As before, the rules to determine | |
3258 | where the C<code> ends are currently somewhat convoluted. | |
3259 | ||
3260 | =end original | |
3261 | ||
3262 | C<code> は埋め込まれません。 | |
3263 | 先の時と同様に C<code> が終了していると決定するルールは少々複雑です。 | |
3264 | ||
3265 | =begin original | |
3266 | ||
3267 | 4452 | The following pattern matches a parenthesized group: |
3268 | 4453 | |
3269 | 4454 | =end original |
3270 | 4455 | |
3271 | 4456 | 次のパターンはかっこで囲まれたグループにマッチングします: |
3272 | 4457 | |
3273 | 4458 | $re = qr{ |
3274 | 4459 | \( |
3275 | 4460 | (?: |
3276 | 4461 | (?> [^()]+ ) # Non-parens without backtracking |
3277 | 4462 | | |
3278 | 4463 | (??{ $re }) # Group with matching parens |
3279 | 4464 | )* |
3280 | 4465 | \) |
3281 | 4466 | }x; |
3282 | 4467 | |
3283 | 4468 | =begin original |
3284 | 4469 | |
3285 | See also | |
4470 | See also | |
4471 | L<C<(?I<PARNO>)>|/(?PARNO) (?-PARNO) (?+PARNO) (?R) (?0)> | |
4472 | for a different, more efficient way to accomplish | |
3286 | 4473 | the same task. |
3287 | 4474 | |
3288 | 4475 | =end original |
3289 | 4476 | |
3290 | 同じタスクを行う別の、より効率的な方法として | |
4477 | 同じタスクを行う別の、より効率的な方法として | |
4478 | L<C<(?I<PARNO>)>|/(?PARNO) (?-PARNO) (?+PARNO) (?R) (?0)> も | |
3291 | 4479 | 参照してください。 |
3292 | 4480 | |
3293 | 4481 | =begin original |
3294 | 4482 | |
3295 | ||
4483 | Executing a postponed regular expression too many times without | |
3296 | ||
4484 | consuming any input string will also result in a fatal error. The depth | |
3297 | ||
4485 | at which that happens is compiled into perl, so it can be changed with a | |
3298 | ||
4486 | custom build. | |
3299 | L<perlop/"qrE<sol>STRINGE<sol>msixpodual">). | |
3300 | 4487 | |
3301 | 4488 | =end original |
3302 | 4489 | |
3303 | ||
4490 | 入力を消費しない多すぎる先送りされた正規表現を実行するのも | |
3304 | ||
4491 | 致命的なエラーとなります。 | |
3305 | ||
4492 | これが起きる深度は perl にコンパイルされているので、カスタムビルドで | |
3306 | ||
4493 | これを変更できます。 | |
3307 | 4494 | |
3308 | = | |
4495 | =item C<(?I<PARNO>)> C<(?-I<PARNO>)> C<(?+I<PARNO>)> C<(?R)> C<(?0)> | |
4496 | X<(?PARNO)> X<(?1)> X<(?R)> X<(?0)> X<(?-1)> X<(?+1)> X<(?-PARNO)> X<(?+PARNO)> | |
4497 | X<regex, recursive> X<regexp, recursive> X<regular expression, recursive> | |
4498 | X<regex, relative recursion> X<GOSUB> X<GOSTART> | |
3309 | 4499 | |
3310 | In perl 5.12.x and earlier, because the regex engine was not re-entrant, | |
3311 | delayed code could not safely invoke the regex engine either directly with | |
3312 | C<m//> or C<s///>), or indirectly with functions such as C<split>. | |
3313 | ||
3314 | =end original | |
3315 | ||
3316 | perl 5.12.x 以前では、Perl の正規表現エンジンは再入可能ではないので、 | |
3317 | 遅延されたコードからは C<m//> または C<s///> を使って直接的にでも | |
3318 | C<split> のような関数を使って間接的にでも安全には呼び出せません。 | |
3319 | ||
3320 | 4500 | =begin original |
3321 | 4501 | |
3322 | Recursi | |
4502 | Recursive subpattern. Treat the contents of a given capture buffer in the | |
3323 | re | |
4503 | current pattern as an independent subpattern and attempt to match it at | |
3324 | ||
4504 | the current position in the string. Information about capture state from | |
4505 | the caller for things like backreferences is available to the subpattern, | |
4506 | but capture buffers set by the subpattern are not visible to the caller. | |
3325 | 4507 | |
3326 | 4508 | =end original |
3327 | 4509 | |
3328 | ||
4510 | 再帰部分パターン。 | |
3329 | ||
4511 | 現在のパターンの与えられた捕捉バッファの内容を独立した | |
3330 | ||
4512 | 部分パターンとして扱って、 | |
4513 | 文字列の現在の位置でマッチングしようとします。 | |
4514 | 後方参照のような呼び出し元からの捕捉状態に関する情報は | |
4515 | 部分パターンで利用可能ですが、 | |
4516 | 部分パターンで設定された捕捉バッファは呼び出し元には見えません。 | |
3331 | 4517 | |
3332 | =item C<(?PARNO)> C<(?-PARNO)> C<(?+PARNO)> C<(?R)> C<(?0)> | |
3333 | X<(?PARNO)> X<(?1)> X<(?R)> X<(?0)> X<(?-1)> X<(?+1)> X<(?-PARNO)> X<(?+PARNO)> | |
3334 | X<regex, recursive> X<regexp, recursive> X<regular expression, recursive> | |
3335 | X<regex, relative recursion> | |
3336 | ||
3337 | 4518 | =begin original |
3338 | 4519 | |
3339 | Similar to C<(??{ code })> except it does not involve c | |
4520 | Similar to C<(??{ code })> except that it does not involve executing any | |
3340 | ||
4521 | code or potentially compiling a returned pattern string; instead it treats | |
3341 | ||
4522 | the part of the current pattern contained within a specified capture group | |
3342 | ||
4523 | as an independent pattern that must match at the current position. Also | |
3343 | ||
4524 | different is the treatment of capture buffers, unlike C<(??{ code })> | |
4525 | recursive patterns have access to their caller's match state, so one can | |
4526 | use backreferences safely. | |
3344 | 4527 | |
3345 | 4528 | =end original |
3346 | 4529 | |
3347 | ||
4530 | C<(??{ code })> と似ていますが、 | |
3348 | ||
4531 | コードの実行を伴なわず、返されたパターン文字列をコンパイルもしません; | |
3349 | ||
4532 | その代わりに、指定された捕捉グループに含まれる現在のパターンの一部を、 | |
3350 | ||
4533 | 現在の位置でマッチングすべき独立したパターンとして扱います。 | |
4534 | また、捕捉バッファの扱いも異なります; | |
4535 | C<(??{ code })> と異なり、再帰パターンはその呼び出し元のマッチング | |
4536 | 状態にアクセスすることが出来るので、安全に後方参照を使えます。 | |
3351 | 4537 | |
3352 | 4538 | =begin original |
3353 | 4539 | |
3354 | PARNO is a sequence of digits (not starting with 0) whose value reflects | |
4540 | I<PARNO> is a sequence of digits (not starting with 0) whose value reflects | |
3355 | 4541 | the paren-number of the capture group to recurse to. C<(?R)> recurses to |
3356 | 4542 | the beginning of the whole pattern. C<(?0)> is an alternate syntax for |
3357 | C<(?R)>. If PARNO is preceded by a plus or minus sign then it is assumed | |
4543 | C<(?R)>. If I<PARNO> is preceded by a plus or minus sign then it is assumed | |
3358 | 4544 | to be relative, with negative numbers indicating preceding capture groups |
3359 | 4545 | and positive ones following. Thus C<(?-1)> refers to the most recently |
3360 | 4546 | declared group, and C<(?+1)> indicates the next group to be declared. |
3361 | 4547 | Note that the counting for relative recursion differs from that of |
3362 | 4548 | relative backreferences, in that with recursion unclosed groups B<are> |
3363 | 4549 | included. |
3364 | 4550 | |
3365 | 4551 | =end original |
3366 | 4552 | |
3367 | PARNO はその値が再帰させる捕捉グループのかっこ番号を反映する一連の | |
4553 | I<PARNO> はその値が再帰させる捕捉グループのかっこ番号を反映する一連の | |
3368 | 4554 | 数字からなります(そして 0 からは始まりません)。 |
3369 | 4555 | C<(?R)> はパターン全体の最初から再帰します。 |
3370 | 4556 | C<(?0)> は C<(?R)> の別の構文です。 |
3371 | PARNO の前に正符号または負符号がついていた場合には相対的な位置として | |
4557 | I<PARNO> の前に正符号または負符号がついていた場合には相対的な位置として | |
3372 | 4558 | 使われます; 負数であれば前の捕捉グループを、正数であれば続く |
3373 | 4559 | 捕捉グループを示します。 |
3374 | 4560 | 従って C<(?-1)> は一番最近宣言されたグループを参照し、C<(?+1)> は次に |
3375 | 4561 | 宣言されるグループを参照します。 |
3376 | 4562 | 相対再帰の数え方は相対後方参照とは違って、グループに閉じていない再帰は |
3377 | 4563 | 含まB<れる>ことに注意してください, |
3378 | 4564 | |
3379 | 4565 | =begin original |
3380 | 4566 | |
3381 | The following pattern matches a function foo() which may contain | |
4567 | The following pattern matches a function C<foo()> which may contain | |
3382 | 4568 | balanced parentheses as the argument. |
3383 | 4569 | |
3384 | 4570 | =end original |
3385 | 4571 | |
3386 | 4572 | 以下のパターンは引数にバランスのとれたかっこを含んでいるかもしれない関数 |
3387 | foo() にマッチングします。 | |
4573 | C<foo()> にマッチングします。 | |
3388 | 4574 | |
3389 | 4575 | $re = qr{ ( # paren group 1 (full function) |
3390 | 4576 | foo |
3391 | 4577 | ( # paren group 2 (parens) |
3392 | 4578 | \( |
3393 | 4579 | ( # paren group 3 (contents of parens) |
3394 | 4580 | (?: |
3395 | 4581 | (?> [^()]+ ) # Non-parens without backtracking |
3396 | 4582 | | |
3397 | 4583 | (?2) # Recurse to start of paren group 2 |
3398 | 4584 | )* |
3399 | 4585 | ) |
3400 | 4586 | \) |
3401 | 4587 | ) |
3402 | 4588 | ) |
3403 | 4589 | }x; |
3404 | 4590 | |
3405 | 4591 | =begin original |
3406 | 4592 | |
3407 | 4593 | If the pattern was used as follows |
3408 | 4594 | |
3409 | 4595 | =end original |
3410 | 4596 | |
3411 | 4597 | このパターンを以下のように使うと, |
3412 | 4598 | |
3413 | 4599 | 'foo(bar(baz)+baz(bop))'=~/$re/ |
3414 | 4600 | and print "\$1 = $1\n", |
3415 | 4601 | "\$2 = $2\n", |
3416 | 4602 | "\$3 = $3\n"; |
3417 | 4603 | |
3418 | 4604 | =begin original |
3419 | 4605 | |
3420 | 4606 | the output produced should be the following: |
3421 | 4607 | |
3422 | 4608 | =end original |
3423 | 4609 | |
3424 | 4610 | 次のように出力されます: |
3425 | 4611 | |
3426 | 4612 | $1 = foo(bar(baz)+baz(bop)) |
3427 | 4613 | $2 = (bar(baz)+baz(bop)) |
3428 | 4614 | $3 = bar(baz)+baz(bop) |
3429 | 4615 | |
3430 | 4616 | =begin original |
3431 | 4617 | |
3432 | 4618 | If there is no corresponding capture group defined, then it is a |
3433 | fatal error. Recursing deep | |
4619 | fatal error. Recursing deeply without consuming any input string will | |
3434 | ||
4620 | also result in a fatal error. The depth at which that happens is | |
3435 | into perl, so chang | |
4621 | compiled into perl, so it can be changed with a custom build. | |
3436 | 4622 | |
3437 | 4623 | =end original |
3438 | 4624 | |
3439 | 4625 | もし対応する捕捉グループが定義されていなかったときには致命的な |
3440 | 4626 | エラーとなります。 |
3441 | 入力を消費しない | |
4627 | 入力を消費しない深い再帰も致命的なエラーとなります。 | |
3442 | ||
4628 | これが起きる深度は perl にコンパイルされているので、カスタムビルドで | |
3443 | ||
4629 | これを変更できます。 | |
3444 | 4630 | |
3445 | 4631 | =begin original |
3446 | 4632 | |
3447 | 4633 | The following shows how using negative indexing can make it |
3448 | 4634 | easier to embed recursive patterns inside of a C<qr//> construct |
3449 | 4635 | for later use: |
3450 | 4636 | |
3451 | 4637 | =end original |
3452 | 4638 | |
3453 | 4639 | 以下に後で使うパターンのために、C<qr//> 構成子内で再帰を埋め込むのに |
3454 | 4640 | 負数の参照を使うとどのように容易になるかを示します: |
3455 | 4641 | |
3456 | 4642 | my $parens = qr/(\((?:[^()]++|(?-1))*+\))/; |
3457 | if (/foo $parens \s+ + \s+ bar $parens/x) { | |
4643 | if (/foo $parens \s+ \+ \s+ bar $parens/x) { | |
3458 | 4644 | # do something here... |
3459 | 4645 | } |
3460 | 4646 | |
3461 | 4647 | =begin original |
3462 | 4648 | |
3463 | 4649 | B<Note> that this pattern does not behave the same way as the equivalent |
3464 | 4650 | PCRE or Python construct of the same form. In Perl you can backtrack into |
3465 | 4651 | a recursed group, in PCRE and Python the recursed into group is treated |
3466 | 4652 | as atomic. Also, modifiers are resolved at compile time, so constructs |
3467 | like (?i:(?1)) or (?:(?i)(?1)) do not affect how the sub-pattern will | |
4653 | like C<(?i:(?1))> or C<(?:(?i)(?1))> do not affect how the sub-pattern will | |
3468 | 4654 | be processed. |
3469 | 4655 | |
3470 | 4656 | =end original |
3471 | 4657 | |
3472 | 4658 | B<補足> このパターンは PCRE や Python での等価な形式の構成子と同じように |
3473 | 4659 | 振る舞うわけではありません。 |
3474 | 4660 | Perl においては再帰グループの中にバックトラックできますが、PCRE や |
3475 | 4661 | Python ではグループへの再帰はアトミックに扱われます。 |
3476 | また、修飾子はコンパイル時に解決されるので、(?i:(?1)) や | |
4662 | また、修飾子はコンパイル時に解決されるので、C<(?i:(?1))> や | |
3477 | (?:(?i)(?1)) といった構成子はサブパターンがどのように処理されたかに | |
4663 | C<(?:(?i)(?1))> といった構成子はサブパターンがどのように処理されたかに | |
3478 | 4664 | 影響されません。 |
3479 | 4665 | |
3480 | 4666 | =item C<(?&NAME)> |
3481 | 4667 | X<(?&NAME)> |
3482 | 4668 | |
3483 | 4669 | =begin original |
3484 | 4670 | |
3485 | Recurse to a named subpattern. Identical to C<(?PARNO)> except that the | |
4671 | Recurse to a named subpattern. Identical to C<(?I<PARNO>)> except that the | |
3486 | 4672 | parenthesis to recurse to is determined by name. If multiple parentheses have |
3487 | 4673 | the same name, then it recurses to the leftmost. |
3488 | 4674 | |
3489 | 4675 | =end original |
3490 | 4676 | |
3491 | 4677 | 名前付きサブパターンへの再帰。 |
3492 | 再帰するかっこが名前によって決定される点以外は C<(?PARNO)> と等価です。 | |
4678 | 再帰するかっこが名前によって決定される点以外は C<(?I<PARNO>)> と等価です。 | |
3493 | 4679 | もし複数のかっこで同じ名前を持っていた場合には一番左のものに再帰します。 |
3494 | 4680 | |
3495 | 4681 | =begin original |
3496 | 4682 | |
3497 | 4683 | It is an error to refer to a name that is not declared somewhere in the |
3498 | 4684 | pattern. |
3499 | 4685 | |
3500 | 4686 | =end original |
3501 | 4687 | |
3502 | 4688 | パターンのどこでも宣言されていない名前の参照はエラーになります。 |
3503 | 4689 | |
3504 | 4690 | =begin original |
3505 | 4691 | |
3506 | 4692 | B<NOTE:> In order to make things easier for programmers with experience |
3507 | 4693 | with the Python or PCRE regex engines the pattern C<< (?P>NAME) >> |
3508 | 4694 | may be used instead of C<< (?&NAME) >>. |
3509 | 4695 | |
3510 | 4696 | =end original |
3511 | 4697 | |
3512 | 4698 | B<補足:> Python または PCRE 正規表現エンジンに慣れているプログラマが |
3513 | 4699 | 簡単になるように C<< (?&NAME) >> の代わりに C<< (?P>NANE) >> を使うことも |
3514 | 4700 | できます。 |
3515 | 4701 | |
3516 | 4702 | =item C<(?(condition)yes-pattern|no-pattern)> |
3517 | 4703 | X<(?()> |
3518 | 4704 | |
3519 | 4705 | =item C<(?(condition)yes-pattern)> |
3520 | 4706 | |
3521 | 4707 | =begin original |
3522 | 4708 | |
3523 | 4709 | Conditional expression. Matches C<yes-pattern> if C<condition> yields |
3524 | 4710 | a true value, matches C<no-pattern> otherwise. A missing pattern always |
3525 | 4711 | matches. |
3526 | 4712 | |
3527 | 4713 | =end original |
3528 | 4714 | |
3529 | 4715 | 条件付き式。 |
3530 | 4716 | C<condition> が真なら C<yes-pattern> にマッチングし、さもなければ |
3531 | 4717 | C<no-pattern> にマッチングします。 |
3532 | 4718 | パターンがなければ常にマッチングします。 |
3533 | 4719 | |
3534 | 4720 | =begin original |
3535 | 4721 | |
3536 | C<(condition)> should be | |
4722 | C<(condition)> should be one of: | |
3537 | ||
3538 | ||
4724 | =end original | |
3539 | ||
3540 | ||
4726 | C<(condition)> は次のいずれかです: | |
3541 | ||
4728 | =over 4 | |
4729 | ||
4730 | =item an integer in parentheses | |
4731 | ||
4732 | (かっこでくるまれた数値) | |
4733 | ||
4734 | =begin original | |
4735 | ||
4736 | (which is valid if the corresponding pair of parentheses | |
4737 | matched); | |
4738 | ||
4739 | =end original | |
4740 | ||
4741 | (対応するかっこ対がマッチングしたときに有効); | |
4742 | ||
4743 | =item a lookahead/lookbehind/evaluate zero-width assertion; | |
4744 | ||
4745 | (先読み/後読み/ゼロ幅で評価される言明) | |
4746 | ||
4747 | =item a name in angle brackets or single quotes | |
4748 | ||
4749 | (角かっこもしくはシングルクォートでくるまれた名前) | |
4750 | ||
4751 | =begin original | |
4752 | ||
4753 | (which is valid if a group with the given name matched); | |
4754 | ||
4755 | =end original | |
4756 | ||
4757 | (その名前のグループがマッチングしたときに有効); | |
4758 | ||
4759 | =item the special symbol C<(R)> | |
4760 | ||
4761 | (特殊なシンボル C<(R)>) | |
4762 | ||
4763 | =begin original | |
4764 | ||
4765 | (true when evaluated inside of recursion or eval). Additionally the | |
4766 | C<"R"> may be | |
3542 | 4767 | followed by a number, (which will be true when evaluated when recursing |
3543 | 4768 | inside of the appropriate group), or by C<&NAME>, in which case it will |
3544 | 4769 | be true only when evaluated during recursion in the named group. |
3545 | 4770 | |
3546 | 4771 | =end original |
3547 | 4772 | |
3548 | ||
4773 | (再帰または eval 内で評価されているときに真)。 | |
3549 | ||
4774 | 加えて C<"R"> には数字(対応するグループ内で再帰しているときに真)、もしくは | |
3550 | もしくはシングルクォートでくるまれた名前(その名前のグループが | |
3551 | マッチングしたときに有効)、特殊なシンボル (R) (再帰または eval 内で | |
3552 | 評価されているときに真)のいずれかです。 | |
3553 | 加えて R には数字(対応するグループ内で再帰しているときに真)、もしくは | |
3554 | 4775 | C<&NAME>、こちらの時はその名前のグループで再帰している時にのみ真、を |
3555 | 4776 | 続けることもできます。 |
3556 | 4777 | |
4778 | =back | |
4779 | ||
3557 | 4780 | =begin original |
3558 | 4781 | |
3559 | 4782 | Here's a summary of the possible predicates: |
3560 | 4783 | |
3561 | 4784 | =end original |
3562 | 4785 | |
3563 | 4786 | 可能な述語の要約を次に示します: |
3564 | 4787 | |
3565 | 4788 | =over 4 |
3566 | 4789 | |
3567 | =item (1) (2) ... | |
4790 | =item C<(1)> C<(2)> ... | |
3568 | 4791 | |
3569 | 4792 | =begin original |
3570 | 4793 | |
3571 | 4794 | Checks if the numbered capturing group has matched something. |
4795 | Full syntax: C<< (?(1)then|else) >> | |
3572 | 4796 | |
3573 | 4797 | =end original |
3574 | 4798 | |
3575 | 4799 | その番号の捕捉グループが何かにマッチングしたかどうかを調べます。 |
4800 | 完全な文法: C<< (?(1)then|else) >> | |
3576 | 4801 | |
3577 | =item (<NAME>) ('NAME') | |
4802 | =item C<(E<lt>I<NAME>E<gt>)> C<('I<NAME>')> | |
3578 | 4803 | |
3579 | 4804 | =begin original |
3580 | 4805 | |
3581 | 4806 | Checks if a group with the given name has matched something. |
4807 | Full syntax: C<< (?(<name>)then|else) >> | |
3582 | 4808 | |
3583 | 4809 | =end original |
3584 | 4810 | |
3585 | 4811 | その名前のグループが何かにマッチングしたかどうかを調べます。 |
4812 | 完全な文法: C<< (?(<name>)then|else) >> | |
3586 | 4813 | |
3587 | =item (?=...) (?!...) (?<=...) (?<!...) | |
4814 | =item C<(?=...)> C<(?!...)> C<(?<=...)> C<(?<!...)> | |
3588 | 4815 | |
3589 | 4816 | =begin original |
3590 | 4817 | |
3591 | Checks whether the pattern matches (or does not match, for the | |
4818 | Checks whether the pattern matches (or does not match, for the C<"!"> | |
3592 | 4819 | variants). |
4820 | Full syntax: C<< (?(?=lookahead)then|else) >> | |
3593 | 4821 | |
3594 | 4822 | =end original |
3595 | 4823 | |
3596 | パターンがマッチングするか (あるいは | |
4824 | パターンがマッチングするか (あるいは C<"!"> 版はマッチングしないか) を | |
3597 | 4825 | チェックします。 |
4826 | 完全な文法: C<< (?(?=lookahead)then|else) >> | |
3598 | 4827 | |
3599 | =item (?{ CODE }) | |
4828 | =item C<(?{ I<CODE> })> | |
3600 | 4829 | |
3601 | 4830 | =begin original |
3602 | 4831 | |
3603 | 4832 | Treats the return value of the code block as the condition. |
4833 | Full syntax: C<< (?(?{ code })then|else) >> | |
3604 | 4834 | |
3605 | 4835 | =end original |
3606 | 4836 | |
3607 | 4837 | コードブロックの返り値を条件として扱います。 |
4838 | 完全な文法: C<< (?(?{ code })then|else) >> | |
3608 | 4839 | |
3609 | =item (R) | |
4840 | =item C<(R)> | |
3610 | 4841 | |
3611 | 4842 | =begin original |
3612 | 4843 | |
3613 | 4844 | Checks if the expression has been evaluated inside of recursion. |
4845 | Full syntax: C<< (?(R)then|else) >> | |
3614 | 4846 | |
3615 | 4847 | =end original |
3616 | 4848 | |
3617 | 4849 | 式が再帰の中で評価されているかどうかを調べます。 |
4850 | 完全な文法: C<< (?(R)then|else) >> | |
3618 | 4851 | |
3619 | =item (R1) (R2) ... | |
4852 | =item C<(R1)> C<(R2)> ... | |
3620 | 4853 | |
3621 | 4854 | =begin original |
3622 | 4855 | |
3623 | 4856 | Checks if the expression has been evaluated while executing directly |
3624 | 4857 | inside of the n-th capture group. This check is the regex equivalent of |
3625 | 4858 | |
3626 | 4859 | =end original |
3627 | 4860 | |
3628 | 4861 | 式がその n 番目の捕捉グループのすぐ内側で実行されているかどうかを調べます。 |
3629 | 4862 | これは次のものと等価な正規表現です |
3630 | 4863 | |
3631 | 4864 | if ((caller(0))[3] eq 'subname') { ... } |
3632 | 4865 | |
3633 | 4866 | =begin original |
3634 | 4867 | |
3635 | 4868 | In other words, it does not check the full recursion stack. |
3636 | 4869 | |
3637 | 4870 | =end original |
3638 | 4871 | |
3639 | 4872 | 言い換えると、これは完全な再帰スタックを調べるわけではありません。 |
3640 | 4873 | |
3641 | = | |
4874 | =begin original | |
3642 | 4875 | |
4876 | Full syntax: C<< (?(R1)then|else) >> | |
4877 | ||
4878 | =end original | |
4879 | ||
4880 | 完全な文法: C<< (?(R1)then|else) >> | |
4881 | ||
4882 | =item C<(R&I<NAME>)> | |
4883 | ||
3643 | 4884 | =begin original |
3644 | 4885 | |
3645 | 4886 | Similar to C<(R1)>, this predicate checks to see if we're executing |
3646 | 4887 | directly inside of the leftmost group with a given name (this is the same |
3647 | logic used by C<(?&NAME)> to disambiguate). It does not check the full | |
4888 | logic used by C<(?&I<NAME>)> to disambiguate). It does not check the full | |
3648 | 4889 | stack, but only the name of the innermost active recursion. |
4890 | Full syntax: C<< (?(R&name)then|else) >> | |
3649 | 4891 | |
3650 | 4892 | =end original |
3651 | 4893 | |
3652 | 4894 | C<(R1)> と似ていて、この述語はその名前のつけられている一番左のグループの |
3653 | すぐ内側で実行されているかどうかをしらべます(一番左は C<(?NAME)> と | |
4895 | すぐ内側で実行されているかどうかをしらべます(一番左は C<(?&I<NAME>)> と | |
3654 | 4896 | 同じロジックです)。 |
3655 | 4897 | これは完全なスタックを調べずに、一番内部のアクティブな再帰の名前だけを |
3656 | 4898 | 調べます。 |
4899 | 完全な文法: C<< (?(R&name)then|else) >> | |
3657 | 4900 | |
3658 | =item (DEFINE) | |
4901 | =item C<(DEFINE)> | |
3659 | 4902 | |
3660 | 4903 | =begin original |
3661 | 4904 | |
3662 | 4905 | In this case, the yes-pattern is never directly executed, and no |
3663 | 4906 | no-pattern is allowed. Similar in spirit to C<(?{0})> but more efficient. |
3664 | 4907 | See below for details. |
4908 | Full syntax: C<< (?(DEFINE)definitions...) >> | |
3665 | 4909 | |
3666 | 4910 | =end original |
3667 | 4911 | |
3668 | 4912 | この場合において、yes-pattern は直接は実行されず、no-pattern は |
3669 | 4913 | 許可されていません。 |
3670 | 4914 | C<(?{0})> と似ていますがより効率的です。 |
3671 | 4915 | 詳細は次のようになります。 |
4916 | 完全な文法: C<< (?(DEFINE)definitions...) >> | |
3672 | 4917 | |
3673 | 4918 | =back |
3674 | 4919 | |
3675 | 4920 | =begin original |
3676 | 4921 | |
3677 | 4922 | For example: |
3678 | 4923 | |
3679 | 4924 | =end original |
3680 | 4925 | |
3681 | 4926 | 例: |
3682 | 4927 | |
3683 | 4928 | m{ ( \( )? |
3684 | 4929 | [^()]+ |
3685 | 4930 | (?(1) \) ) |
3686 | 4931 | }x |
3687 | 4932 | |
3688 | 4933 | =begin original |
3689 | 4934 | |
3690 | 4935 | matches a chunk of non-parentheses, possibly included in parentheses |
3691 | 4936 | themselves. |
3692 | 4937 | |
3693 | 4938 | =end original |
3694 | 4939 | |
3695 | 4940 | これはかっこ以外からなる固まりかかっこの中にあるそれらにマッチングします。 |
3696 | 4941 | |
3697 | 4942 | =begin original |
3698 | 4943 | |
3699 | 4944 | A special form is the C<(DEFINE)> predicate, which never executes its |
3700 | 4945 | yes-pattern directly, and does not allow a no-pattern. This allows one to |
3701 | 4946 | define subpatterns which will be executed only by the recursion mechanism. |
3702 | 4947 | This way, you can define a set of regular expression rules that can be |
3703 | 4948 | bundled into any pattern you choose. |
3704 | 4949 | |
3705 | 4950 | =end original |
3706 | 4951 | |
3707 | 4952 | C<(DEFINE)> は特殊な形式で、これはその yes-pattern を直接は実行せず、 |
3708 | 4953 | no-pattern も許可していません。 |
3709 | 4954 | これは再帰メカニズムの中で利用することでのみ実行されるサブパターンの |
3710 | 4955 | 定義を許可します。 |
3711 | 4956 | これによって、選んだパターンと一緒に正規表現ルールを定義できます。 |
3712 | 4957 | |
3713 | 4958 | =begin original |
3714 | 4959 | |
3715 | 4960 | It is recommended that for this usage you put the DEFINE block at the |
3716 | 4961 | end of the pattern, and that you name any subpatterns defined within it. |
3717 | 4962 | |
3718 | 4963 | =end original |
3719 | 4964 | |
3720 | 4965 | この使い方において、DEFINE ブロックはパターンの最後におくこと、 |
3721 | 4966 | そしてそこで定義する全てのサブパターンに名前をつけることが |
3722 | 4967 | 推奨されています。 |
3723 | 4968 | |
3724 | 4969 | =begin original |
3725 | 4970 | |
3726 | 4971 | Also, it's worth noting that patterns defined this way probably will |
3727 | not be as efficient, as the optimi | |
4972 | not be as efficient, as the optimizer is not very clever about | |
3728 | 4973 | handling them. |
3729 | 4974 | |
3730 | 4975 | =end original |
3731 | 4976 | |
3732 | 4977 | また、この方法によって定義されるパターンはその処理に関してそんなに |
3733 | 4978 | 賢い訳ではないので効率的でないことに価値は何もないでしょう。 |
3734 | 4979 | |
3735 | 4980 | =begin original |
3736 | 4981 | |
3737 | 4982 | An example of how this might be used is as follows: |
3738 | 4983 | |
3739 | 4984 | =end original |
3740 | 4985 | |
3741 | 4986 | これをどのように使うかの例を次に示します: |
3742 | 4987 | |
3743 | 4988 | /(?<NAME>(?&NAME_PAT))(?<ADDR>(?&ADDRESS_PAT)) |
3744 | 4989 | (?(DEFINE) |
3745 | 4990 | (?<NAME_PAT>....) |
3746 | (?<ADRESS_PAT>....) | |
4991 | (?<ADDRESS_PAT>....) | |
3747 | 4992 | )/x |
3748 | 4993 | |
3749 | 4994 | =begin original |
3750 | 4995 | |
3751 | 4996 | Note that capture groups matched inside of recursion are not accessible |
3752 | 4997 | after the recursion returns, so the extra layer of capturing groups is |
3753 | 4998 | necessary. Thus C<$+{NAME_PAT}> would not be defined even though |
3754 | 4999 | C<$+{NAME}> would be. |
3755 | 5000 | |
3756 | 5001 | =end original |
3757 | 5002 | |
3758 | 5003 | 再帰の内側でマッチングした捕捉グループは再帰から戻った後には |
3759 | 5004 | アクセスできないため、余分な捕捉グループの |
3760 | 5005 | レイヤは必要な点に注意してください。 |
3761 | 5006 | 従って C<$+{NAME}> が定義されていても C<$+{NAME_PAT}> は定義されません。 |
3762 | 5007 | |
3763 | 5008 | =begin original |
3764 | 5009 | |
3765 | 5010 | Finally, keep in mind that subpatterns created inside a DEFINE block |
3766 | 5011 | count towards the absolute and relative number of captures, so this: |
3767 | 5012 | |
3768 | 5013 | =end original |
3769 | 5014 | |
3770 | 5015 | 最後に、DEFINE ブロックの内側で作られた副パターンは捕捉の絶対及び |
3771 | 5016 | 相対番号で数えることに注意してください; 従ってこうすると: |
3772 | 5017 | |
3773 | 5018 | my @captures = "a" =~ /(.) # First capture |
3774 | 5019 | (?(DEFINE) |
3775 | 5020 | (?<EXAMPLE> 1 ) # Second capture |
3776 | 5021 | )/x; |
3777 | 5022 | say scalar @captures; |
3778 | 5023 | |
3779 | 5024 | =begin original |
3780 | 5025 | |
3781 | 5026 | Will output 2, not 1. This is particularly important if you intend to |
3782 | 5027 | compile the definitions with the C<qr//> operator, and later |
3783 | 5028 | interpolate them in another pattern. |
3784 | 5029 | |
3785 | 5030 | =end original |
3786 | 5031 | |
3787 | 5032 | 1 ではなく 2 を出力します。 |
3788 | 5033 | これは、C<qr//> 演算子で定義をコンパイルして、 |
3789 | 5034 | 後で他のパターンの中で展開することを意図している場合に特に重要です。 |
3790 | 5035 | |
3791 | 5036 | =item C<< (?>pattern) >> |
5037 | ||
5038 | =item C<< (*atomic:pattern) >> | |
5039 | X<(?E<gt>pattern)> | |
5040 | X<(*atomic> | |
3792 | 5041 | X<backtrack> X<backtracking> X<atomic> X<possessive> |
3793 | 5042 | |
3794 | 5043 | =begin original |
3795 | 5044 | |
3796 | 5045 | An "independent" subexpression, one which matches the substring |
3797 | 5046 | that a I<standalone> C<pattern> would match if anchored at the given |
3798 | 5047 | position, and it matches I<nothing other than this substring>. This |
3799 | 5048 | construct is useful for optimizations of what would otherwise be |
3800 | "eternal" matches, because it will not backtrack (see L<"Backtracking">). | |
5049 | "eternal" matches, because it will not backtrack (see L</"Backtracking">). | |
3801 | 5050 | It may also be useful in places where the "grab all you can, and do not |
3802 | 5051 | give anything back" semantic is desirable. |
3803 | 5052 | |
3804 | 5053 | =end original |
3805 | 5054 | |
3806 | 5055 | 「独立した」部分式、I<スタンドアロンの> C<pattern> がその場所に |
3807 | 5056 | 固定されてマッチングする部分文字列にマッチングし、 |
3808 | 5057 | I<その文字列以外にはなにも>マッチングしません。 |
3809 | 5058 | この構成子は他の"外部"マッチングになる最適化に便利です; |
3810 | なぜならこれはバックトラックしないためです(L<"Backtracking"> 参照)。 | |
5059 | なぜならこれはバックトラックしないためです(L</"Backtracking"> 参照)。 | |
3811 | 5060 | これは "できる限りを取り込んで、後は戻らない"セマンティクスが |
3812 | 5061 | 必要な場所でも便利です。 |
3813 | 5062 | |
3814 | 5063 | =begin original |
3815 | 5064 | |
3816 | 5065 | For example: C<< ^(?>a*)ab >> will never match, since C<< (?>a*) >> |
3817 | 5066 | (anchored at the beginning of string, as above) will match I<all> |
3818 | characters C<a> at the beginning of string, leaving no C<a> for | |
5067 | characters C<"a"> at the beginning of string, leaving no C<"a"> for | |
3819 | 5068 | C<ab> to match. In contrast, C<a*ab> will match the same as C<a+b>, |
3820 | 5069 | since the match of the subgroup C<a*> is influenced by the following |
3821 | group C<ab> (see L<"Backtracking">). In particular, C<a*> inside | |
5070 | group C<ab> (see L</"Backtracking">). In particular, C<a*> inside | |
3822 | 5071 | C<a*ab> will match fewer characters than a standalone C<a*>, since |
3823 | 5072 | this makes the tail match. |
3824 | 5073 | |
3825 | 5074 | =end original |
3826 | 5075 | |
3827 | 5076 | 例: C<< ^(?>a*)ab >> は何もマッチングしません、 |
3828 | 5077 | なぜなら C<< (?>a*) >> (前述のように、文字列の開始で固定されます)は |
3829 | 文字列のはじめにある全ての文字 C<a> にマッチングし、 | |
5078 | 文字列のはじめにある全ての文字 C<"a"> にマッチングし、 | |
3830 | C<ab> のマッチングのための C<a> を残さないためです。 | |
5079 | C<ab> のマッチングのための C<"a"> を残さないためです。 | |
3831 | 5080 | 対照的に、C<a*ab> は C<a+b> と同じようにマッチングします、 |
3832 | 5081 | これはサブグループ C<a*> のマッチングは次のグループ C<ab> の影響を |
3833 | 受けるためです (L<"Backtracking"> 参照)。 | |
5082 | 受けるためです (L</"Backtracking"> 参照)。 | |
3834 | 5083 | 特に、C<a*ab> の中の C<a*> は単独の C<a*> より短い文字にマッチングします; |
3835 | 5084 | これによって最後のマッチングが行えるようになります。 |
3836 | 5085 | |
3837 | 5086 | =begin original |
3838 | 5087 | |
3839 | 5088 | C<< (?>pattern) >> does not disable backtracking altogether once it has |
3840 | 5089 | matched. It is still possible to backtrack past the construct, but not |
3841 | 5090 | into it. So C<< ((?>a*)|(?>b*))ar >> will still match "bar". |
3842 | 5091 | |
3843 | 5092 | =end original |
3844 | 5093 | |
3845 | 5094 | C<< (?>pattern) >> は、一旦マッチングしたら、全くバックトラックを |
3846 | 5095 | 無効にしません。 |
3847 | 5096 | 未だこの構文の前までバックトラックする可能性はありますが、構文の中に |
3848 | 5097 | バックトラックすることはありません。 |
3849 | 5098 | 従って C<< ((?>a*)|(?>b*))ar >> は "bar" にマッチングするままです。 |
3850 | 5099 | |
3851 | 5100 | =begin original |
3852 | 5101 | |
3853 | 5102 | An effect similar to C<< (?>pattern) >> may be achieved by writing |
3854 | 5103 | C<(?=(pattern))\g{-1}>. This matches the same substring as a standalone |
3855 | 5104 | C<a+>, and the following C<\g{-1}> eats the matched string; it therefore |
3856 | 5105 | makes a zero-length assertion into an analogue of C<< (?>...) >>. |
3857 | 5106 | (The difference between these two constructs is that the second one |
3858 | 5107 | uses a capturing group, thus shifting ordinals of backreferences |
3859 | 5108 | in the rest of a regular expression.) |
3860 | 5109 | |
3861 | 5110 | =end original |
3862 | 5111 | |
3863 | 5112 | C<< (?>pattern) >> と似た効果は C<(?=(pattern))\g{-1}> でも達成できます。 |
3864 | 5113 | これは単独の C<a+> と同じ部分文字列にマッチングし、それに続く C<\g{-1}> が |
3865 | 5114 | マッチングした文字列を消費します; |
3866 | 5115 | これはゼロ幅の言明が C<< (?>...) >> の類似を作るためです。 |
3867 | 5116 | (この2つの構成子は後者はグループをキャプチャするため、 |
3868 | 5117 | それに続く正規表現の残りで後方参照の順序をずらす点で違いがあります。) |
3869 | 5118 | |
3870 | 5119 | =begin original |
3871 | 5120 | |
3872 | 5121 | Consider this pattern: |
3873 | 5122 | |
3874 | 5123 | =end original |
3875 | 5124 | |
3876 | 5125 | 次のパターンを考えてみてください: |
3877 | 5126 | |
3878 | 5127 | m{ \( |
3879 | 5128 | ( |
3880 | 5129 | [^()]+ # x+ |
3881 | 5130 | | |
3882 | 5131 | \( [^()]* \) |
3883 | 5132 | )+ |
3884 | 5133 | \) |
3885 | 5134 | }x |
3886 | 5135 | |
3887 | 5136 | =begin original |
3888 | 5137 | |
3889 | 5138 | That will efficiently match a nonempty group with matching parentheses |
3890 | 5139 | two levels deep or less. However, if there is no such group, it |
3891 | 5140 | will take virtually forever on a long string. That's because there |
3892 | 5141 | are so many different ways to split a long string into several |
3893 | 5142 | substrings. This is what C<(.+)+> is doing, and C<(.+)+> is similar |
3894 | 5143 | to a subpattern of the above pattern. Consider how the pattern |
3895 | 5144 | above detects no-match on C<((()aaaaaaaaaaaaaaaaaa> in several |
3896 | 5145 | seconds, but that each extra letter doubles this time. This |
3897 | 5146 | exponential performance will make it appear that your program has |
3898 | 5147 | hung. However, a tiny change to this pattern |
3899 | 5148 | |
3900 | 5149 | =end original |
3901 | 5150 | |
3902 | 5151 | これは 2 段階までのかっこでくるまれた空でないグループに効率的に |
3903 | 5152 | マッチングします。 |
3904 | 5153 | しかしながら、これはマッチングするグループがなかったときに長い |
3905 | 5154 | 文字列においてはほとんど永遠に戻りません。 |
3906 | 5155 | これは長い文字列をいくつかの部分文字列に分解する方法がいくつもあるためです。 |
3907 | 5156 | これは C<(.+)+> が行うことでもあり、C<(.+)+> は このパターンの |
3908 | 5157 | 部分パターンと似ています。 |
3909 | 5158 | このパターンが C<((()aaaaaaaaaaaaaaaaaa> にはマッチングしないことを |
3910 | 5159 | どうやって検出するかを少し考えてみましょう、 |
3911 | 5160 | しかしここでは余計な文字を2倍にしてみます。 |
3912 | 5161 | この指数的なパフォーマンスはプログラムのハングアップとして表面化します。 |
3913 | 5162 | しかしながら、このパターンに小さな変更をいれてみます, |
3914 | 5163 | |
3915 | 5164 | m{ \( |
3916 | 5165 | ( |
3917 | 5166 | (?> [^()]+ ) # change x+ above to (?> x+ ) |
3918 | 5167 | | |
3919 | 5168 | \( [^()]* \) |
3920 | 5169 | )+ |
3921 | 5170 | \) |
3922 | 5171 | }x |
3923 | 5172 | |
3924 | 5173 | =begin original |
3925 | 5174 | |
3926 | 5175 | which uses C<< (?>...) >> matches exactly when the one above does (verifying |
3927 | 5176 | this yourself would be a productive exercise), but finishes in a fourth |
3928 | the time when used on a similar string with 1000000 C<a>s. Be aware, | |
5177 | the time when used on a similar string with 1000000 C<"a">s. Be aware, | |
3929 | 5178 | however, that, when this construct is followed by a |
3930 | 5179 | quantifier, it currently triggers a warning message under |
3931 | 5180 | the C<use warnings> pragma or B<-w> switch saying it |
3932 | 5181 | C<"matches null string many times in regex">. |
3933 | 5182 | |
3934 | 5183 | =end original |
3935 | 5184 | |
3936 | 5185 | これは上で行っているように C<< (?>...) >> マッチングを |
3937 | 5186 | 使っています(これは自身で確認してみるとよいでしょう)が、 |
3938 | しかし 1000000 個の C<a> からなる似た文字列を使ってみると、4 分の 1 の | |
5187 | しかし 1000000 個の C<"a"> からなる似た文字列を使ってみると、4 分の 1 の | |
3939 | 5188 | 時間で完了します。 |
3940 | 5189 | しかしながら、この構文は量指定子が引き続くと現在のところ |
3941 | 5190 | C<use warnings> プラグマまたは B<-w> スイッチの影響下では |
3942 | 5191 | C<"matches null string many times in regex"> |
3943 | 5192 | (正規表現において空文字列に何回もマッチングしました) という警告を |
3944 | 5193 | 発するでしょう。 |
3945 | 5194 | |
3946 | 5195 | =begin original |
3947 | 5196 | |
3948 | 5197 | On simple groups, such as the pattern C<< (?> [^()]+ ) >>, a comparable |
3949 | effect may be achieved by negative look | |
5198 | effect may be achieved by negative lookahead, as in C<[^()]+ (?! [^()] )>. | |
3950 | This was only 4 times slower on a string with 1000000 C<a>s. | |
5199 | This was only 4 times slower on a string with 1000000 C<"a">s. | |
3951 | 5200 | |
3952 | 5201 | =end original |
3953 | 5202 | |
3954 | 5203 | パターン C<< (?> [^()]+ ) >> のような簡単なグループでは、 |
3955 | 5204 | 比較できる影響は C<[^()]+ (?! [^()] )> のように負の先読みの |
3956 | 5205 | 言明で達することができます。 |
3957 | これは 1000000 個の C<a> からなる文字列において 4 倍だけ遅くなります。 | |
5206 | これは 1000000 個の C<"a"> からなる文字列において 4 倍だけ遅くなります。 | |
3958 | 5207 | |
3959 | 5208 | =begin original |
3960 | 5209 | |
3961 | 5210 | The "grab all you can, and do not give anything back" semantic is desirable |
3962 | 5211 | in many situations where on the first sight a simple C<()*> looks like |
3963 | 5212 | the correct solution. Suppose we parse text with comments being delimited |
3964 | by C<#> followed by some optional (horizontal) whitespace. Contrary to | |
5213 | by C<"<#ins>"> followed by some optional (horizontal) whitespace. Contrary to | |
3965 | 5214 | its appearance, C<#[ \t]*> I<is not> the correct subexpression to match |
3966 | 5215 | the comment delimiter, because it may "give up" some whitespace if |
3967 | 5216 | the remainder of the pattern can be made to match that way. The correct |
3968 | 5217 | answer is either one of these: |
3969 | 5218 | |
3970 | 5219 | =end original |
3971 | 5220 | |
3972 | 5221 | 最初の C<()*> のような正しい解法となる多くの状況において |
3973 | 5222 | 「できる限りを取り込んで、後は戻らない」セマンティクスが望まれるものです。 |
3974 | 任意で(水平)空白の続く C<#> によって区切られるコメントのついたテキストの | |
5223 | 任意で(水平)空白の続く C<"<#ins>"> によって区切られるコメントのついたテキストの | |
3975 | 5224 | パースを考えてみます。 |
3976 | 5225 | その出現と対比して、C<#[ \t]*> はコメント区切りにマッチングする |
3977 | 5226 | 正しい部分式ではありません; なぜならパターンの残りがそれのマッチングを |
3978 | 5227 | 作ることができるのならそれはいくつかの空白を「あきらめてしまう」ためです。 |
3979 | 5228 | 正しい回答は以下のいずれかです: |
3980 | 5229 | |
3981 | 5230 | (?>#[ \t]*) |
3982 | 5231 | #[ \t]*(?![ \t]) |
3983 | 5232 | |
3984 | 5233 | =begin original |
3985 | 5234 | |
3986 | For example, to grab non-empty comments into $1, one should use either | |
5235 | For example, to grab non-empty comments into C<$1>, one should use either | |
3987 | 5236 | one of these: |
3988 | 5237 | |
3989 | 5238 | =end original |
3990 | 5239 | |
3991 | 例えば空でないコメントを $1 に取り込むためには次のいずれかを使います: | |
5240 | 例えば空でないコメントを C<$1> に取り込むためには次のいずれかを使います: | |
3992 | 5241 | |
3993 | 5242 | / (?> \# [ \t]* ) ( .+ ) /x; |
3994 | 5243 | / \# [ \t]* ( [^ \t] .* ) /x; |
3995 | 5244 | |
3996 | 5245 | =begin original |
3997 | 5246 | |
3998 | 5247 | Which one you pick depends on which of these expressions better reflects |
3999 | 5248 | the above specification of comments. |
4000 | 5249 | |
4001 | 5250 | =end original |
4002 | 5251 | |
4003 | 5252 | 選んだ方はコメントの仕様をより適切に反映した式に依存します。 |
4004 | 5253 | |
4005 | 5254 | =begin original |
4006 | 5255 | |
4007 | 5256 | In some literature this construct is called "atomic matching" or |
4008 | 5257 | "possessive matching". |
4009 | 5258 | |
4010 | 5259 | =end original |
4011 | 5260 | |
4012 | 5261 | いくつかの書籍においてこの構成子は「アトミックなマッチング」 |
4013 | 5262 | または「絶対最大量マッチング(possessive matching)」と呼ばれます。 |
4014 | 5263 | |
4015 | 5264 | =begin original |
4016 | 5265 | |
4017 | 5266 | Possessive quantifiers are equivalent to putting the item they are applied |
4018 | 5267 | to inside of one of these constructs. The following equivalences apply: |
4019 | 5268 | |
4020 | 5269 | =end original |
4021 | 5270 | |
4022 | 5271 | 絶対最大量指定子はそれが適用されている項目をこれらの構成子の中に置くことと |
4023 | 5272 | 等価です。 |
4024 | 5273 | 以下の等式が適用されます: |
4025 | 5274 | |
4026 | 5275 | Quantifier Form Bracketing Form |
4027 | 5276 | --------------- --------------- |
4028 | 5277 | PAT*+ (?>PAT*) |
4029 | 5278 | PAT++ (?>PAT+) |
4030 | 5279 | PAT?+ (?>PAT?) |
4031 | 5280 | PAT{min,max}+ (?>PAT{min,max}) |
4032 | 5281 | |
5282 | =begin original | |
5283 | ||
5284 | Nested C<(?E<gt>...)> constructs are not no-ops, even if at first glance | |
5285 | they might seem to be. This is because the nested C<(?E<gt>...)> can | |
5286 | restrict internal backtracking that otherwise might occur. For example, | |
5287 | ||
5288 | =end original | |
5289 | ||
5290 | ネストした C<(?E<gt>...)> 構文は、 | |
5291 | たとえ一見何もしないように見えても、何もしないものではありません。 | |
5292 | これは、ネストした C<(?E<gt>...)> は、なければ起きるかもしれない | |
5293 | 内部バックトラックを制限するからです。 | |
5294 | 例えば: | |
5295 | ||
5296 | "abc" =~ /(?>a[bc]*c)/ | |
5297 | ||
5298 | =begin original | |
5299 | ||
5300 | matches, but | |
5301 | ||
5302 | =end original | |
5303 | ||
5304 | これはマッチングしますが: | |
5305 | ||
5306 | "abc" =~ /(?>a(?>[bc]*)c)/ | |
5307 | ||
5308 | =begin original | |
5309 | ||
5310 | does not. | |
5311 | ||
5312 | =end original | |
5313 | ||
5314 | これはマッチングしません。 | |
5315 | ||
5316 | =begin original | |
5317 | ||
5318 | The alphabetic form (C<(*atomic:...)>) is experimental; using it | |
5319 | yields a warning in the C<experimental::alpha_assertions> category. | |
5320 | ||
5321 | =end original | |
5322 | ||
5323 | 英字形式 (C<(*atomic:...)>) は実験的です; これを使うと | |
5324 | C<experimental::alpha_assertions> カテゴリの警告が出ます。 | |
5325 | ||
5326 | =item C<(?[ ])> | |
5327 | ||
5328 | =begin original | |
5329 | ||
5330 | See L<perlrecharclass/Extended Bracketed Character Classes>. | |
5331 | ||
5332 | =end original | |
5333 | ||
5334 | L<perlrecharclass/Extended Bracketed Character Classes> を参照してください。 | |
5335 | ||
5336 | =begin original | |
5337 | ||
5338 | Note that this feature is currently L<experimental|perlpolicy/experimental>; | |
5339 | using it yields a warning in the C<experimental::regex_sets> category. | |
5340 | ||
5341 | =end original | |
5342 | ||
5343 | この機能は現在 L<実験的|perlpolicy/experimental> です; | |
5344 | これを使うと C<experimental::regex_sets> カテゴリの警告が発生します。 | |
5345 | ||
4033 | 5346 | =back |
4034 | 5347 | |
4035 | =head2 | |
5348 | =head2 Backtracking | |
5349 | X<backtrack> X<backtracking> | |
4036 | 5350 | |
4037 | ( | |
5351 | (バックトラック) | |
4038 | 5352 | |
4039 | 5353 | =begin original |
4040 | 5354 | |
4041 | ||
5355 | NOTE: This section presents an abstract approximation of regular | |
4042 | ||
5356 | expression behavior. For a more rigorous (and complicated) view of | |
4043 | ||
5357 | the rules involved in selecting a match among possible alternatives, | |
5358 | see L</Combining RE Pieces>. | |
4044 | 5359 | |
4045 | 5360 | =end original |
4046 | 5361 | |
4047 | ||
5362 | 補足: このセクションでは正規表現の振る舞いに関する抽象的な概要を | |
4048 | ||
5363 | 説明します。 | |
4049 | ||
5364 | 可能な代替におけるマッチングの選択におけるルールの厳密な(そして複雑な) | |
4050 | ||
5365 | 説明は L</Combining RE Pieces> を参照してください。 | |
4051 | 5366 | |
4052 | 5367 | =begin original |
4053 | 5368 | |
4054 | ||
5369 | A fundamental feature of regular expression matching involves the | |
4055 | ot | |
5370 | notion called I<backtracking>, which is currently used (when needed) | |
4056 | ||
5371 | by all regular non-possessive expression quantifiers, namely C<"*">, C<*?>, C<"+">, | |
5372 | C<+?>, C<{n,m}>, and C<{n,m}?>. Backtracking is often optimized | |
5373 | internally, but the general principle outlined here is valid. | |
4057 | 5374 | |
4058 | 5375 | =end original |
4059 | 5376 | |
4060 | ||
5377 | 正規表現マッチングの基本的な機能には最近(必要であれば)すべての強欲でない | |
4061 | ||
5378 | 正規表現量指定子、つまり、 | |
4062 | ||
5379 | C<"*">, C<*?>, C<"+">, C<+?>, C<{n,m}>, C<{n,m}?> で | |
5380 | 使われる I<バックトラッキング> と呼ばれる概念が含まれています。 | |
5381 | バックトラックはしばしば内部で最適化されますが、ここで概説する一般的な | |
5382 | 原則は妥当です。 | |
4063 | 5383 | |
4064 | 5384 | =begin original |
4065 | 5385 | |
5386 | For a regular expression to match, the I<entire> regular expression must | |
5387 | match, not just part of it. So if the beginning of a pattern containing a | |
5388 | quantifier succeeds in a way that causes later parts in the pattern to | |
5389 | fail, the matching engine backs up and recalculates the beginning | |
5390 | part--that's why it's called backtracking. | |
5391 | ||
5392 | =end original | |
5393 | ||
5394 | 正規表現がマッチングする時、その正規表現の一部ではなく、 | |
5395 | I<全体> がマッチングしなければなりません。 | |
5396 | そのためもしパターンの前半にパターンの後半部分を失敗させてしまう | |
5397 | 量指定子が含まれているのなら、マッチングングエンジンはいったん戻って | |
5398 | 開始位置を再計算します -- これがバックトラッキングと呼ばれる所以です。 | |
5399 | ||
5400 | =begin original | |
5401 | ||
5402 | Here is an example of backtracking: Let's say you want to find the | |
5403 | word following "foo" in the string "Food is on the foo table.": | |
5404 | ||
5405 | =end original | |
5406 | ||
5407 | バックトラッキングの例をあげてみます: "Food is on the foo table." という | |
5408 | 文字列の中で "foo" に続く単語を取り出してください: | |
5409 | ||
5410 | $_ = "Food is on the foo table."; | |
5411 | if ( /\b(foo)\s+(\w+)/i ) { | |
5412 | print "$2 follows $1.\n"; | |
5413 | } | |
5414 | ||
5415 | =begin original | |
5416 | ||
5417 | When the match runs, the first part of the regular expression (C<\b(foo)>) | |
5418 | finds a possible match right at the beginning of the string, and loads up | |
5419 | C<$1> with "Foo". However, as soon as the matching engine sees that there's | |
5420 | no whitespace following the "Foo" that it had saved in C<$1>, it realizes its | |
5421 | mistake and starts over again one character after where it had the | |
5422 | tentative match. This time it goes all the way until the next occurrence | |
5423 | of "foo". The complete regular expression matches this time, and you get | |
5424 | the expected output of "table follows foo." | |
5425 | ||
5426 | =end original | |
5427 | ||
5428 | マッチングが実行される時、正規表現の最初の部分 (C<\b(foo)>) は開始文字列の | |
5429 | 右側で可能なマッチングを探します; そして C<$1> に "Foo" をロードします。 | |
5430 | しかし、すぐにマッチングエンジンは C<$1> に保存した "Foo" の後に空白が | |
5431 | 無いことを見つけ、それが失敗だったことを検出して仮にマッチングさせた | |
5432 | 場所の 1 文字後から開始します。 | |
5433 | この時次の "foo" の出現まで進みます。 | |
5434 | この時に正規表現は完全にマッチングし、予測した出力 "table follows foo." を | |
5435 | 得ます。 | |
5436 | ||
5437 | =begin original | |
5438 | ||
5439 | Sometimes minimal matching can help a lot. Imagine you'd like to match | |
5440 | everything between "foo" and "bar". Initially, you write something | |
5441 | like this: | |
5442 | ||
5443 | =end original | |
5444 | ||
5445 | 最小マッチングが役立つこともあります。 | |
5446 | "foo" と "bar" の間の全てにマッチングしたいと考えてください。 | |
5447 | 最初に、次のように書くかもしれません: | |
5448 | ||
5449 | $_ = "The food is under the bar in the barn."; | |
5450 | if ( /foo(.*)bar/ ) { | |
5451 | print "got <$1>\n"; | |
5452 | } | |
5453 | ||
5454 | =begin original | |
5455 | ||
5456 | Which perhaps unexpectedly yields: | |
5457 | ||
5458 | =end original | |
5459 | ||
5460 | しかしこれは考えたのと違う結果となるでしょう: | |
5461 | ||
5462 | got <d is under the bar in the > | |
5463 | ||
5464 | =begin original | |
5465 | ||
5466 | That's because C<.*> was greedy, so you get everything between the | |
5467 | I<first> "foo" and the I<last> "bar". Here it's more effective | |
5468 | to use minimal matching to make sure you get the text between a "foo" | |
5469 | and the first "bar" thereafter. | |
5470 | ||
5471 | =end original | |
5472 | ||
5473 | これは C<.*> が貪欲であり、そのために I<最初の> "foo" と I<最後の> | |
5474 | "bar" の間にある全てを取り出してしまいます。 | |
5475 | 次に "foo" とその後の最初の "bar" の間にあるテキストを取り出す | |
5476 | 最小マッチングを使ったもっと効率的な方法を示します: | |
5477 | ||
5478 | if ( /foo(.*?)bar/ ) { print "got <$1>\n" } | |
5479 | got <d is under the > | |
5480 | ||
5481 | =begin original | |
5482 | ||
5483 | Here's another example. Let's say you'd like to match a number at the end | |
5484 | of a string, and you also want to keep the preceding part of the match. | |
5485 | So you write this: | |
5486 | ||
5487 | =end original | |
5488 | ||
5489 | 別の例も出してみます。 | |
5490 | 文字列の最後にある数字にマッチングさせて、そのマッチングの前の部分も | |
5491 | 保持させてみましょう。 | |
5492 | そしてあなたは次のように書くかもしれません。 | |
5493 | ||
5494 | $_ = "I have 2 numbers: 53147"; | |
5495 | if ( /(.*)(\d*)/ ) { # Wrong! | |
5496 | print "Beginning is <$1>, number is <$2>.\n"; | |
5497 | } | |
5498 | ||
5499 | =begin original | |
5500 | ||
5501 | That won't work at all, because C<.*> was greedy and gobbled up the | |
5502 | whole string. As C<\d*> can match on an empty string the complete | |
5503 | regular expression matched successfully. | |
5504 | ||
5505 | =end original | |
5506 | ||
5507 | これは全く動作しません、なぜなら C<.*> は貪欲であり文字列全体を | |
5508 | 飲み込んでしまいます。 | |
5509 | C<\d*> は空の文字列にマッチングできるので正規表現は完全に正常に | |
5510 | マッチングします。 | |
5511 | ||
5512 | Beginning is <I have 2 numbers: 53147>, number is <>. | |
5513 | ||
5514 | =begin original | |
5515 | ||
5516 | Here are some variants, most of which don't work: | |
5517 | ||
5518 | =end original | |
5519 | ||
5520 | 動作しない主なバリエーションをあげておきます: | |
5521 | ||
5522 | $_ = "I have 2 numbers: 53147"; | |
5523 | @pats = qw{ | |
5524 | (.*)(\d*) | |
5525 | (.*)(\d+) | |
5526 | (.*?)(\d*) | |
5527 | (.*?)(\d+) | |
5528 | (.*)(\d+)$ | |
5529 | (.*?)(\d+)$ | |
5530 | (.*)\b(\d+)$ | |
5531 | (.*\D)(\d+)$ | |
5532 | }; | |
5533 | ||
5534 | for $pat (@pats) { | |
5535 | printf "%-12s ", $pat; | |
5536 | if ( /$pat/ ) { | |
5537 | print "<$1> <$2>\n"; | |
5538 | } else { | |
5539 | print "FAIL\n"; | |
5540 | } | |
5541 | } | |
5542 | ||
5543 | =begin original | |
5544 | ||
5545 | That will print out: | |
5546 | ||
5547 | =end original | |
5548 | ||
5549 | これらの結果は次のようになります: | |
5550 | ||
5551 | (.*)(\d*) <I have 2 numbers: 53147> <> | |
5552 | (.*)(\d+) <I have 2 numbers: 5314> <7> | |
5553 | (.*?)(\d*) <> <> | |
5554 | (.*?)(\d+) <I have > <2> | |
5555 | (.*)(\d+)$ <I have 2 numbers: 5314> <7> | |
5556 | (.*?)(\d+)$ <I have 2 numbers: > <53147> | |
5557 | (.*)\b(\d+)$ <I have 2 numbers: > <53147> | |
5558 | (.*\D)(\d+)$ <I have 2 numbers: > <53147> | |
5559 | ||
5560 | =begin original | |
5561 | ||
5562 | As you see, this can be a bit tricky. It's important to realize that a | |
5563 | regular expression is merely a set of assertions that gives a definition | |
5564 | of success. There may be 0, 1, or several different ways that the | |
5565 | definition might succeed against a particular string. And if there are | |
5566 | multiple ways it might succeed, you need to understand backtracking to | |
5567 | know which variety of success you will achieve. | |
5568 | ||
5569 | =end original | |
5570 | ||
5571 | このように、これは幾分トリッキーです。 | |
5572 | 重要なのは正規表現は成功の定義を定める主張の集合にすぎないことを | |
5573 | 認識することです。 | |
5574 | 特定の文字列で成功となる定義には 0, 1 または複数の違ったやり方が存在します。 | |
5575 | そしてもし成功する複数の方法が存在するのなら成功したうちのどれが目的と | |
5576 | するものなのかを知るためにバックトラッキングを理解しておく必要があります。 | |
5577 | ||
5578 | =begin original | |
5579 | ||
5580 | When using lookahead assertions and negations, this can all get even | |
5581 | trickier. Imagine you'd like to find a sequence of non-digits not | |
5582 | followed by "123". You might try to write that as | |
5583 | ||
5584 | =end original | |
5585 | ||
5586 | 前読みの言明及び否定を使っている時にはこれはますますトリッキーになります。 | |
5587 | "123" が後ろに続かない数字以外の列を探したいと考えてみてください。 | |
5588 | あなたは次のように書くかもしれません。 | |
5589 | ||
5590 | $_ = "ABC123"; | |
5591 | if ( /^\D*(?!123)/ ) { # Wrong! | |
5592 | print "Yup, no 123 in $_\n"; | |
5593 | } | |
5594 | ||
5595 | =begin original | |
5596 | ||
5597 | But that isn't going to match; at least, not the way you're hoping. It | |
5598 | claims that there is no 123 in the string. Here's a clearer picture of | |
5599 | why that pattern matches, contrary to popular expectations: | |
5600 | ||
5601 | =end original | |
5602 | ||
5603 | ですがこれはマッチングしません; 少なくともなってほしかったようには。 | |
5604 | これは文字列の中に 123 がないことを要求します。 | |
5605 | よくある予想と比較してなぜパターンがマッチングするのかのわかりやすい | |
5606 | 説明を次に示します: | |
5607 | ||
5608 | $x = 'ABC123'; | |
5609 | $y = 'ABC445'; | |
5610 | ||
5611 | print "1: got $1\n" if $x =~ /^(ABC)(?!123)/; | |
5612 | print "2: got $1\n" if $y =~ /^(ABC)(?!123)/; | |
5613 | ||
5614 | print "3: got $1\n" if $x =~ /^(\D*)(?!123)/; | |
5615 | print "4: got $1\n" if $y =~ /^(\D*)(?!123)/; | |
5616 | ||
5617 | =begin original | |
5618 | ||
5619 | This prints | |
5620 | ||
5621 | =end original | |
5622 | ||
5623 | これは次の出力となります | |
5624 | ||
5625 | 2: got ABC | |
5626 | 3: got AB | |
5627 | 4: got ABC | |
5628 | ||
5629 | =begin original | |
5630 | ||
5631 | You might have expected test 3 to fail because it seems to a more | |
5632 | general purpose version of test 1. The important difference between | |
5633 | them is that test 3 contains a quantifier (C<\D*>) and so can use | |
5634 | backtracking, whereas test 1 will not. What's happening is | |
5635 | that you've asked "Is it true that at the start of C<$x>, following 0 or more | |
5636 | non-digits, you have something that's not 123?" If the pattern matcher had | |
5637 | let C<\D*> expand to "ABC", this would have caused the whole pattern to | |
5638 | fail. | |
5639 | ||
5640 | =end original | |
5641 | ||
5642 | テスト 3 はテスト 1 のより一般的なバージョンなのでそれが失敗すると | |
5643 | 考えたかもしれません。 | |
5644 | この 2 つの重要な違いは、テスト 3 には量指定子(C<\D*>)が含まれているので | |
5645 | テスト1ではできなかったバックトラッキングを行うことが | |
5646 | できるところにあります。 | |
5647 | ここであなたは「C<$x> のはじめで 0 個以上の非数字があるから 123 ではない | |
5648 | 何かを得られるんじゃないの?」と聞くでしょう。 | |
5649 | このパターンマッチングが C<\D*> を "ABC" に展開させると | |
5650 | これはパターン全体を失敗させることになります。 | |
5651 | ||
5652 | =begin original | |
5653 | ||
5654 | The search engine will initially match C<\D*> with "ABC". Then it will | |
5655 | try to match C<(?!123)> with "123", which fails. But because | |
5656 | a quantifier (C<\D*>) has been used in the regular expression, the | |
5657 | search engine can backtrack and retry the match differently | |
5658 | in the hope of matching the complete regular expression. | |
5659 | ||
5660 | =end original | |
5661 | ||
5662 | 探索エンジンは最初に C<\D*> を "ABC" にマッチングさせます。 | |
5663 | そして C<(?!123)> を "123" にマッチングさせ、これは失敗します。 | |
5664 | けれども量指定子 (C<\D*>) が正規表現の中で使われているので、探索エンジンは | |
5665 | バックトラックしてこの正規表現全体をマッチングさせるように異なるマッチングを | |
5666 | 行うことができます。 | |
5667 | ||
5668 | =begin original | |
5669 | ||
5670 | The pattern really, I<really> wants to succeed, so it uses the | |
5671 | standard pattern back-off-and-retry and lets C<\D*> expand to just "AB" this | |
5672 | time. Now there's indeed something following "AB" that is not | |
5673 | "123". It's "C123", which suffices. | |
5674 | ||
5675 | =end original | |
5676 | ||
5677 | このパターンは本当に、I<本当に> 成功したいので、これは標準的なパターンの | |
5678 | 後退再試行を行い、この時に C<\D*> を "AB" のみに展開させます。 | |
5679 | そして確かに "AB" の後ろは "123" ではありません。 | |
5680 | "C123" は十分満たしています。 | |
5681 | ||
5682 | =begin original | |
5683 | ||
5684 | We can deal with this by using both an assertion and a negation. | |
5685 | We'll say that the first part in C<$1> must be followed both by a digit | |
5686 | and by something that's not "123". Remember that the lookaheads | |
5687 | are zero-width expressions--they only look, but don't consume any | |
5688 | of the string in their match. So rewriting this way produces what | |
5689 | you'd expect; that is, case 5 will fail, but case 6 succeeds: | |
5690 | ||
5691 | =end original | |
5692 | ||
5693 | これは言明と否定の両方を使うことで処理することができます。 | |
5694 | C<$1> の最初の部分は数字が続きかつそれは "123" ではないことを宣言します。 | |
5695 | 先読みはゼロ幅の式なのでそれがマッチングした文字列を全く消費しないことを | |
5696 | 思い出してください。 | |
5697 | そしてこれを必要なものを生成するように書き換えます; | |
5698 | つまり、5 のケースでは失敗し、6 のケースは成功します: | |
5699 | ||
5700 | print "5: got $1\n" if $x =~ /^(\D*)(?=\d)(?!123)/; | |
5701 | print "6: got $1\n" if $y =~ /^(\D*)(?=\d)(?!123)/; | |
5702 | ||
5703 | 6: got ABC | |
5704 | ||
5705 | =begin original | |
5706 | ||
5707 | In other words, the two zero-width assertions next to each other work as though | |
5708 | they're ANDed together, just as you'd use any built-in assertions: C</^$/> | |
5709 | matches only if you're at the beginning of the line AND the end of the | |
5710 | line simultaneously. The deeper underlying truth is that juxtaposition in | |
5711 | regular expressions always means AND, except when you write an explicit OR | |
5712 | using the vertical bar. C</ab/> means match "a" AND (then) match "b", | |
5713 | although the attempted matches are made at different positions because "a" | |
5714 | is not a zero-width assertion, but a one-width assertion. | |
5715 | ||
5716 | =end original | |
5717 | ||
5718 | 言い換えると、このそれぞれの次にある2つのゼロ幅の言明はちょうど何か組み込みの | |
5719 | 言明を使ったかのようにそれらがともに AND されているかのように動作します: | |
5720 | C</^$/> は行の始まりで且つ同時に行の終了でる時にのみマッチングします。 | |
5721 | もっと深部での真実は、併記された正規表現は垂直線を使って明示的に OR を | |
5722 | 書いたとき以外は常に AND を意味します。 | |
5723 | C</ab/> は、"a" がゼロ幅の言明ではなく 1 文字幅の言明なので異なる場所で | |
5724 | マッチングが行われはしますが、 "a" にマッチング且つ(そして) "b" に | |
5725 | マッチングということを意味します。 | |
5726 | ||
5727 | =begin original | |
5728 | ||
5729 | B<WARNING>: Particularly complicated regular expressions can take | |
5730 | exponential time to solve because of the immense number of possible | |
5731 | ways they can use backtracking to try for a match. For example, without | |
5732 | internal optimizations done by the regular expression engine, this will | |
5733 | take a painfully long time to run: | |
5734 | ||
5735 | =end original | |
5736 | ||
5737 | B<警告>: 特にコンパイルされた正規表現はマッチングのために | |
5738 | できる限りのバックトラックを非常に多くの回数行うので | |
5739 | 解くために指数的な時間を必要とすることがあります。 | |
5740 | 例えば、正規表現エンジンの内部で行われる最適化がなかったときには、次の評価は | |
5741 | 尋常じゃないくらい長時間かかります: | |
5742 | ||
5743 | 'aaaaaaaaaaaa' =~ /((a{0,5}){0,5})*[c]/ | |
5744 | ||
5745 | =begin original | |
5746 | ||
5747 | And if you used C<"*">'s in the internal groups instead of limiting them | |
5748 | to 0 through 5 matches, then it would take forever--or until you ran | |
5749 | out of stack space. Moreover, these internal optimizations are not | |
5750 | always applicable. For example, if you put C<{0,5}> instead of C<"*"> | |
5751 | on the external group, no current optimization is applicable, and the | |
5752 | match takes a long time to finish. | |
5753 | ||
5754 | =end original | |
5755 | ||
5756 | そしてもし内側のグループで 0 から 5 回にマッチングを制限する代わりに | |
5757 | C<"*"> を使うと、永久に、またはスタックを使い果たすまで | |
5758 | 実行し続けることになります。 | |
5759 | その上、これらの最適化は常にできるわけではありません。 | |
5760 | 例えば、外側のグループで C<"*"> の代わりに C<{0,5}> を使ったときに、現在の | |
5761 | 最適化は適用されません; そしてマッチングが終わるまでの長い時間が | |
5762 | 必要になります。 | |
5763 | ||
5764 | =begin original | |
5765 | ||
5766 | A powerful tool for optimizing such beasts is what is known as an | |
5767 | "independent group", | |
5768 | which does not backtrack (see L</C<< (?>pattern) >>>). Note also that | |
5769 | zero-length lookahead/lookbehind assertions will not backtrack to make | |
5770 | the tail match, since they are in "logical" context: only | |
5771 | whether they match is considered relevant. For an example | |
5772 | where side-effects of lookahead I<might> have influenced the | |
5773 | following match, see L</C<< (?>pattern) >>>. | |
5774 | ||
5775 | =end original | |
5776 | ||
5777 | そのような野獣のような最適化のためのパワフルなツールとして | |
5778 | 知られているものに、「独立グループ」があります; これはバックトラックを | |
5779 | 行いません (L</C<< (?>pattern) >>> を参照)。 | |
5780 | ゼロ幅の先読み/後読みの言明も「論理的な」文脈なので末尾のマッチングを | |
5781 | バックトラックしません: マッチングが関連して考慮されるかどうかだけです。 | |
5782 | 先読みの言明の副作用がそれに続くマッチングに影響する I<かもしれない> 例は、 | |
5783 | L</C<< (?>pattern) >>> を参照してください。 | |
5784 | ||
5785 | =head2 Script Runs | |
5786 | X<(*script_run:...)> X<(sr:...)> | |
5787 | X<(*atomic_script_run:...)> X<(asr:...)> | |
5788 | ||
5789 | (用字連続) | |
5790 | ||
5791 | =begin original | |
5792 | ||
5793 | A script run is basically a sequence of characters, all from the same | |
5794 | Unicode script (see L<perlunicode/Scripts>), such as Latin or Greek. In | |
5795 | most places a single word would never be written in multiple scripts, | |
5796 | unless it is a spoofing attack. An infamous example, is | |
5797 | ||
5798 | =end original | |
5799 | ||
5800 | 用字連続は基本的には、ラテン文字やギリシャ文字のような、 | |
5801 | 全て同じ Unicode 用字 (L<perlunicode/Scripts> 参照) からの文字の並びです。 | |
5802 | ほとんどの場所では、なりすまし攻撃でない限り、一つの単語は決して複数の用字で | |
5803 | 書かれることはありません。 | |
5804 | 悪名高い例は次のものです: | |
5805 | ||
5806 | paypal.com | |
5807 | ||
5808 | =begin original | |
5809 | ||
5810 | Those letters could all be Latin (as in the example just above), or they | |
5811 | could be all Cyrillic (except for the dot), or they could be a mixture | |
5812 | of the two. In the case of an internet address the C<.com> would be in | |
5813 | Latin, And any Cyrillic ones would cause it to be a mixture, not a | |
5814 | script run. Someone clicking on such a link would not be directed to | |
5815 | the real Paypal website, but an attacker would craft a look-alike one to | |
5816 | attempt to gather sensitive information from the person. | |
5817 | ||
5818 | =end original | |
5819 | ||
5820 | これらの文字は全て (直前のように) ラテン文字かもしれませんし、 | |
5821 | (ドットを除いて) 全てキリル文字かもしれませんし、二つの混合かも | |
5822 | しれません。 | |
5823 | インターネットアドレスの場合、C<.com> はラテン文字で、 | |
5824 | キリル文字は混合となり用字連続ではありません。 | |
5825 | 誰かがこのようなリンクをクリックすると、本当の Paypal ウェブサイトに | |
5826 | 移動せず、攻撃者がその人から機微情報を集めようとするために | |
5827 | 見た目が似たものを細工するかもしれません。 | |
5828 | ||
5829 | =begin original | |
5830 | ||
5831 | Starting in Perl 5.28, it is now easy to detect strings that aren't | |
5832 | script runs. Simply enclose just about any pattern like either of | |
5833 | these: | |
5834 | ||
5835 | =end original | |
5836 | ||
5837 | Perl 5.28 から、用字連続でない文字列を簡単に検出できるようになりました。 | |
5838 | 単にパターンを次のどちらかのような形で囲みます: | |
5839 | ||
5840 | (*script_run:pattern) | |
5841 | (*sr:pattern) | |
5842 | ||
5843 | =begin original | |
5844 | ||
5845 | What happens is that after I<pattern> succeeds in matching, it is | |
5846 | subjected to the additional criterion that every character in it must be | |
5847 | from the same script (see exceptions below). If this isn't true, | |
5848 | backtracking occurs until something all in the same script is found that | |
5849 | matches, or all possibilities are exhausted. This can cause a lot of | |
5850 | backtracking, but generally, only malicious input will result in this, | |
5851 | though the slow down could cause a denial of service attack. If your | |
5852 | needs permit, it is best to make the pattern atomic. This is so likely | |
5853 | to be what you want, that instead of writing this: | |
5854 | ||
5855 | =end original | |
5856 | ||
5857 | I<pattern> がマッチングした後に次のことが起きます; | |
5858 | その中の全ての文字が同じ用字であるという追加の条件が課されます | |
5859 | (後述する例外参照)。 | |
5860 | これが真でない場合、全て同じ用字でマッチングする何かが見つかるか、 | |
5861 | 全ての可能性がなくなるまで、バックトラッキングが発生します。 | |
5862 | これは多くのバックトラッキングを引き起こしますが、一般的に、 | |
5863 | 悪意のある入力だけがこれを引き起こします; | |
5864 | しかし、この速度低下はサービス不能攻撃を引き起こすかもしれません。 | |
5865 | 事情が許すなら、パターンをアトミックに書くのが最善です。 | |
5866 | これはおそらくあなたが求めているものなので、次のように書く代わりに: | |
5867 | ||
5868 | (*script_run:(?>pattern)) | |
5869 | ||
5870 | =begin original | |
5871 | ||
5872 | you can write either of these: | |
5873 | ||
5874 | =end original | |
5875 | ||
5876 | 次のどちらかのように書けます: | |
5877 | ||
5878 | (*atomic_script_run:pattern) | |
5879 | (*asr:pattern) | |
5880 | ||
5881 | =begin original | |
5882 | ||
5883 | (See L</C<(?E<gt>pattern)>>.) | |
5884 | ||
5885 | =end original | |
5886 | ||
5887 | (L</C<(?E<gt>pattern)>> を参照してください。) | |
5888 | ||
5889 | =begin original | |
5890 | ||
5891 | In Taiwan, Japan, and Korea, it is common for text to have a mixture of | |
5892 | characters from their native scripts and base Chinese. Perl follows | |
5893 | Unicode's UTS 39 (L<http://unicode.org/reports/tr39/>) Unicode Security | |
5894 | Mechanisms in allowing such mixtures. | |
5895 | ||
5896 | =end original | |
5897 | ||
5898 | 台湾、日本、韓国では、独自の用字からの文字と中国語を基にした文字が | |
5899 | 混合している文章は一般的です。 | |
5900 | Perl はこのような混合を許すために Unicode の UTS 39 | |
5901 | (L<http://unicode.org/reports/tr39/>) Unicode Security Mechanisms に | |
5902 | 従います。 | |
5903 | ||
5904 | =begin original | |
5905 | ||
5906 | The rules used for matching decimal digits are somewhat different. Many | |
5907 | scripts have their own sets of digits equivalent to the Western C<0> | |
5908 | through C<9> ones. A few, such as Arabic, have more than one set. For | |
5909 | a string to be considered a script run, all digits in it must come from | |
5910 | the same set, as determined by the first digit encountered. The ASCII | |
5911 | C<[0-9]> are accepted as being in any script, even those that have their | |
5912 | own set. This is because these are often used in commerce even in such | |
5913 | scripts. But any mixing of the ASCII and other digits will cause the | |
5914 | sequence to not be a script run, failing the match. As an example, | |
5915 | ||
5916 | =end original | |
5917 | ||
5918 | 10 進数字とマッチングするために使われる規則は少し異なります。 | |
5919 | 多くの用字は、西洋の C<0> から C<9> と等価な独自の数字の集合を持ちます。 | |
5920 | アラビア文字のように、複数の集合を持つものもあります。 | |
5921 | 用字連続が考慮される文字列については、全ての数字は、遭遇した | |
5922 | 最初の数字で決定されるものと同じ集合でなければなりません。 | |
5923 | たとえ独自の集合を持っている用字でも ASCII の C<[0-9]> は受け入れられます。 | |
5924 | これは、これらがそのような用字でも照合の分野ではしばしば使われるからです。 | |
5925 | しかし ASCII とその他の数字の混合は、用字連続とはならず、 | |
5926 | マッチングに失敗します。 | |
5927 | 例えば: | |
5928 | ||
5929 | qr/(*script_run: \d+ \b )/x | |
5930 | ||
5931 | =begin original | |
5932 | ||
5933 | guarantees that the digits matched will all be from the same set of 10. | |
5934 | You won't get a look-alike digit from a different script that has a | |
5935 | different value than what it appears to be. | |
5936 | ||
5937 | =end original | |
5938 | ||
5939 | これはマッチングした数字が全て同じ集合の 10 文字からであることを保証します。 | |
5940 | 見た目と異なる値を持つ、異なる用字からの数字に見えるものを得ることは | |
5941 | ありません。 | |
5942 | ||
5943 | =begin original | |
5944 | ||
5945 | Unicode has three pseudo scripts that are handled specially. | |
5946 | ||
5947 | =end original | |
5948 | ||
5949 | Unicode には特別に扱われる三つの疑似用字があります。 | |
5950 | ||
5951 | =begin original | |
5952 | ||
5953 | "Unknown" is applied to code points whose meaning has yet to be | |
5954 | determined. Perl currently will match as a script run, any single | |
5955 | character string consisting of one of these code points. But any string | |
5956 | longer than one code point containing one of these will not be | |
5957 | considered a script run. | |
5958 | ||
5959 | =end original | |
5960 | ||
5961 | "Unknown" は、意味がまだ決定されていない符号位置に適用されます。 | |
5962 | Perl は現在の所それらのうちの一つの符号位置からなる 1 文字文字列を | |
5963 | 用字連続としてマッチングします。 | |
5964 | しかし、それらの一つを含む 2 符号位置以上の文字列は用字連続として | |
5965 | 扱われません。 | |
5966 | ||
5967 | =begin original | |
5968 | ||
5969 | "Inherited" is applied to characters that modify another, such as an | |
5970 | accent of some type. These are considered to be in the script of the | |
5971 | master character, and so never cause a script run to not match. | |
5972 | ||
5973 | =end original | |
5974 | ||
5975 | "Inherited" は、一部のアクセントのように、他のものを変更する文字に | |
5976 | 適用されます。 | |
5977 | これらは元の文字の用字として扱われるので、 | |
5978 | マッチングしない用字連続になることはありません。 | |
5979 | ||
5980 | =begin original | |
5981 | ||
5982 | The other one is "Common". This consists of mostly punctuation, emoji, | |
5983 | and characters used in mathematics and music, and the ASCII digits C<0> | |
5984 | through C<9>. These characters can appear intermixed in text in many of | |
5985 | the world's scripts. These also don't cause a script run to not match, | |
5986 | except any ASCII digits encountered have to obey the decimal digit rules | |
5987 | described above. | |
5988 | ||
5989 | =end original | |
5990 | ||
5991 | もう一つは "Common" です。 | |
5992 | これはほとんど句読点、絵文字、数学と音楽で使われる文字、および | |
5993 | ASCII の数字 C<0> から C<9> で構成されます。 | |
5994 | これらの文字は世界中の多くの用字の文章で混ぜられて現れます。 | |
5995 | これらもまた、マッチングしない用字連続にはなりません; | |
5996 | ただし、現れた ASCII の数字は、前述した 10 進数の規則に従う必要があります。 | |
5997 | ||
5998 | =begin original | |
5999 | ||
6000 | This construct is non-capturing. You can add parentheses to I<pattern> | |
6001 | to capture, if desired. You will have to do this if you plan to use | |
6002 | L</(*ACCEPT) (*ACCEPT:arg)> and not have it bypass the script run | |
6003 | checking. | |
6004 | ||
6005 | =end original | |
6006 | ||
6007 | この構文は捕捉しません。 | |
6008 | 捕捉したい場合は、その I<pattern> にかっこを追加できます。 | |
6009 | L</(*ACCEPT) (*ACCEPT:arg)> を使う予定で、用字連続チェックを | |
6010 | 迂回しない場合は、こうすることが必要でしょう。 | |
6011 | ||
6012 | =begin original | |
6013 | ||
6014 | This feature is experimental, and the exact syntax and details of | |
6015 | operation are subject to change; using it yields a warning in the | |
6016 | C<experimental::script_run> category. | |
6017 | ||
6018 | =end original | |
6019 | ||
6020 | この機能は実験的で、正確な文法と操作の詳細は変更される予定です; | |
6021 | これを使うと、C<experimental::script_run> カテゴリの警告が出力されます。 | |
6022 | ||
6023 | =begin original | |
6024 | ||
6025 | The C<Script_Extensions> property is used as the basis for this feature. | |
6026 | ||
6027 | =end original | |
6028 | ||
6029 | この機能の基礎としては C<Script_Extensions> 特性が使われます。 | |
6030 | ||
6031 | =head2 Special Backtracking Control Verbs | |
6032 | ||
6033 | (特殊なバックトラック制御記号) | |
6034 | ||
6035 | =begin original | |
6036 | ||
6037 | These special patterns are generally of the form C<(*I<VERB>:I<ARG>)>. Unless | |
6038 | otherwise stated the I<ARG> argument is optional; in some cases, it is | |
6039 | mandatory. | |
6040 | ||
6041 | =end original | |
6042 | ||
6043 | これらの特殊なパターンは C<(*I<VERB>:I<ARG>)> という一般形式を持っています。 | |
6044 | 特に記されていない限り、I<ARG> はオプションです; 一部の場合では、 | |
6045 | これは必須です。 | |
6046 | ||
6047 | =begin original | |
6048 | ||
4066 | 6049 | Any pattern containing a special backtracking verb that allows an argument |
4067 | 6050 | has the special behaviour that when executed it sets the current package's |
4068 | 6051 | C<$REGERROR> and C<$REGMARK> variables. When doing so the following |
4069 | 6052 | rules apply: |
4070 | 6053 | |
4071 | 6054 | =end original |
4072 | 6055 | |
4073 | 6056 | 引数を許可する特殊バックトラック制御記号を含んでいる全てのパターンは、 |
4074 | 6057 | それが実行されると現在のパッケージの C<$REGERROR> 及び C<$REGMARK> 変数を |
4075 | 6058 | 設定する特殊な振る舞いを持っています。 |
4076 | 6059 | これが行われる時以下の手順が適用されます。 |
4077 | 6060 | |
4078 | 6061 | =begin original |
4079 | 6062 | |
4080 | On failure, the C<$REGERROR> variable will be set to the ARG value of the | |
6063 | On failure, the C<$REGERROR> variable will be set to the I<ARG> value of the | |
4081 | 6064 | verb pattern, if the verb was involved in the failure of the match. If the |
4082 | ARG part of the pattern was omitted, then C<$REGERROR> will be set to the | |
6065 | I<ARG> part of the pattern was omitted, then C<$REGERROR> will be set to the | |
4083 | 6066 | name of the last C<(*MARK:NAME)> pattern executed, or to TRUE if there was |
4084 | 6067 | none. Also, the C<$REGMARK> variable will be set to FALSE. |
4085 | 6068 | |
4086 | 6069 | =end original |
4087 | 6070 | |
4088 | 6071 | 失敗時には C<$REGERROR> 変数には、記号がマッチングの失敗の中で |
4089 | 使われていたのならその記号パターンの ARG の値がセットされます。 | |
6072 | 使われていたのならその記号パターンの I<ARG> の値がセットされます。 | |
4090 | もしパターンの ARG 部分が省略されていたときには、C<$REGERROR> には | |
6073 | もしパターンの I<ARG> 部分が省略されていたときには、C<$REGERROR> には | |
4091 | 6074 | 最後に実行された C<(*MARK:NAME)> パターンの名前、またはそれもなければ |
4092 | 6075 | 真に設定されます。 |
4093 | 6076 | また、C<$REGMARK> 変数は偽に設定されます。 |
4094 | 6077 | |
4095 | 6078 | =begin original |
4096 | 6079 | |
4097 | 6080 | On a successful match, the C<$REGERROR> variable will be set to FALSE, and |
4098 | 6081 | the C<$REGMARK> variable will be set to the name of the last |
4099 | 6082 | C<(*MARK:NAME)> pattern executed. See the explanation for the |
4100 | 6083 | C<(*MARK:NAME)> verb below for more details. |
4101 | 6084 | |
4102 | 6085 | =end original |
4103 | 6086 | |
4104 | 6087 | マッチングの成功時には、C<$REGERROR> 変数は偽に設定され、C<$REGMARK> 変数には |
4105 | 6088 | 最後に実行された C<(*MARK:NAME)> パターンの名前が設定されます。 |
4106 | 6089 | 詳細は C<(*MARK:NAME)> 記号の説明を参照してください。 |
4107 | 6090 | |
4108 | 6091 | =begin original |
4109 | 6092 | |
4110 | 6093 | B<NOTE:> C<$REGERROR> and C<$REGMARK> are not magic variables like C<$1> |
4111 | 6094 | and most other regex-related variables. They are not local to a scope, nor |
4112 | 6095 | readonly, but instead are volatile package variables similar to C<$AUTOLOAD>. |
4113 | ||
6096 | They are set in the package containing the code that I<executed> the regex | |
6097 | (rather than the one that compiled it, where those differ). If necessary, you | |
6098 | can use C<local> to localize changes to these variables to a specific scope | |
6099 | before executing a regex. | |
4114 | 6100 | |
4115 | 6101 | =end original |
4116 | 6102 | |
4117 | 6103 | B<補足:> C<$REGERROR> 及び C<$REGMARK> は C<$1> や他の多くの |
4118 | 6104 | 正規表現関連の変数のようにマジック変数ではありません。 |
4119 | 6105 | それらはスコープ内にローカルにならず、読み込み専用でもありませんが、 |
4120 | 6106 | C<$AUTOLOAD> と似た揮発するパッケージ変数です。 |
4121 | ||
6107 | これらには正規表現が I<実行される> コードを含むパッケージが設定されます | |
6108 | (コンパイルされるコードではありません; これは異なることがあります)。 | |
6109 | 必要な場合は、正規表現を実行する前に | |
6110 | これらの変数の変更を特定のスコープ内に留めるために C<local> を使えます。 | |
4122 | 6111 | |
4123 | 6112 | =begin original |
4124 | 6113 | |
4125 | 6114 | If a pattern does not contain a special backtracking verb that allows an |
4126 | 6115 | argument, then C<$REGERROR> and C<$REGMARK> are not touched at all. |
4127 | 6116 | |
4128 | 6117 | =end original |
4129 | 6118 | |
4130 | 6119 | もしパターンが引数を許可する特殊バックトラック記号を含んでなかった場合には、 |
4131 | 6120 | C<$REGERROR> 及び C<$REGMARK> は全く触られません。 |
4132 | 6121 | |
4133 | 6122 | =over 3 |
4134 | 6123 | |
4135 | =item Verbs | |
6124 | =item Verbs | |
4136 | 6125 | |
4137 | ( | |
6126 | (動詞) | |
4138 | 6127 | |
4139 | 6128 | =over 4 |
4140 | 6129 | |
4141 | 6130 | =item C<(*PRUNE)> C<(*PRUNE:NAME)> |
4142 | 6131 | X<(*PRUNE)> X<(*PRUNE:NAME)> |
4143 | 6132 | |
4144 | 6133 | =begin original |
4145 | 6134 | |
4146 | 6135 | This zero-width pattern prunes the backtracking tree at the current point |
4147 | when backtracked into on failure. Consider the pattern C<A (*PRUNE) B>, | |
6136 | when backtracked into on failure. Consider the pattern C</I<A> (*PRUNE) I<B>/>, | |
4148 | where A and B are complex patterns. Until the C<(*PRUNE)> verb is reached, | |
6137 | where I<A> and I<B> are complex patterns. Until the C<(*PRUNE)> verb is reached, | |
4149 | A may backtrack as necessary to match. Once it is reached, matching | |
6138 | I<A> may backtrack as necessary to match. Once it is reached, matching | |
4150 | continues in B, which may also backtrack as necessary; however, should B | |
6139 | continues in I<B>, which may also backtrack as necessary; however, should B | |
4151 | 6140 | not match, then no further backtracking will take place, and the pattern |
4152 | 6141 | will fail outright at the current starting position. |
4153 | 6142 | |
4154 | 6143 | =end original |
4155 | 6144 | |
4156 | 6145 | このゼロ幅のパターンは失敗でバックトラックしてきたときに現在の位置で |
4157 | 6146 | バックトラックツリーを刈り取ります。 |
4158 | C<A (*PRUNE) B> というパターンで A も B も複雑なパターンである時を | |
6147 | C</I<A> (*PRUNE) I<B>/> というパターンで I<A> も I<B> も複雑なパターンである時を | |
4159 | 6148 | 考えてみます。 |
4160 | C<(*PRUNE)> に達するまでは、A はマッチングに必要であれば | |
6149 | C<(*PRUNE)> に達するまでは、I<A> はマッチングに必要であれば | |
4161 | 6150 | バックトラックしていきます。 |
4162 | しかし一旦そこに達して B に続くと、そこでも必要に応じてバックトラックします; | |
6151 | しかし一旦そこに達して I<B> に続くと、そこでも必要に応じてバックトラックします; | |
4163 | 6152 | しかしながら、B がマッチングしなかったときにはそれ以上のバックトラックは |
4164 | 6153 | 行われず、現在の開始位置でのマッチングはすぐに失敗します。 |
4165 | 6154 | |
4166 | 6155 | =begin original |
4167 | 6156 | |
4168 | 6157 | The following example counts all the possible matching strings in a |
4169 | 6158 | pattern (without actually matching any of them). |
4170 | 6159 | |
4171 | 6160 | =end original |
4172 | 6161 | |
4173 | 6162 | 次の例ではパターンに対してマッチングできるすべての文字列を(実際には |
4174 | 6163 | マッチングさせずに)数えます。 |
4175 | 6164 | |
4176 | 6165 | 'aaab' =~ /a+b?(?{print "$&\n"; $count++})(*FAIL)/; |
4177 | 6166 | print "Count=$count\n"; |
4178 | 6167 | |
4179 | 6168 | =begin original |
4180 | 6169 | |
4181 | 6170 | which produces: |
4182 | 6171 | |
4183 | 6172 | =end original |
4184 | 6173 | |
4185 | 6174 | この出力: |
4186 | 6175 | |
4187 | 6176 | aaab |
4188 | 6177 | aaa |
4189 | 6178 | aa |
4190 | 6179 | a |
4191 | 6180 | aab |
4192 | 6181 | aa |
4193 | 6182 | a |
4194 | 6183 | ab |
4195 | 6184 | a |
4196 | 6185 | Count=9 |
4197 | 6186 | |
4198 | 6187 | =begin original |
4199 | 6188 | |
4200 | 6189 | If we add a C<(*PRUNE)> before the count like the following |
4201 | 6190 | |
4202 | 6191 | =end original |
4203 | 6192 | |
4204 | 6193 | 次のように数える前に C<(*PRUNE)> を加えると |
4205 | 6194 | |
4206 | 6195 | 'aaab' =~ /a+b?(*PRUNE)(?{print "$&\n"; $count++})(*FAIL)/; |
4207 | 6196 | print "Count=$count\n"; |
4208 | 6197 | |
4209 | 6198 | =begin original |
4210 | 6199 | |
4211 | 6200 | we prevent backtracking and find the count of the longest matching string |
4212 | 6201 | at each matching starting point like so: |
4213 | 6202 | |
4214 | 6203 | =end original |
4215 | 6204 | |
4216 | 6205 | バックトラックを妨げ次のように各開始位置での一番長いマッチング文字列を |
4217 | 6206 | 数えるようになります: |
4218 | 6207 | |
4219 | 6208 | aaab |
4220 | 6209 | aab |
4221 | 6210 | ab |
4222 | 6211 | Count=3 |
4223 | 6212 | |
4224 | 6213 | =begin original |
4225 | 6214 | |
4226 | 6215 | Any number of C<(*PRUNE)> assertions may be used in a pattern. |
4227 | 6216 | |
4228 | 6217 | =end original |
4229 | 6218 | |
4230 | 6219 | 1つのパターン内で C<(*PRUNE)> 言明はいくつでも使えます。 |
4231 | 6220 | |
4232 | 6221 | =begin original |
4233 | 6222 | |
4234 | See also C<< (?>pattern) >> and possessive quantifiers for | |
6223 | See also C<<< L<< /(?>pattern) >> >>> and possessive quantifiers for | |
6224 | other ways to | |
4235 | 6225 | control backtracking. In some cases, the use of C<(*PRUNE)> can be |
4236 | 6226 | replaced with a C<< (?>pattern) >> with no functional difference; however, |
4237 | 6227 | C<(*PRUNE)> can be used to handle cases that cannot be expressed using a |
4238 | 6228 | C<< (?>pattern) >> alone. |
4239 | 6229 | |
4240 | 6230 | =end original |
4241 | 6231 | |
4242 | バックトラックを制御する他の方法として | |
6232 | バックトラックを制御する他の方法として C<<< L<< /(?>pattern) >> >>> | |
4243 | 6233 | 及び絶対最大量指定子も参照してください。 |
4244 | 6234 | 幾つかのケースにおいては C<(*PRUNE)> の利用は機能的な違いなしに |
4245 | 6235 | C<< (?>pattern) >> で置き換えることができます; しかしながら C<(*PRUNE)> は |
4246 | 6236 | C<< (?>pattern) >> 単独では表現できないケースを扱うために使えます。 |
4247 | 6237 | |
4248 | 6238 | =item C<(*SKIP)> C<(*SKIP:NAME)> |
4249 | 6239 | X<(*SKIP)> |
4250 | 6240 | |
4251 | 6241 | =begin original |
4252 | 6242 | |
4253 | 6243 | This zero-width pattern is similar to C<(*PRUNE)>, except that on |
4254 | 6244 | failure it also signifies that whatever text that was matched leading up |
4255 | 6245 | to the C<(*SKIP)> pattern being executed cannot be part of I<any> match |
4256 | 6246 | of this pattern. This effectively means that the regex engine "skips" forward |
4257 | 6247 | to this position on failure and tries to match again, (assuming that |
4258 | 6248 | there is sufficient room to match). |
4259 | 6249 | |
4260 | 6250 | =end original |
4261 | 6251 | |
4262 | 6252 | このゼロ幅のパターンは C<*PRUNE> と似ていますが、実行されている |
4263 | 6253 | C<(*SKIP)> パターンまでにマッチングしたテキストはこのパターンの |
4264 | 6254 | I<どの>マッチングの一部にもならないことを示します。 |
4265 | 6255 | これは正規表現エンジンがこの位置まで失敗として「スキップ」して(マッチングに |
4266 | 6256 | 十分な空間があれば)再びマッチングを試みることを効率的に意味します。 |
4267 | 6257 | |
4268 | 6258 | =begin original |
4269 | 6259 | |
4270 | 6260 | The name of the C<(*SKIP:NAME)> pattern has special significance. If a |
4271 | 6261 | C<(*MARK:NAME)> was encountered while matching, then it is that position |
4272 | 6262 | which is used as the "skip point". If no C<(*MARK)> of that name was |
4273 | 6263 | encountered, then the C<(*SKIP)> operator has no effect. When used |
4274 | 6264 | without a name the "skip point" is where the match point was when |
4275 | executing the (*SKIP) pattern. | |
6265 | executing the C<(*SKIP)> pattern. | |
4276 | 6266 | |
4277 | 6267 | =end original |
4278 | 6268 | |
4279 | 6269 | C<(*SKIP:NAME)> パターンの名前部分には特別な意味があります。 |
4280 | 6270 | もしマッチングにおいて C<(*MARK:NAME)> に遭遇すると、それは「スキップ |
4281 | 6271 | 位置」として使われる位置になります。 |
4282 | 6272 | その名前の C<(*MARK)> と東宮していなければ、C<(*SKIP)> 操作は効果を |
4283 | 6273 | 持ちません。 |
4284 | 名前がなければ「スキップ位置」は(*SKIP)パターンの実行されたときに | |
6274 | 名前がなければ「スキップ位置」は C<(*SKIP)> パターンの実行されたときに | |
4285 | 6275 | マッチングポイントが使われます。 |
4286 | 6276 | |
4287 | 6277 | =begin original |
4288 | 6278 | |
4289 | 6279 | Compare the following to the examples in C<(*PRUNE)>; note the string |
4290 | 6280 | is twice as long: |
4291 | 6281 | |
4292 | 6282 | =end original |
4293 | 6283 | |
4294 | 6284 | 以下の例を C<(*PRUNE)> と比べてみてください; |
4295 | 6285 | 文字列が2倍になってることに注意してください: |
4296 | 6286 | |
4297 | 6287 | 'aaabaaab' =~ /a+b?(*SKIP)(?{print "$&\n"; $count++})(*FAIL)/; |
4298 | 6288 | print "Count=$count\n"; |
4299 | 6289 | |
4300 | 6290 | =begin original |
4301 | 6291 | |
4302 | 6292 | outputs |
4303 | 6293 | |
4304 | 6294 | =end original |
4305 | 6295 | |
4306 | 6296 | これの出力は |
4307 | 6297 | |
4308 | 6298 | aaab |
4309 | 6299 | aaab |
4310 | 6300 | Count=2 |
4311 | 6301 | |
4312 | 6302 | =begin original |
4313 | 6303 | |
4314 | 6304 | Once the 'aaab' at the start of the string has matched, and the C<(*SKIP)> |
4315 | 6305 | executed, the next starting point will be where the cursor was when the |
4316 | 6306 | C<(*SKIP)> was executed. |
4317 | 6307 | |
4318 | 6308 | =end original |
4319 | 6309 | |
4320 | 6310 | いったん文字列の最初の 'aaab' がマッチングして、C<(*SKIP)> が実行されると、 |
4321 | 6311 | 次の開始位置は C<(*SKIP)> が実行されたときのカーソルがいた位置になります。 |
4322 | 6312 | |
4323 | 6313 | =item C<(*MARK:NAME)> C<(*:NAME)> |
4324 | 6314 | X<(*MARK)> X<(*MARK:NAME)> X<(*:NAME)> |
4325 | 6315 | |
4326 | 6316 | =begin original |
4327 | 6317 | |
4328 | 6318 | This zero-width pattern can be used to mark the point reached in a string |
4329 | 6319 | when a certain part of the pattern has been successfully matched. This |
4330 | 6320 | mark may be given a name. A later C<(*SKIP)> pattern will then skip |
4331 | 6321 | forward to that point if backtracked into on failure. Any number of |
4332 | C<(*MARK)> patterns are allowed, and the NAME portion may be duplicated. | |
6322 | C<(*MARK)> patterns are allowed, and the I<NAME> portion may be duplicated. | |
4333 | 6323 | |
4334 | 6324 | =end original |
4335 | 6325 | |
4336 | 6326 | このゼロ幅のマッチングはパターン内の特定の箇所がマッチングに成功したときに、 |
4337 | 6327 | 文字列の中で達した位置を記録するために使われます。 |
4338 | 6328 | このマークには名前をつけることもできます。 |
4339 | 6329 | 後者の C<(*SKIP)> パターンは失敗時でバックトラックしたときにその箇所まで |
4340 | 6330 | スキップします。 |
4341 | C<(*MARK)> パターンはいくつでも使うことができて、NAME 部分は | |
6331 | C<(*MARK)> パターンはいくつでも使うことができて、I<NAME> 部分は | |
4342 | 6332 | 重複することもあります。 |
4343 | 6333 | |
4344 | 6334 | =begin original |
4345 | 6335 | |
4346 | 6336 | In addition to interacting with the C<(*SKIP)> pattern, C<(*MARK:NAME)> |
4347 | 6337 | can be used to "label" a pattern branch, so that after matching, the |
4348 | 6338 | program can determine which branches of the pattern were involved in the |
4349 | 6339 | match. |
4350 | 6340 | |
4351 | 6341 | =end original |
4352 | 6342 | |
4353 | 6343 | C<(*SKIP)> パターンとの相互動作に加えて、C<(*MARK:NAME)> はパターン分岐の |
4354 | 6344 | 「ラベル」としても使うことができます; このためマッチングの後で、プログラムは |
4355 | 6345 | そのマッチングにおいてパターンのどの分岐が使われたのかを知ることができます。 |
4356 | 6346 | |
4357 | 6347 | =begin original |
4358 | 6348 | |
4359 | 6349 | When a match is successful, the C<$REGMARK> variable will be set to the |
4360 | 6350 | name of the most recently executed C<(*MARK:NAME)> that was involved |
4361 | 6351 | in the match. |
4362 | 6352 | |
4363 | 6353 | =end original |
4364 | 6354 | |
4365 | 6355 | マッチングの成功時に、C<$REGMARK> 変数はマッチングの中で一番最近に |
4366 | 6356 | 実行された C<(*MARK:NAME)> の名前を設定します。 |
4367 | 6357 | |
4368 | 6358 | =begin original |
4369 | 6359 | |
4370 | 6360 | This can be used to determine which branch of a pattern was matched |
4371 | 6361 | without using a separate capture group for each branch, which in turn |
4372 | 6362 | can result in a performance improvement, as perl cannot optimize |
4373 | 6363 | C</(?:(x)|(y)|(z))/> as efficiently as something like |
4374 | 6364 | C</(?:x(*MARK:x)|y(*MARK:y)|z(*MARK:z))/>. |
4375 | 6365 | |
4376 | 6366 | =end original |
4377 | 6367 | |
4378 | 6368 | これは書く分岐で別々の捕捉グループを使うことなしにパターンのどの分岐が |
4379 | 6369 | マッチングしたのかを知るために使うことができます; これは perl は |
4380 | 6370 | C</(?:(x)|(y)|(z))/> を C</(?:x(*MARK:x)|y(*MARK:y)|z(*MARK:z))/> 程度に |
4381 | 6371 | 効率的には最適化できないためパフォーマンスの向上をもたらします。 |
4382 | 6372 | |
4383 | 6373 | =begin original |
4384 | 6374 | |
4385 | 6375 | When a match has failed, and unless another verb has been involved in |
4386 | 6376 | failing the match and has provided its own name to use, the C<$REGERROR> |
4387 | 6377 | variable will be set to the name of the most recently executed |
4388 | 6378 | C<(*MARK:NAME)>. |
4389 | 6379 | |
4390 | 6380 | =end original |
4391 | 6381 | |
4392 | 6382 | マッチングが失敗して、そして他の記号がマッチングの失敗で行われずかつ名前を |
4393 | 6383 | 持っているというのでなければ、C<$REGERROR> 変数には一番最近に実行された |
4394 | 6384 | 名前が設定されます。 |
4395 | 6385 | |
4396 | 6386 | =begin original |
4397 | 6387 | |
4398 | 6388 | See L</(*SKIP)> for more details. |
4399 | 6389 | |
4400 | 6390 | =end original |
4401 | 6391 | |
4402 | 6392 | 詳細は L</(*SKIP)> を参照してください。 |
4403 | 6393 | |
4404 | 6394 | =begin original |
4405 | 6395 | |
4406 | 6396 | As a shortcut C<(*MARK:NAME)> can be written C<(*:NAME)>. |
4407 | 6397 | |
4408 | 6398 | =end original |
4409 | 6399 | |
4410 | 6400 | C<(*MARK:NAME)> の短縮形として C<(*:NAME)> とも記述できます。 |
4411 | 6401 | |
4412 | 6402 | =item C<(*THEN)> C<(*THEN:NAME)> |
4413 | 6403 | |
4414 | 6404 | =begin original |
4415 | 6405 | |
4416 | 6406 | This is similar to the "cut group" operator C<::> from Perl 6. Like |
4417 | 6407 | C<(*PRUNE)>, this verb always matches, and when backtracked into on |
4418 | 6408 | failure, it causes the regex engine to try the next alternation in the |
4419 | 6409 | innermost enclosing group (capturing or otherwise) that has alternations. |
4420 | 6410 | The two branches of a C<(?(condition)yes-pattern|no-pattern)> do not |
4421 | 6411 | count as an alternation, as far as C<(*THEN)> is concerned. |
4422 | 6412 | |
4423 | 6413 | =end original |
4424 | 6414 | |
4425 | 6415 | これは Perl 6 の "cut group" 演算子 C<::> と似ています。 |
4426 | 6416 | C<(*PRUNE)> のように、この記号は常にマッチングし、そして失敗で |
4427 | 6417 | バックトラックした時に正規表現エンジンに、代替のある一番内側で閉じている |
4428 | 6418 | グループ(キャプチャでもそうでなくとも)で次の代替を試みるようにさせます。 |
4429 | 6419 | C<(*THEN)> が有効である限り、 |
4430 | 6420 | C<(?(condition)yes-pattern|no-pattern)> の二つの枝は代替とは扱われません。 |
4431 | 6421 | |
4432 | 6422 | =begin original |
4433 | 6423 | |
4434 | 6424 | Its name comes from the observation that this operation combined with the |
4435 | alternation operator (C<|>) can be used to create what is essentially a | |
6425 | alternation operator (C<"|">) can be used to create what is essentially a | |
4436 | 6426 | pattern-based if/then/else block: |
4437 | 6427 | |
4438 | 6428 | =end original |
4439 | 6429 | |
4440 | この名前は代替演算子(C<|>) と連結されたこの演算子で本質的にパターンベースの | |
6430 | この名前は代替演算子 (C<"|">) と連結されたこの演算子で本質的にパターンベースの | |
4441 | 6431 | if/then/else ブロックとなるものを作るために使うことが |
4442 | 6432 | できることからきています: |
4443 | 6433 | |
4444 | 6434 | ( COND (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) |
4445 | 6435 | |
4446 | 6436 | =begin original |
4447 | 6437 | |
4448 | 6438 | Note that if this operator is used and NOT inside of an alternation then |
4449 | 6439 | it acts exactly like the C<(*PRUNE)> operator. |
4450 | 6440 | |
4451 | 6441 | =end original |
4452 | 6442 | |
4453 | 6443 | この演算子が使われていてそしてそれが代替の内側ではなければ |
4454 | 6444 | これはちょうど C<(*PRUNE)> 演算子のように動作します。 |
4455 | 6445 | |
4456 | 6446 | / A (*PRUNE) B / |
4457 | 6447 | |
4458 | 6448 | =begin original |
4459 | 6449 | |
4460 | 6450 | is the same as |
4461 | 6451 | |
4462 | 6452 | =end original |
4463 | 6453 | |
4464 | 6454 | は次と同じです |
4465 | 6455 | |
4466 | 6456 | / A (*THEN) B / |
4467 | 6457 | |
4468 | 6458 | =begin original |
4469 | 6459 | |
4470 | 6460 | but |
4471 | 6461 | |
4472 | 6462 | =end original |
4473 | 6463 | |
4474 | 6464 | しかし |
4475 | 6465 | |
4476 | / ( A (*THEN) B | C | |
6466 | / ( A (*THEN) B | C ) / | |
4477 | 6467 | |
4478 | 6468 | =begin original |
4479 | 6469 | |
4480 | 6470 | is not the same as |
4481 | 6471 | |
4482 | 6472 | =end original |
4483 | 6473 | |
4484 | 6474 | は次と同じではありません |
4485 | 6475 | |
4486 | / ( A (*PRUNE) B | C | |
6476 | / ( A (*PRUNE) B | C ) / | |
4487 | 6477 | |
4488 | 6478 | =begin original |
4489 | 6479 | |
4490 | as after matching the A but failing on the B the C<(*THEN)> verb will | |
6480 | as after matching the I<A> but failing on the I<B> the C<(*THEN)> verb will | |
4491 | backtrack and try C; but the C<(*PRUNE)> verb will simply fail. | |
6481 | backtrack and try I<C>; but the C<(*PRUNE)> verb will simply fail. | |
4492 | 6482 | |
4493 | 6483 | =end original |
4494 | 6484 | |
4495 | A にマッチングしたけれど B に失敗した後 C<(*THEN)> 記号は | |
6485 | I<A> にマッチングしたけれど I<B> に失敗した後 C<(*THEN)> 記号は | |
4496 | C を試みます; しかし C<(*PRUNE)> 記号であれば | |
6486 | バックトラックして I<C> を試みます; しかし C<(*PRUNE)> 記号であれば | |
6487 | 単純に失敗します。 | |
4497 | 6488 | |
4498 | = | |
6489 | =item C<(*COMMIT)> C<(*COMMIT:args)> | |
4499 | ||
4500 | =item Verbs without an argument | |
4501 | ||
4502 | =over 4 | |
4503 | ||
4504 | =item C<(*COMMIT)> | |
4505 | 6490 | X<(*COMMIT)> |
4506 | 6491 | |
4507 | 6492 | =begin original |
4508 | 6493 | |
4509 | 6494 | This is the Perl 6 "commit pattern" C<< <commit> >> or C<:::>. It's a |
4510 | 6495 | zero-width pattern similar to C<(*SKIP)>, except that when backtracked |
4511 | 6496 | into on failure it causes the match to fail outright. No further attempts |
4512 | 6497 | to find a valid match by advancing the start pointer will occur again. |
4513 | 6498 | For example, |
4514 | 6499 | |
4515 | 6500 | =end original |
4516 | 6501 | |
4517 | 6502 | これは Perl 6 の"コミットパターン" C<< <commit> >> または C<:::> です。 |
4518 | 6503 | これは C<(*SKIP)> と似たゼロ幅のパターンですが、失敗でバックトラックした |
4519 | 6504 | 際にマッチングがすぐに失敗する点で異なります。 |
4520 | 6505 | それ以降で開始位置を進めて有効なマッチングを探す試行は行われません。 |
4521 | 6506 | 例えば、 |
4522 | 6507 | |
4523 | 6508 | 'aaabaaab' =~ /a+b?(*COMMIT)(?{print "$&\n"; $count++})(*FAIL)/; |
4524 | 6509 | print "Count=$count\n"; |
4525 | 6510 | |
4526 | 6511 | =begin original |
4527 | 6512 | |
4528 | 6513 | outputs |
4529 | 6514 | |
4530 | 6515 | =end original |
4531 | 6516 | |
4532 | 6517 | これの出力は |
4533 | 6518 | |
4534 | 6519 | aaab |
4535 | 6520 | Count=1 |
4536 | 6521 | |
4537 | 6522 | =begin original |
4538 | 6523 | |
4539 | 6524 | In other words, once the C<(*COMMIT)> has been entered, and if the pattern |
4540 | 6525 | does not match, the regex engine will not try any further matching on the |
4541 | 6526 | rest of the string. |
4542 | 6527 | |
4543 | 6528 | =end original |
4544 | 6529 | |
4545 | 6530 | 言い換えると、いったん C<(*COMMIT)> に入った後に、そのパターンが |
4546 | 6531 | マッチングしなかったのなら、正規表現エンジンは文字列の残りに対して |
4547 | 6532 | それ以上のマッチングを試みません。 |
4548 | 6533 | |
4549 | =item C<(*FAIL)> C<(*F)> | |
6534 | =item C<(*FAIL)> C<(*F)> C<(*FAIL:arg)> | |
4550 | 6535 | X<(*FAIL)> X<(*F)> |
4551 | 6536 | |
4552 | 6537 | =begin original |
4553 | 6538 | |
4554 | 6539 | This pattern matches nothing and always fails. It can be used to force the |
4555 | 6540 | engine to backtrack. It is equivalent to C<(?!)>, but easier to read. In |
4556 | fact, C<(?!)> gets optimised into C<(*FAIL)> internally. | |
6541 | fact, C<(?!)> gets optimised into C<(*FAIL)> internally. You can provide | |
6542 | an argument so that if the match fails because of this C<FAIL> directive | |
6543 | the argument can be obtained from C<$REGERROR>. | |
4557 | 6544 | |
4558 | 6545 | =end original |
4559 | 6546 | |
4560 | 6547 | このパターンは何にもマッチングせず常に失敗します。 |
4561 | 6548 | これはエンジンを強制的にバックトラックさせるために使えます。 |
4562 | 6549 | これは C<(?!)> と等価ですが、より読みやすくなっています。 |
4563 | 6550 | 実際、C<(?!)> は内部的には C<(*FAIL)> に最適化されます。 |
6551 | この C<FAIL> 指示子によってマッチングが失敗したときに | |
6552 | C<$REGERROR> から得られる、引数を指定できます。 | |
4564 | 6553 | |
4565 | 6554 | =begin original |
4566 | 6555 | |
4567 | 6556 | It is probably useful only when combined with C<(?{})> or C<(??{})>. |
4568 | 6557 | |
4569 | 6558 | =end original |
4570 | 6559 | |
4571 | 6560 | これはおそらく C<(?{})> または C<(??{})> と組み合わせた時にだけ |
4572 | 6561 | 役に立つでしょう。 |
4573 | 6562 | |
4574 | =item C<(*ACCEPT)> | |
6563 | =item C<(*ACCEPT)> C<(*ACCEPT:arg)> | |
4575 | 6564 | X<(*ACCEPT)> |
4576 | 6565 | |
4577 | 6566 | =begin original |
4578 | 6567 | |
4579 | B<WARNING:> This feature is highly experimental. It is not recommended | |
4580 | for production code. | |
4581 | ||
4582 | =end original | |
4583 | ||
4584 | B<警告:> この機能は強く実験的です。 | |
4585 | 製品コードでは推奨されません。 | |
4586 | ||
4587 | =begin original | |
4588 | ||
4589 | 6568 | This pattern matches nothing and causes the end of successful matching at |
4590 | 6569 | the point at which the C<(*ACCEPT)> pattern was encountered, regardless of |
4591 | 6570 | whether there is actually more to match in the string. When inside of a |
4592 | 6571 | nested pattern, such as recursion, or in a subpattern dynamically generated |
4593 | 6572 | via C<(??{})>, only the innermost pattern is ended immediately. |
4594 | 6573 | |
4595 | 6574 | =end original |
4596 | 6575 | |
4597 | 6576 | このパターンマッチングは何もせず C<(*ACCEPT)> パターンと遭遇した場所で |
4598 | 6577 | 文字列の中で実際にもっとマッチングするものがあるかどうかにかかわらず |
4599 | 6578 | 成功のマッチングを終了させます。 |
4600 | 6579 | 再帰、または C<(??{})> といったネストしたパターンの内側では、一番内側の |
4601 | 6580 | パターンのみがすぐに終了します。 |
4602 | 6581 | |
4603 | 6582 | =begin original |
4604 | 6583 | |
4605 | 6584 | If the C<(*ACCEPT)> is inside of capturing groups then the groups are |
4606 | 6585 | marked as ended at the point at which the C<(*ACCEPT)> was encountered. |
4607 | 6586 | For instance: |
4608 | 6587 | |
4609 | 6588 | =end original |
4610 | 6589 | |
4611 | 6590 | C<(*ACCEPT)> が捕捉グループの内側で使われた場合捕捉グループは |
4612 | 6591 | C<(*ACCEPT)> と遭遇した位置で終了とマークされます。 |
4613 | 6592 | 例えば: |
4614 | 6593 | |
4615 | 6594 | 'AB' =~ /(A (A|B(*ACCEPT)|C) D)(E)/x; |
4616 | 6595 | |
4617 | 6596 | =begin original |
4618 | 6597 | |
4619 | will match, and C<$1> will be C<AB> and C<$2> will be C<B>, C<$3> will not | |
6598 | will match, and C<$1> will be C<AB> and C<$2> will be C<"B">, C<$3> will not | |
4620 | 6599 | be set. If another branch in the inner parentheses was matched, such as in the |
4621 | string 'ACDE', then the C<D> and C<E> would have to be matched as well. | |
6600 | string 'ACDE', then the C<"D"> and C<"E"> would have to be matched as well. | |
4622 | 6601 | |
4623 | 6602 | =end original |
4624 | 6603 | |
4625 | はマッチングし、C<$1> は C<AB> になり、C<$2> は C<B> に、そして | |
6604 | はマッチングし、C<$1> は C<AB> になり、C<$2> は C<"B"> に、そして | |
4626 | 6605 | C<$3> は設定されません。 |
4627 | 'ACDE' のようにかっこの内側で他の分岐がマッチングしたのなら、 | |
6606 | 'ACDE' のようにかっこの内側で他の分岐がマッチングしたのなら、 | |
4628 | C<E> もマッチングします。 | |
6607 | C<"D"> と C<"E"> もマッチングします。 | |
4629 | 6608 | |
4630 | =back | |
4631 | ||
4632 | =back | |
4633 | ||
4634 | =head2 Backtracking | |
4635 | X<backtrack> X<backtracking> | |
4636 | ||
4637 | (バックトラック) | |
4638 | ||
4639 | 6609 | =begin original |
4640 | 6610 | |
4641 | ||
6611 | You can provide an argument, which will be available in the var | |
4642 | ||
6612 | C<$REGMARK> after the match completes. | |
4643 | the rules involved in selecting a match among possible alternatives, | |
4644 | see L<Combining RE Pieces>. | |
4645 | 6613 | |
4646 | 6614 | =end original |
4647 | 6615 | |
4648 | ||
6616 | マッチングが成功した後に C<$REGMARK> 変数で利用可能な引数を指定できます。 | |
4649 | 説明します。 | |
4650 | 可能な代替におけるマッチングの選択におけるルールの厳密な(そして複雑な) | |
4651 | 説明は L<Combining RE Pieces> を参照してください。 | |
4652 | 6617 | |
4653 | =b | |
6618 | =back | |
4654 | 6619 | |
4655 | ||
6620 | =back | |
4656 | notion called I<backtracking>, which is currently used (when needed) | |
4657 | by all regular non-possessive expression quantifiers, namely C<*>, C<*?>, C<+>, | |
4658 | C<+?>, C<{n,m}>, and C<{n,m}?>. Backtracking is often optimized | |
4659 | internally, but the general principle outlined here is valid. | |
4660 | 6621 | |
4661 | =e | |
6622 | =head2 Warning on C<\1> Instead of C<$1> | |
4662 | 6623 | |
4663 | ||
6624 | (C<$1> ではなく C<\1> だったときの警告) | |
4664 | 正規表現量指定子、つまり、C<*>, C<*?>, C<+>, C<+?>, C<{n,m}>, C<{n,m}?> で | |
4665 | 使われる I<バックトラッキング> と呼ばれる概念が含まれています。 | |
4666 | バックトラックはしばしば内部で最適化されますが、ここで概説する一般的な | |
4667 | 原則は妥当です。 | |
4668 | 6625 | |
4669 | 6626 | =begin original |
4670 | 6627 | |
4671 | For a regular expression to match, the I<entire> regular expression must | |
4672 | match, not just part of it. So if the beginning of a pattern containing a | |
4673 | quantifier succeeds in a way that causes later parts in the pattern to | |
4674 | fail, the matching engine backs up and recalculates the beginning | |
4675 | part--that's why it's called backtracking. | |
4676 | ||
4677 | =end original | |
4678 | ||
4679 | 正規表現がマッチングする時、その正規表現の一部ではなく、 | |
4680 | I<全体> がマッチングしなければなりません。 | |
4681 | そのためもしパターンの前半にパターンの後半部分を失敗させてしまう | |
4682 | 量指定子が含まれているのなら、マッチングングエンジンはいったん戻って | |
4683 | 開始位置を再計算します -- これがバックトラッキングと呼ばれる所以です。 | |
4684 | ||
4685 | =begin original | |
4686 | ||
4687 | Here is an example of backtracking: Let's say you want to find the | |
4688 | word following "foo" in the string "Food is on the foo table.": | |
4689 | ||
4690 | =end original | |
4691 | ||
4692 | バックトラッキングの例をあげてみます: "Food is on the foo table." という | |
4693 | 文字列の中で "foo" に続く単語を取り出してください: | |
4694 | ||
4695 | $_ = "Food is on the foo table."; | |
4696 | if ( /\b(foo)\s+(\w+)/i ) { | |
4697 | print "$2 follows $1.\n"; | |
4698 | } | |
4699 | ||
4700 | =begin original | |
4701 | ||
4702 | When the match runs, the first part of the regular expression (C<\b(foo)>) | |
4703 | finds 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 | |
4705 | no whitespace following the "Foo" that it had saved in $1, it realizes its | |
4706 | mistake and starts over again one character after where it had the | |
4707 | tentative match. This time it goes all the way until the next occurrence | |
4708 | of "foo". The complete regular expression matches this time, and you get | |
4709 | the expected output of "table follows foo." | |
4710 | ||
4711 | =end original | |
4712 | ||
4713 | マッチングが実行される時、正規表現の最初の部分 (C<\b(foo)>) は開始文字列の | |
4714 | 右側で可能なマッチングを探します; そして $1 に "Foo" をロードします。 | |
4715 | しかし、すぐにマッチングエンジンは $1 に保存した "Foo" の後に空白が | |
4716 | 無いことを見つけ、それが失敗だったことを検出して仮にマッチングさせた | |
4717 | 場所の1文字後から開始します。 | |
4718 | この時次の "foo" の出現まで進みます。 | |
4719 | この時に正規表現は完全にマッチングし、予測した出力 "table follows foo." を | |
4720 | 得ます。 | |
4721 | ||
4722 | =begin original | |
4723 | ||
4724 | Sometimes minimal matching can help a lot. Imagine you'd like to match | |
4725 | everything between "foo" and "bar". Initially, you write something | |
4726 | like this: | |
4727 | ||
4728 | =end original | |
4729 | ||
4730 | 最小マッチングが役立つこともあります。 | |
4731 | "foo" と "bar" の間の全てにマッチングしたいと考えてください。 | |
4732 | 最初に、次のように書くかもしれません: | |
4733 | ||
4734 | $_ = "The food is under the bar in the barn."; | |
4735 | if ( /foo(.*)bar/ ) { | |
4736 | print "got <$1>\n"; | |
4737 | } | |
4738 | ||
4739 | =begin original | |
4740 | ||
4741 | Which perhaps unexpectedly yields: | |
4742 | ||
4743 | =end original | |
4744 | ||
4745 | しかしこれは考えたのと違う結果となるでしょう: | |
4746 | ||
4747 | got <d is under the bar in the > | |
4748 | ||
4749 | =begin original | |
4750 | ||
4751 | That's because C<.*> was greedy, so you get everything between the | |
4752 | I<first> "foo" and the I<last> "bar". Here it's more effective | |
4753 | to use minimal matching to make sure you get the text between a "foo" | |
4754 | and the first "bar" thereafter. | |
4755 | ||
4756 | =end original | |
4757 | ||
4758 | これは C<.*> が貪欲であり、そのために I<最初の> "foo" と I<最後の> | |
4759 | "bar" の間にある全てを取り出してしまいます。 | |
4760 | 次に "foo" とその後の最初の "bar" の間にあるテキストを取り出す | |
4761 | 最小マッチングを使ったもっと効率的な方法を示します: | |
4762 | ||
4763 | if ( /foo(.*?)bar/ ) { print "got <$1>\n" } | |
4764 | got <d is under the > | |
4765 | ||
4766 | =begin original | |
4767 | ||
4768 | Here's another example. Let's say you'd like to match a number at the end | |
4769 | of a string, and you also want to keep the preceding part of the match. | |
4770 | So you write this: | |
4771 | ||
4772 | =end original | |
4773 | ||
4774 | 別の例も出してみます。 | |
4775 | 文字列の最後にある数字にマッチングさせて、そのマッチングの前の部分も | |
4776 | 保持させてみましょう。 | |
4777 | そしてあなたは次のように書くかもしれません。 | |
4778 | ||
4779 | $_ = "I have 2 numbers: 53147"; | |
4780 | if ( /(.*)(\d*)/ ) { # Wrong! | |
4781 | print "Beginning is <$1>, number is <$2>.\n"; | |
4782 | } | |
4783 | ||
4784 | =begin original | |
4785 | ||
4786 | That won't work at all, because C<.*> was greedy and gobbled up the | |
4787 | whole string. As C<\d*> can match on an empty string the complete | |
4788 | regular expression matched successfully. | |
4789 | ||
4790 | =end original | |
4791 | ||
4792 | これは全く動作しません、なぜなら C<.*> は貪欲であり文字列全体を | |
4793 | 飲み込んでしまいます。 | |
4794 | C<\d*> は空の文字列にマッチングできるので正規表現は完全に正常に | |
4795 | マッチングします。 | |
4796 | ||
4797 | Beginning is <I have 2 numbers: 53147>, number is <>. | |
4798 | ||
4799 | =begin original | |
4800 | ||
4801 | Here are some variants, most of which don't work: | |
4802 | ||
4803 | =end original | |
4804 | ||
4805 | 動作しない主なバリエーションをあげておきます: | |
4806 | ||
4807 | $_ = "I have 2 numbers: 53147"; | |
4808 | @pats = qw{ | |
4809 | (.*)(\d*) | |
4810 | (.*)(\d+) | |
4811 | (.*?)(\d*) | |
4812 | (.*?)(\d+) | |
4813 | (.*)(\d+)$ | |
4814 | (.*?)(\d+)$ | |
4815 | (.*)\b(\d+)$ | |
4816 | (.*\D)(\d+)$ | |
4817 | }; | |
4818 | ||
4819 | for $pat (@pats) { | |
4820 | printf "%-12s ", $pat; | |
4821 | if ( /$pat/ ) { | |
4822 | print "<$1> <$2>\n"; | |
4823 | } else { | |
4824 | print "FAIL\n"; | |
4825 | } | |
4826 | } | |
4827 | ||
4828 | =begin original | |
4829 | ||
4830 | That will print out: | |
4831 | ||
4832 | =end original | |
4833 | ||
4834 | これらの結果は次のようになります: | |
4835 | ||
4836 | (.*)(\d*) <I have 2 numbers: 53147> <> | |
4837 | (.*)(\d+) <I have 2 numbers: 5314> <7> | |
4838 | (.*?)(\d*) <> <> | |
4839 | (.*?)(\d+) <I have > <2> | |
4840 | (.*)(\d+)$ <I have 2 numbers: 5314> <7> | |
4841 | (.*?)(\d+)$ <I have 2 numbers: > <53147> | |
4842 | (.*)\b(\d+)$ <I have 2 numbers: > <53147> | |
4843 | (.*\D)(\d+)$ <I have 2 numbers: > <53147> | |
4844 | ||
4845 | =begin original | |
4846 | ||
4847 | As you see, this can be a bit tricky. It's important to realize that a | |
4848 | regular expression is merely a set of assertions that gives a definition | |
4849 | of success. There may be 0, 1, or several different ways that the | |
4850 | definition might succeed against a particular string. And if there are | |
4851 | multiple ways it might succeed, you need to understand backtracking to | |
4852 | know which variety of success you will achieve. | |
4853 | ||
4854 | =end original | |
4855 | ||
4856 | このように、これは幾分トリッキーです。 | |
4857 | 重要なのは正規表現は成功の定義を定める主張の集合にすぎないことを | |
4858 | 認識することです。 | |
4859 | 特定の文字列で成功となる定義には 0, 1 または複数の違ったやり方が存在します。 | |
4860 | そしてもし成功する複数の方法が存在するのなら成功したうちのどれが目的と | |
4861 | するものなのかを知るためにバックトラッキングを理解しておく必要があります。 | |
4862 | ||
4863 | =begin original | |
4864 | ||
4865 | When using look-ahead assertions and negations, this can all get even | |
4866 | trickier. Imagine you'd like to find a sequence of non-digits not | |
4867 | followed by "123". You might try to write that as | |
4868 | ||
4869 | =end original | |
4870 | ||
4871 | 前読みの言明及び否定を使っている時にはこれはますますトリッキーになります。 | |
4872 | "123" が後ろに続かない数字以外の列を探したいと考えてみてください。 | |
4873 | あなたは次のように書くかもしれません。 | |
4874 | ||
4875 | $_ = "ABC123"; | |
4876 | if ( /^\D*(?!123)/ ) { # Wrong! | |
4877 | print "Yup, no 123 in $_\n"; | |
4878 | } | |
4879 | ||
4880 | =begin original | |
4881 | ||
4882 | But that isn't going to match; at least, not the way you're hoping. It | |
4883 | claims that there is no 123 in the string. Here's a clearer picture of | |
4884 | why that pattern matches, contrary to popular expectations: | |
4885 | ||
4886 | =end original | |
4887 | ||
4888 | ですがこれはマッチングしません; 少なくともなってほしかったようには。 | |
4889 | これは文字列の中に 123 がないことを要求します。 | |
4890 | よくある予想と比較してなぜパターンがマッチングするのかのわかりやすい | |
4891 | 説明を次に示します: | |
4892 | ||
4893 | $x = 'ABC123'; | |
4894 | $y = 'ABC445'; | |
4895 | ||
4896 | print "1: got $1\n" if $x =~ /^(ABC)(?!123)/; | |
4897 | print "2: got $1\n" if $y =~ /^(ABC)(?!123)/; | |
4898 | ||
4899 | print "3: got $1\n" if $x =~ /^(\D*)(?!123)/; | |
4900 | print "4: got $1\n" if $y =~ /^(\D*)(?!123)/; | |
4901 | ||
4902 | =begin original | |
4903 | ||
4904 | This prints | |
4905 | ||
4906 | =end original | |
4907 | ||
4908 | これは次の出力となります | |
4909 | ||
4910 | 2: got ABC | |
4911 | 3: got AB | |
4912 | 4: got ABC | |
4913 | ||
4914 | =begin original | |
4915 | ||
4916 | You might have expected test 3 to fail because it seems to a more | |
4917 | general purpose version of test 1. The important difference between | |
4918 | them is that test 3 contains a quantifier (C<\D*>) and so can use | |
4919 | backtracking, whereas test 1 will not. What's happening is | |
4920 | that you've asked "Is it true that at the start of $x, following 0 or more | |
4921 | non-digits, you have something that's not 123?" If the pattern matcher had | |
4922 | let C<\D*> expand to "ABC", this would have caused the whole pattern to | |
4923 | fail. | |
4924 | ||
4925 | =end original | |
4926 | ||
4927 | テスト 3 はテスト 1 のより一般的なバージョンなのでそれが失敗すると | |
4928 | 考えたかもしれません。 | |
4929 | この 2 つの重要な違いは、テスト 3 には量指定子(C<\D*>)が含まれているので | |
4930 | テスト1ではできなかったバックトラッキングを行うことが | |
4931 | できるところにあります。 | |
4932 | ここであなたは「$x のはじめで 0 個以上の非数字があるから 123 ではない | |
4933 | 何かを得られるんじゃないの?」と聞くでしょう。 | |
4934 | このパターンマッチングが C<\D*> を "ABC" に展開させると | |
4935 | これはパターン全体を失敗させることになります。 | |
4936 | ||
4937 | =begin original | |
4938 | ||
4939 | The search engine will initially match C<\D*> with "ABC". Then it will | |
4940 | try to match C<(?!123)> with "123", which fails. But because | |
4941 | a quantifier (C<\D*>) has been used in the regular expression, the | |
4942 | search engine can backtrack and retry the match differently | |
4943 | in the hope of matching the complete regular expression. | |
4944 | ||
4945 | =end original | |
4946 | ||
4947 | 探索エンジンは最初に C<\D*> を "ABC" にマッチングさせます。 | |
4948 | そして C<(?!123)> を "123" にマッチングさせ、これは失敗します。 | |
4949 | けれども量指定子 (C<\D*>) が正規表現の中で使われているので、探索エンジンは | |
4950 | バックトラックしてこの正規表現全体をマッチングさせるように異なるマッチングを | |
4951 | 行うことができます。 | |
4952 | ||
4953 | =begin original | |
4954 | ||
4955 | The pattern really, I<really> wants to succeed, so it uses the | |
4956 | standard pattern back-off-and-retry and lets C<\D*> expand to just "AB" this | |
4957 | time. Now there's indeed something following "AB" that is not | |
4958 | "123". It's "C123", which suffices. | |
4959 | ||
4960 | =end original | |
4961 | ||
4962 | このパターンは本当に、I<本当に> 成功したいので、これは標準的なパターンの | |
4963 | 後退再試行を行い、この時に C<\D*> を "AB" のみに展開させます。 | |
4964 | そして確かに "AB" の後ろは "123" ではありません。 | |
4965 | "C123" は十分満たしています。 | |
4966 | ||
4967 | =begin original | |
4968 | ||
4969 | We can deal with this by using both an assertion and a negation. | |
4970 | We'll say that the first part in $1 must be followed both by a digit | |
4971 | and by something that's not "123". Remember that the look-aheads | |
4972 | are zero-width expressions--they only look, but don't consume any | |
4973 | of the string in their match. So rewriting this way produces what | |
4974 | you'd expect; that is, case 5 will fail, but case 6 succeeds: | |
4975 | ||
4976 | =end original | |
4977 | ||
4978 | これは言明と否定の両方を使うことで処理することができます。 | |
4979 | $1 の最初の部分は数字が続きかつそれは "123" ではないことを宣言します。 | |
4980 | 先読みはゼロ幅の式なのでそれがマッチングした文字列を全く消費しないことを | |
4981 | 思い出してください。 | |
4982 | そしてこれを必要なものを生成するように書き換えます; | |
4983 | つまり、5 のケースでは失敗し、6 のケースは成功します: | |
4984 | ||
4985 | print "5: got $1\n" if $x =~ /^(\D*)(?=\d)(?!123)/; | |
4986 | print "6: got $1\n" if $y =~ /^(\D*)(?=\d)(?!123)/; | |
4987 | ||
4988 | 6: got ABC | |
4989 | ||
4990 | =begin original | |
4991 | ||
4992 | In other words, the two zero-width assertions next to each other work as though | |
4993 | they're ANDed together, just as you'd use any built-in assertions: C</^$/> | |
4994 | matches only if you're at the beginning of the line AND the end of the | |
4995 | line simultaneously. The deeper underlying truth is that juxtaposition in | |
4996 | regular expressions always means AND, except when you write an explicit OR | |
4997 | using the vertical bar. C</ab/> means match "a" AND (then) match "b", | |
4998 | although the attempted matches are made at different positions because "a" | |
4999 | is not a zero-width assertion, but a one-width assertion. | |
5000 | ||
5001 | =end original | |
5002 | ||
5003 | 言い換えると、このそれぞれの次にある2つのゼロ幅の言明はちょうど何か組み込みの | |
5004 | 言明を使ったかのようにそれらがともに AND されているかのように動作します: | |
5005 | C</^$/> は行の始まりで且つ同時に行の終了でる時にのみマッチングします。 | |
5006 | もっと深部での真実は、併記された正規表現は垂直線を使って明示的に OR を | |
5007 | 書いたとき以外は常に AND を意味します。 | |
5008 | C</ab/> は、"a" がゼロ幅の言明ではなく 1 文字幅の言明なので異なる場所で | |
5009 | マッチングが行われはしますが、 "a" にマッチング且つ(そして) "b" に | |
5010 | マッチングということを意味します。 | |
5011 | ||
5012 | =begin original | |
5013 | ||
5014 | B<WARNING>: Particularly complicated regular expressions can take | |
5015 | exponential time to solve because of the immense number of possible | |
5016 | ways they can use backtracking to try for a match. For example, without | |
5017 | internal optimizations done by the regular expression engine, this will | |
5018 | take a painfully long time to run: | |
5019 | ||
5020 | =end original | |
5021 | ||
5022 | B<警告>: 特にコンパイルされた正規表現はマッチングのために | |
5023 | できる限りのバックトラックを非常に多くの回数行うので | |
5024 | 解くために指数的な時間を必要とすることがあります。 | |
5025 | 例えば、正規表現エンジンの内部で行われる最適化がなかったときには、次の評価は | |
5026 | 尋常じゃないくらい長時間かかります: | |
5027 | ||
5028 | 'aaaaaaaaaaaa' =~ /((a{0,5}){0,5})*[c]/ | |
5029 | ||
5030 | =begin original | |
5031 | ||
5032 | And if you used C<*>'s in the internal groups instead of limiting them | |
5033 | to 0 through 5 matches, then it would take forever--or until you ran | |
5034 | out of stack space. Moreover, these internal optimizations are not | |
5035 | always applicable. For example, if you put C<{0,5}> instead of C<*> | |
5036 | on the external group, no current optimization is applicable, and the | |
5037 | match takes a long time to finish. | |
5038 | ||
5039 | =end original | |
5040 | ||
5041 | そしてもし内側のグループで 0 から 5 回にマッチングを制限する代わりに | |
5042 | C<*> を使うと、永久に、またはスタックを使い果たすまで | |
5043 | 実行し続けることになります。 | |
5044 | その上、これらの最適化は常にできるわけではありません。 | |
5045 | 例えば、外側のグループで C<*> の代わりに C<{0,5}> を使ったときに、現在の | |
5046 | 最適化は適用されません; そしてマッチングが終わるまでの長い時間が | |
5047 | 必要になります。 | |
5048 | ||
5049 | =begin original | |
5050 | ||
5051 | A powerful tool for optimizing such beasts is what is known as an | |
5052 | "independent group", | |
5053 | which does not backtrack (see L</C<< (?>pattern) >>>). Note also that | |
5054 | zero-length look-ahead/look-behind assertions will not backtrack to make | |
5055 | the tail match, since they are in "logical" context: only | |
5056 | whether they match is considered relevant. For an example | |
5057 | where side-effects of look-ahead I<might> have influenced the | |
5058 | following match, see L</C<< (?>pattern) >>>. | |
5059 | ||
5060 | =end original | |
5061 | ||
5062 | そのような野獣のような最適化のためのパワフルなツールとして | |
5063 | 知られているものに、「独立グループ」があります; これはバックトラックを | |
5064 | 行いません (L</C<< (?>pattern) >>> を参照)。 | |
5065 | ゼロ幅の先読み/後読みの言明も「論理的な」文脈なので末尾のマッチングを | |
5066 | バックトラックしません: マッチングが関連して考慮されるかどうかだけです。 | |
5067 | 先読みの言明の副作用がそれに続くマッチングに影響する I<かもしれない> 例は、 | |
5068 | L</C<< (?>pattern) >>> を参照してください。 | |
5069 | ||
5070 | =head2 Version 8 Regular Expressions | |
5071 | X<regular expression, version 8> X<regex, version 8> X<regexp, version 8> | |
5072 | ||
5073 | (バージョン 8 正規表現) | |
5074 | ||
5075 | =begin original | |
5076 | ||
5077 | In case you're not familiar with the "regular" Version 8 regex | |
5078 | routines, here are the pattern-matching rules not described above. | |
5079 | ||
5080 | =end original | |
5081 | ||
5082 | 「通常の」バージョン 8 正規表現ルーチンに詳しくないのであれば、ここには | |
5083 | これまでに説明されていないパターンマッチングルールがあります。 | |
5084 | ||
5085 | =begin original | |
5086 | ||
5087 | Any single character matches itself, unless it is a I<metacharacter> | |
5088 | with a special meaning described here or above. You can cause | |
5089 | characters that normally function as metacharacters to be interpreted | |
5090 | literally by prefixing them with a "\" (e.g., "\." matches a ".", not any | |
5091 | character; "\\" matches a "\"). This escape mechanism is also required | |
5092 | for the character used as the pattern delimiter. | |
5093 | ||
5094 | =end original | |
5095 | ||
5096 | すべての単一の文字は、それが個々でまたはこれまでに説明した特別な意味を | |
5097 | 持っている I<メタ文字> である場合以外は、文字それ自身にマッチングします。 | |
5098 | 文字は "\" で前置されることで通常はメタ文字としての機能を持っている文字を | |
5099 | リテラルとして処理させれるようになります(つまり、"\." は任意の 1 文字ではなく | |
5100 | "." にマッチングするようになり、"\\" は "\" にマッチングするようになります。 | |
5101 | このエスケープ機構はパターン区切りとして使われている文字でも必要です。 | |
5102 | ||
5103 | =begin original | |
5104 | ||
5105 | A series of characters matches that series of characters in the target | |
5106 | string, so the pattern C<blurfl> would match "blurfl" in the target | |
5107 | string. | |
5108 | ||
5109 | =end original | |
5110 | ||
5111 | 文字の並びは、ターゲット文字列の文字の並びにマッチングします; 従って | |
5112 | パターン C<blurfl> は、ターゲット文字列の "blurfl" にマッチングします。 | |
5113 | ||
5114 | =begin original | |
5115 | ||
5116 | You can specify a character class, by enclosing a list of characters | |
5117 | in C<[]>, which will match any character from the list. If the | |
5118 | first character after the "[" is "^", the class matches any character not | |
5119 | in the list. Within a list, the "-" character specifies a | |
5120 | range, so that C<a-z> represents all characters between "a" and "z", | |
5121 | inclusive. If you want either "-" or "]" itself to be a member of a | |
5122 | class, put it at the start of the list (possibly after a "^"), or | |
5123 | escape it with a backslash. "-" is also taken literally when it is | |
5124 | at the end of the list, just before the closing "]". (The | |
5125 | following all specify the same class of three characters: C<[-az]>, | |
5126 | C<[az-]>, and C<[a\-z]>. All are different from C<[a-z]>, which | |
5127 | specifies a class containing twenty-six characters, even on EBCDIC-based | |
5128 | character sets.) Also, if you try to use the character | |
5129 | classes C<\w>, C<\W>, C<\s>, C<\S>, C<\d>, or C<\D> as endpoints of | |
5130 | a range, the "-" is understood literally. | |
5131 | ||
5132 | =end original | |
5133 | ||
5134 | C<[]> で文字のリストを囲むことで文字クラスを指定することができます; | |
5135 | これはリストの中の任意の文字にマッチングします。 | |
5136 | もし "[" の後の最初の文字が "^" だったときには、その文字クラスは | |
5137 | リストの中にない任意の文字にマッチングします。 | |
5138 | リストの中では、文字 "-" は範囲を意味します; なので C<a-z> は "a" と "z" を | |
5139 | 含めてそれらの間にあるすべての文字を表現します。 | |
5140 | 文字クラスの要素として "-" または "]" を使いたい時には、リストの先頭に | |
5141 | (あるいは"^"の後に)置くか、バックスラッシュを使ってエスケープします。 | |
5142 | "-" はリストの終端、リストを閉じる "]" の直前にあったときもリテラルとして | |
5143 | 扱われます。 | |
5144 | (次の例はすべて同じ3文字からなる文字クラスです: C<[-az]>, C<[az-]>, | |
5145 | C<[a\-z]>。 | |
5146 | これらはすべて EBCDIC ベースの文字集合であっても26文字からなる文字集合 | |
5147 | C<[a-z]> とは異なります。) また、範囲の端点として文字クラス C<\w>, C<\W>, | |
5148 | C<\s>, C<\S>, C<\d>, C<\D> を使ったときも "-" はリテラルとして | |
5149 | 処理されます。 | |
5150 | ||
5151 | =begin original | |
5152 | ||
5153 | Note also that the whole range idea is rather unportable between | |
5154 | character sets--and even within character sets they may cause results | |
5155 | you probably didn't expect. A sound principle is to use only ranges | |
5156 | that begin from and end at either alphabetics of equal case ([a-e], | |
5157 | [A-E]), or digits ([0-9]). Anything else is unsafe. If in doubt, | |
5158 | spell out the character sets in full. | |
5159 | ||
5160 | =end original | |
5161 | ||
5162 | 範囲全体というアイデアは文字集合間でポータブルではありません -- そして | |
5163 | 結果となる文字集合では予期したものではないでしょう。 | |
5164 | ひとつの安全策としては同じケースの英字の([a-e], [A-E]), | |
5165 | または数字([0-9])という範囲でのみ使うことです。 | |
5166 | これ以外は安全ではありません。 | |
5167 | もし信じられないのであれば文字集合を完全につづってみてください。 | |
5168 | ||
5169 | =begin original | |
5170 | ||
5171 | Characters may be specified using a metacharacter syntax much like that | |
5172 | used in C: "\n" matches a newline, "\t" a tab, "\r" a carriage return, | |
5173 | "\f" a form feed, etc. More generally, \I<nnn>, where I<nnn> is a string | |
5174 | of three octal digits, matches the character whose coded character set value | |
5175 | is I<nnn>. Similarly, \xI<nn>, where I<nn> are hexadecimal digits, | |
5176 | matches the character whose ordinal is I<nn>. The expression \cI<x> | |
5177 | matches the character control-I<x>. Finally, the "." metacharacter | |
5178 | matches any character except "\n" (unless you use C</s>). | |
5179 | ||
5180 | =end original | |
5181 | ||
5182 | 文字は C でよく使われているようなメタ文字の構文を使って指定することも | |
5183 | できます: "\n" は改行にマッチングし、"\t" はタブに、"\r" は復帰に、 | |
5184 | "\f" はフォームフィードにといった具合にマッチングします。 | |
5185 | より一般的に、\I<nnn> (I<nnn> は 3 桁の 8 進数字) はその文字集合でコード値 | |
5186 | I<nnn> の文字にマッチングします。 | |
5187 | 同じように、\xI<nn> (I<nn> は16進数字) は数値で I<nn> になる文字に | |
5188 | マッチングします。 | |
5189 | 式 \cI<x> は制御文字 I<x> にマッチングします。 | |
5190 | そして最後に、"." メタ文字は (C</s> を使っていない限り) "\n" 以外の任意の | |
5191 | 文字にマッチングします。 | |
5192 | ||
5193 | =begin original | |
5194 | ||
5195 | You can specify a series of alternatives for a pattern using "|" to | |
5196 | separate them, so that C<fee|fie|foe> will match any of "fee", "fie", | |
5197 | or "foe" in the target string (as would C<f(e|i|o)e>). The | |
5198 | first alternative includes everything from the last pattern delimiter | |
5199 | ("(", "(?:", etc. or the beginning of the pattern) up to the first "|", and | |
5200 | the last alternative contains everything from the last "|" to the next | |
5201 | closing pattern delimiter. That's why it's common practice to include | |
5202 | alternatives in parentheses: to minimize confusion about where they | |
5203 | start and end. | |
5204 | ||
5205 | =end original | |
5206 | ||
5207 | "|" を使ってパターンを区切って一連の代替を指定することもできます; なので | |
5208 | C<fee|fie|foe> は対象の文字列の "fee"、"fie"、または "foe" のいずれかに | |
5209 | (C<f(e|i|o)e> のように)マッチングします。 | |
5210 | 最初の代替には最後のパターン区切り ("(", "(?:" など、またはパターンの始まり)から | |
5211 | 最初の "|" までのすべてが含まれ、最後の代替には最後の "|" から | |
5212 | 次の閉じパターン区切りまでが含まれます。 | |
5213 | 通常代替をかっこの中に入れるのは、その開始位置と終了位置が少しはわかりやすく | |
5214 | なるようにです。 | |
5215 | ||
5216 | =begin original | |
5217 | ||
5218 | Alternatives are tried from left to right, so the first | |
5219 | alternative found for which the entire expression matches, is the one that | |
5220 | is chosen. This means that alternatives are not necessarily greedy. For | |
5221 | example: when matching C<foo|foot> against "barefoot", only the "foo" | |
5222 | part will match, as that is the first alternative tried, and it successfully | |
5223 | matches the target string. (This might not seem important, but it is | |
5224 | important when you are capturing matched text using parentheses.) | |
5225 | ||
5226 | =end original | |
5227 | ||
5228 | 代替は左から右へと試されます、なので最初の代替がその完全な式で | |
5229 | マッチングしたのならそれが選択されます。 | |
5230 | これは代替は貪欲である必要はないということを意味します。 | |
5231 | 例えば: "barefoot" に対して C<foo|foot> をマッチングさせると、最初の代替から | |
5232 | 試されるので、"foo" の部分がマッチングし、これは対象の文字列に対して成功で | |
5233 | マッチングします。 | |
5234 | (これは重要ではないでしょうが、かっこを使ってマッチングしたテキストを | |
5235 | 捕捉しているときには重要でしょう。) | |
5236 | ||
5237 | =begin original | |
5238 | ||
5239 | Also remember that "|" is interpreted as a literal within square brackets, | |
5240 | so if you write C<[fee|fie|foe]> you're really only matching C<[feio|]>. | |
5241 | ||
5242 | =end original | |
5243 | ||
5244 | また "|" は角かっこの中ではリテラルとして処理されるので、 | |
5245 | C<[fee|fie|foe]> と書くとこれは実際には C<[feio|]> にのみマッチングします。 | |
5246 | ||
5247 | =begin original | |
5248 | ||
5249 | Within a pattern, you may designate subpatterns for later reference | |
5250 | by enclosing them in parentheses, and you may refer back to the | |
5251 | I<n>th subpattern later in the pattern using the metacharacter | |
5252 | \I<n> or \gI<n>. Subpatterns are numbered based on the left to right order | |
5253 | of their opening parenthesis. A backreference matches whatever | |
5254 | actually matched the subpattern in the string being examined, not | |
5255 | the rules for that subpattern. Therefore, C<(0|0x)\d*\s\g1\d*> will | |
5256 | match "0x1234 0x4321", but not "0x1234 01234", because subpattern | |
5257 | 1 matched "0x", even though the rule C<0|0x> could potentially match | |
5258 | the leading 0 in the second number. | |
5259 | ||
5260 | =end original | |
5261 | ||
5262 | パターンにおいて、後で参照するためにかっこで括って部分パターンを指定できます; | |
5263 | してメタ文字 \I<n> または \gI<n> を使ってパターンの後の方で I<n> 番目の | |
5264 | 部分パターンを参照することができます。 | |
5265 | 部分パターンはその開きかっこの左から右への順に番号づけられます。 | |
5266 | 後方参照は評価された文字列の中でその部分パターンに実際にマッチングしたものに | |
5267 | マッチングします。 | |
5268 | 従って、C<(0|0x)\d*\s\g1\d*> は "0x1234 0x4321" にはマッチングしますが、 | |
5269 | "0x1234 01234" にはマッチングしません; なぜなら、C<0|0x> は二つめ数字の | |
5270 | 先頭にある 0 にマッチングすることができるのですが、 | |
5271 | 部分パターン 1 は "0x" にマッチングするためです。 | |
5272 | ||
5273 | =head2 Warning on \1 Instead of $1 | |
5274 | ||
5275 | ($1 ではなく \1 だったときの警告) | |
5276 | ||
5277 | =begin original | |
5278 | ||
5279 | 6628 | Some people get too used to writing things like: |
5280 | 6629 | |
5281 | 6630 | =end original |
5282 | 6631 | |
5283 | 6632 | 次のように書くことになれている人も中にはいるでしょう: |
5284 | 6633 | |
5285 | 6634 | $pattern =~ s/(\W)/\\\1/g; |
5286 | 6635 | |
5287 | 6636 | =begin original |
5288 | 6637 | |
5289 | 6638 | This is grandfathered (for \1 to \9) for the RHS of a substitute to avoid |
5290 | 6639 | shocking the |
5291 | 6640 | B<sed> addicts, but it's a dirty habit to get into. That's because in |
5292 | 6641 | PerlThink, the righthand side of an C<s///> is a double-quoted string. C<\1> in |
5293 | 6642 | the usual double-quoted string means a control-A. The customary Unix |
5294 | 6643 | meaning of C<\1> is kludged in for C<s///>. However, if you get into the habit |
5295 | 6644 | of doing that, you get yourself into trouble if you then add an C</e> |
5296 | 6645 | modifier. |
5297 | 6646 | |
5298 | 6647 | =end original |
5299 | 6648 | |
5300 | 6649 | (\1 から \9 については) B<sed> 中毒な人をびっくりさせないための RHS 置換の |
5301 | 6650 | 祖先ですが、しかしこれは汚らしい癖です。 |
5302 | 6651 | Perl においては、C<s///> の右側はダブルクォートされた文字列と |
5303 | 6652 | 考えられるためです。 |
5304 | 6653 | 通常のダブルクォートされた文字列の中では C<\1> は control-A を意味します。 |
5305 | 6654 | C<\1> の Unix での習慣的な意味は C<s///> だけのその場しのぎです。 |
5306 | 6655 | しかしながら、この癖に漬かっていると C</e> 修飾子を使ったときに |
5307 | 6656 | トラブルとなるでしょう。 |
5308 | 6657 | |
5309 | 6658 | s/(\d+)/ \1 + 1 /eg; # causes warning under -w |
5310 | 6659 | |
5311 | 6660 | =begin original |
5312 | 6661 | |
5313 | 6662 | Or if you try to do |
5314 | 6663 | |
5315 | 6664 | =end original |
5316 | 6665 | |
5317 | 6666 | または次のようにするかもしれません |
5318 | 6667 | |
5319 | 6668 | s/(\d+)/\1000/; |
5320 | 6669 | |
5321 | 6670 | =begin original |
5322 | 6671 | |
5323 | 6672 | You can't disambiguate that by saying C<\{1}000>, whereas you can fix it with |
5324 | 6673 | C<${1}000>. The operation of interpolation should not be confused |
5325 | 6674 | with the operation of matching a backreference. Certainly they mean two |
5326 | 6675 | different things on the I<left> side of the C<s///>. |
5327 | 6676 | |
5328 | 6677 | =end original |
5329 | 6678 | |
5330 | 6679 | これを解消するために C<\{1}000> としないでください; |
5331 | 6680 | ここでは C<${1}000> とするべきです。 |
5332 | 6681 | 埋め込みの処理は後方参照にマッチングさせる操作より混乱は少ないでしょう。 |
5333 | 6682 | 特に C<s///> の I<左> 側では2つの異なった意味になります。 |
5334 | 6683 | |
5335 | 6684 | =head2 Repeated Patterns Matching a Zero-length Substring |
5336 | 6685 | |
5337 | 6686 | (ゼロ幅の部分文字列にマッチングするパターンの繰り返し) |
5338 | 6687 | |
5339 | 6688 | =begin original |
5340 | 6689 | |
5341 | 6690 | B<WARNING>: Difficult material (and prose) ahead. This section needs a rewrite. |
5342 | 6691 | |
5343 | 6692 | =end original |
5344 | 6693 | |
5345 | 6694 | B<警告>: この先には難しい(そして無味乾燥な)内容があります。 |
5346 | 6695 | このセクションは書き直す必要があるでしょう。 |
5347 | 6696 | |
5348 | 6697 | =begin original |
5349 | 6698 | |
5350 | 6699 | Regular expressions provide a terse and powerful programming language. As |
5351 | 6700 | with most other power tools, power comes together with the ability |
5352 | 6701 | to wreak havoc. |
5353 | 6702 | |
5354 | 6703 | =end original |
5355 | 6704 | |
5356 | 6705 | 正規表現は簡潔でパワフルなプログラミング言語を提供します。 |
5357 | 6706 | 他の多くの強力なツールとともに、力は破壊の源にもなります。 |
5358 | 6707 | |
5359 | 6708 | =begin original |
5360 | 6709 | |
5361 | 6710 | A common abuse of this power stems from the ability to make infinite |
5362 | 6711 | loops using regular expressions, with something as innocuous as: |
5363 | 6712 | |
5364 | 6713 | =end original |
5365 | 6714 | |
5366 | 6715 | この力のよくある乱用は無害な何かとともに、正規表現使った |
5367 | 6716 | 無限ループとなります。 |
5368 | 6717 | |
5369 | 6718 | 'foo' =~ m{ ( o? )* }x; |
5370 | 6719 | |
5371 | 6720 | =begin original |
5372 | 6721 | |
5373 | The C<o?> matches at the beginning of C< | |
6722 | The C<o?> matches at the beginning of "C<foo>", and since the position | |
5374 | 6723 | in the string is not moved by the match, C<o?> would match again and again |
5375 | because of the C<*> quantifier. Another common way to create a similar cycle | |
6724 | because of the C<"*"> quantifier. Another common way to create a similar cycle | |
5376 | is with the looping modifier C</ | |
6725 | is with the looping modifier C</g>: | |
5377 | 6726 | |
5378 | 6727 | =end original |
5379 | 6728 | |
5380 | C<o?> は C< | |
6729 | C<o?> は "C<foo>" の始まりにマッチングし、文字列中での位置はこの | |
5381 | マッチングでは動かないので、C<o?> は C<*> 量指定子によって何回も | |
6730 | マッチングでは動かないので、C<o?> は C<"*"> 量指定子によって何回も | |
5382 | 6731 | マッチングします。 |
5383 | 同じような繰り返しを作るもう一つのよくある形として C</ | |
6732 | 同じような繰り返しを作るもう一つのよくある形として C</g> 修飾子を使った | |
5384 | 6733 | ループがあります: |
5385 | 6734 | |
5386 | 6735 | @matches = ( 'foo' =~ m{ o? }xg ); |
5387 | 6736 | |
5388 | 6737 | =begin original |
5389 | 6738 | |
5390 | 6739 | or |
5391 | 6740 | |
5392 | 6741 | =end original |
5393 | 6742 | |
5394 | 6743 | または |
5395 | 6744 | |
5396 | 6745 | print "match: <$&>\n" while 'foo' =~ m{ o? }xg; |
5397 | 6746 | |
5398 | 6747 | =begin original |
5399 | 6748 | |
5400 | or the loop implied by split(). | |
6749 | or the loop implied by C<split()>. | |
5401 | 6750 | |
5402 | 6751 | =end original |
5403 | 6752 | |
5404 | または split() による暗黙のループ。 | |
6753 | または C<split()> による暗黙のループ。 | |
5405 | 6754 | |
5406 | 6755 | =begin original |
5407 | 6756 | |
5408 | 6757 | However, long experience has shown that many programming tasks may |
5409 | 6758 | be significantly simplified by using repeated subexpressions that |
5410 | 6759 | may match zero-length substrings. Here's a simple example being: |
5411 | 6760 | |
5412 | 6761 | =end original |
5413 | 6762 | |
5414 | 6763 | しかしながら、長きにわたる経験からいくつかのプログラミングタスクは |
5415 | 6764 | ゼロ幅の部分文字列に対するマッチングを行う部分式の繰り返しで大幅に |
5416 | 6765 | 単純にできることがわかりました。 |
5417 | 6766 | 簡単な例を挙げてみます: |
5418 | 6767 | |
5419 | 6768 | @chars = split //, $string; # // is not magic in split |
5420 | 6769 | ($whitewashed = $string) =~ s/()/ /g; # parens avoid magic s// / |
5421 | 6770 | |
5422 | 6771 | =begin original |
5423 | 6772 | |
5424 | 6773 | Thus Perl allows such constructs, by I<forcefully breaking |
5425 | 6774 | the infinite loop>. The rules for this are different for lower-level |
5426 | 6775 | loops given by the greedy quantifiers C<*+{}>, and for higher-level |
5427 | ones like the C</g> modifier or split() operator. | |
6776 | ones like the C</g> modifier or C<split()> operator. | |
5428 | 6777 | |
5429 | 6778 | =end original |
5430 | 6779 | |
5431 | 6780 | このように Perl は I<強制的に無限ループを砕く> ことによってこういった構築を |
5432 | 6781 | 可能にしています。 |
5433 | 6782 | このためのルールは貪欲な量指定子 C<*+{}> によって与えられる |
5434 | 低レベルなループとも、C</g> 修飾子や split() 演算子による | |
6783 | 低レベルなループとも、C</g> 修飾子や C<split()> 演算子による | |
5435 | 6784 | 高レベルなループとも異なります。 |
5436 | 6785 | |
5437 | 6786 | =begin original |
5438 | 6787 | |
5439 | 6788 | The lower-level loops are I<interrupted> (that is, the loop is |
5440 | 6789 | broken) when Perl detects that a repeated expression matched a |
5441 | 6790 | zero-length substring. Thus |
5442 | 6791 | |
5443 | 6792 | =end original |
5444 | 6793 | |
5445 | 6794 | 低レベルなループは Perl がゼロ幅の部分文字列に対してマッチングする式が |
5446 | 6795 | 繰り返されたことを検出すると I<中断> されます (つまり、ループは壊されます)。 |
5447 | 6796 | 従って |
5448 | 6797 | |
5449 | 6798 | m{ (?: NON_ZERO_LENGTH | ZERO_LENGTH )* }x; |
5450 | 6799 | |
5451 | 6800 | =begin original |
5452 | 6801 | |
5453 | 6802 | is made equivalent to |
5454 | 6803 | |
5455 | 6804 | =end original |
5456 | 6805 | |
5457 | 6806 | は次と等価にされます |
5458 | 6807 | |
5459 | 6808 | m{ (?: NON_ZERO_LENGTH )* (?: ZERO_LENGTH )? }x; |
5460 | 6809 | |
5461 | 6810 | =begin original |
5462 | 6811 | |
5463 | 6812 | For example, this program |
5464 | 6813 | |
5465 | 6814 | =end original |
5466 | 6815 | |
5467 | 6816 | 例えば、以下のプログラムは |
5468 | 6817 | |
5469 | 6818 | #!perl -l |
5470 | 6819 | "aaaaab" =~ / |
5471 | 6820 | (?: |
5472 | 6821 | a # non-zero |
5473 | 6822 | | # or |
5474 | 6823 | (?{print "hello"}) # print hello whenever this |
5475 | 6824 | # branch is tried |
5476 | 6825 | (?=(b)) # zero-width assertion |
5477 | 6826 | )* # any number of times |
5478 | 6827 | /x; |
5479 | 6828 | print $&; |
5480 | 6829 | print $1; |
5481 | 6830 | |
5482 | 6831 | =begin original |
5483 | 6832 | |
5484 | 6833 | prints |
5485 | 6834 | |
5486 | 6835 | =end original |
5487 | 6836 | |
5488 | 6837 | 以下を表示します |
5489 | 6838 | |
5490 | 6839 | hello |
5491 | 6840 | aaaaa |
5492 | 6841 | b |
5493 | 6842 | |
5494 | 6843 | =begin original |
5495 | 6844 | |
5496 | 6845 | Notice that "hello" is only printed once, as when Perl sees that the sixth |
5497 | 6846 | iteration of the outermost C<(?:)*> matches a zero-length string, it stops |
5498 | the C<*>. | |
6847 | the C<"*">. | |
5499 | 6848 | |
5500 | 6849 | =end original |
5501 | 6850 | |
5502 | 6851 | "hello" は一度だけ表示されることに注目して下さい; Perl は |
5503 | 6852 | 一番外側の C<(?:)*> の 6 回目の繰り返しがゼロ長文字列にマッチングするのを |
5504 | 見るので、C<*> を止めます。 | |
6853 | 見るので、C<"*"> を止めます。 | |
5505 | 6854 | |
5506 | 6855 | =begin original |
5507 | 6856 | |
5508 | 6857 | The higher-level loops preserve an additional state between iterations: |
5509 | 6858 | whether the last match was zero-length. To break the loop, the following |
5510 | 6859 | match after a zero-length match is prohibited to have a length of zero. |
5511 | This prohibition interacts with backtracking (see L<"Backtracking">), | |
6860 | This prohibition interacts with backtracking (see L</"Backtracking">), | |
5512 | 6861 | and so the I<second best> match is chosen if the I<best> match is of |
5513 | 6862 | zero length. |
5514 | 6863 | |
5515 | 6864 | =end original |
5516 | 6865 | |
5517 | 6866 | 高レベルのループは各繰り返しの間に最後のマッチングがゼロ幅だったかどうかを |
5518 | 6867 | 追加で保持しています。 |
5519 | 6868 | ループを終えるために、ゼロ幅のマッチングの後のマッチングはゼロ幅と |
5520 | 6869 | なることを拒否します。 |
5521 | この禁則処理はバックトラックと相互に動作し(L<"Backtracking"> 参照)、そして | |
6870 | この禁則処理はバックトラックと相互に動作し(L</"Backtracking"> 参照)、そして | |
5522 | 6871 | I<ベストな> マッチングがゼロ幅だったのなら I<2 番目にベストな> |
5523 | 6872 | マッチングが選択されます。 |
5524 | 6873 | |
5525 | 6874 | =begin original |
5526 | 6875 | |
5527 | 6876 | For example: |
5528 | 6877 | |
5529 | 6878 | =end original |
5530 | 6879 | |
5531 | 6880 | 例: |
5532 | 6881 | |
5533 | 6882 | $_ = 'bar'; |
5534 | 6883 | s/\w??/<$&>/g; |
5535 | 6884 | |
5536 | 6885 | =begin original |
5537 | 6886 | |
5538 | 6887 | results in C<< <><b><><a><><r><> >>. At each position of the string the best |
5539 | 6888 | match given by non-greedy C<??> is the zero-length match, and the I<second |
5540 | 6889 | best> match is what is matched by C<\w>. Thus zero-length matches |
5541 | 6890 | alternate with one-character-long matches. |
5542 | 6891 | |
5543 | 6892 | =end original |
5544 | 6893 | |
5545 | 6894 | これは C<< <><b><><a><><r><> >> となります。 |
5546 | 6895 | 文字列の各位置に於いて、貪欲でない C<??> によって得られるベストな |
5547 | 6896 | マッチングはゼロ幅のマッチングです、 |
5548 | 6897 | そして I<2 番目にベストな>マッチングは C<\w> によってマッチングするものです。 |
5549 | 6898 | 従ってゼロ幅のマッチングは 1 文字幅のマッチングの代替となります。 |
5550 | 6899 | |
5551 | 6900 | =begin original |
5552 | 6901 | |
5553 | 6902 | Similarly, for repeated C<m/()/g> the second-best match is the match at the |
5554 | 6903 | position one notch further in the string. |
5555 | 6904 | |
5556 | 6905 | =end original |
5557 | 6906 | |
5558 | 6907 | 同じように、C<m/()/g> の繰り返しでは文字列中の境界一つ遠い位置に 2 番目に |
5559 | 6908 | ベストなマッチングがマッチングします。 |
5560 | 6909 | |
5561 | 6910 | =begin original |
5562 | 6911 | |
5563 | 6912 | The additional state of being I<matched with zero-length> is associated with |
5564 | the matched string, and is reset by each assignment to pos(). | |
6913 | the matched string, and is reset by each assignment to C<pos()>. | |
5565 | 6914 | Zero-length matches at the end of the previous match are ignored |
5566 | 6915 | during C<split>. |
5567 | 6916 | |
5568 | 6917 | =end original |
5569 | 6918 | |
5570 | 6919 | I<ゼロ幅にマッチングしている> という追加の状態はマッチングした文字列に |
5571 | 関連づけられていて、pos() に対する割り当てによってリセットされます。 | |
6920 | 関連づけられていて、C<pos()> に対する割り当てによってリセットされます。 | |
5572 | 6921 | 前のマッチングの終端でのゼロ幅のマッチングは C<split> の間は無視されます。 |
5573 | 6922 | |
5574 | 6923 | =head2 Combining RE Pieces |
5575 | 6924 | |
5576 | 6925 | (RE の欠片の結合) |
5577 | 6926 | |
5578 | 6927 | =begin original |
5579 | 6928 | |
5580 | 6929 | Each of the elementary pieces of regular expressions which were described |
5581 | 6930 | before (such as C<ab> or C<\Z>) could match at most one substring |
5582 | 6931 | at the given position of the input string. However, in a typical regular |
5583 | 6932 | expression these elementary pieces are combined into more complicated |
5584 | patterns using combining operators C<ST>, C<S|T>, C<S*> etc. | |
6933 | patterns using combining operators C<ST>, C<S|T>, C<S*> I<etc>. | |
5585 | (in these examples C<S> and C<T> are regular subexpressions). | |
6934 | (in these examples C<"S"> and C<"T"> are regular subexpressions). | |
5586 | 6935 | |
5587 | 6936 | =end original |
5588 | 6937 | |
5589 | 6938 | これまでに説明された (C<ab> や C<\Z> といった) 正規表現の基本的な欠片 |
5590 | 6939 | それぞれは、入力文字列上の与えられた位置で多くとも1つの部分文字列に |
5591 | 6940 | マッチングします。 |
5592 | 6941 | しかしながら、典型的な正規表現ではこれらの基本的な欠片は結合演算 |
5593 | C<ST>、C<S|T>、C<S*> 等(ここで C<S> や C<T> は正規表現の部分式)を使って | |
6942 | C<ST>、C<S|T>、C<S*> 等(ここで C<"S"> や C<"T"> は正規表現の部分式)を使って | |
5594 | 6943 | より複雑なパターンへと合成することができます。 |
5595 | 6944 | |
5596 | 6945 | =begin original |
5597 | 6946 | |
5598 | 6947 | Such combinations can include alternatives, leading to a problem of choice: |
5599 | 6948 | if we match a regular expression C<a|ab> against C<"abc">, will it match |
5600 | 6949 | substring C<"a"> or C<"ab">? One way to describe which substring is |
5601 | actually matched is the concept of backtracking (see L<"Backtracking">). | |
6950 | actually matched is the concept of backtracking (see L</"Backtracking">). | |
5602 | 6951 | However, this description is too low-level and makes you think |
5603 | 6952 | in terms of a particular implementation. |
5604 | 6953 | |
5605 | 6954 | =end original |
5606 | 6955 | |
5607 | 6956 | このような合成には選択の問題を導くために代替を含めることができます: |
5608 | 6957 | 正規表現 C<a|ab> を C<"abc"> に対してマッチングさせようとしたとき、これは |
5609 | 6958 | C<"a"> と C<"ab"> のどちらにマッチングするのか? |
5610 | 6959 | 実際にどちらがマッチングするのかを説明する1つの方法として、 |
5611 | バックトラッキングのコンセプトがあります(L<"Backtracking"> 参照)。 | |
6960 | バックトラッキングのコンセプトがあります(L</"Backtracking"> 参照)。 | |
5612 | 6961 | しかしながら、この説明は低レベルすぎて特定の実装を考えなければなりません。 |
5613 | 6962 | |
5614 | 6963 | =begin original |
5615 | 6964 | |
5616 | 6965 | Another description starts with notions of "better"/"worse". All the |
5617 | 6966 | substrings which may be matched by the given regular expression can be |
5618 | 6967 | sorted from the "best" match to the "worst" match, and it is the "best" |
5619 | 6968 | match which is chosen. This substitutes the question of "what is chosen?" |
5620 | 6969 | by the question of "which matches are better, and which are worse?". |
5621 | 6970 | |
5622 | 6971 | =end original |
5623 | 6972 | |
5624 | 6973 | もう一つの説明は"より良い"/"より悪い"の考え方で始めます。 |
5625 | 6974 | 与えられた正規表現にマッチングするすべての部分文字列は「最良の」 |
5626 | 6975 | マッチングから「最悪の」マッチングへとソートすることができます; そして |
5627 | 6976 | 「最良の」マッチングが選択されます。 |
5628 | 6977 | これは「どれが選ばれるのか?」という問いかけを「どのマッチングがより良くて、 |
5629 | 6978 | それがより悪いのか?」という問いかけに置き換えることができます。 |
5630 | 6979 | |
5631 | 6980 | =begin original |
5632 | 6981 | |
5633 | 6982 | Again, for elementary pieces there is no such question, since at most |
5634 | 6983 | one match at a given position is possible. This section describes the |
5635 | 6984 | notion of better/worse for combining operators. In the description |
5636 | below C<S> and C<T> are regular subexpressions. | |
6985 | below C<"S"> and C<"T"> are regular subexpressions. | |
5637 | 6986 | |
5638 | 6987 | =end original |
5639 | 6988 | |
5640 | 6989 | そして、基本的な要素ではそういった問いかけはありません; |
5641 | 6990 | なぜならこれらは与えられた位置で可能なマッチングは多くとも1つだからです。 |
5642 | 6991 | このセクションでは結合演算のより良い/より悪いの考え方で説明していきます。 |
5643 | 以下の説明では C<S> 及び C<T> は正規表現の部分式です。 | |
6992 | 以下の説明では C<"S"> 及び C<"T"> は正規表現の部分式です。 | |
5644 | 6993 | |
5645 | 6994 | =over 4 |
5646 | 6995 | |
5647 | 6996 | =item C<ST> |
5648 | 6997 | |
5649 | 6998 | =begin original |
5650 | 6999 | |
5651 | Consider two possible matches, C<AB> and C<A'B'>, C<A> and C<A'> are | |
7000 | Consider two possible matches, C<AB> and C<A'B'>, C<"A"> and C<A'> are | |
5652 | substrings which can be matched by C<S>, C<B> and C<B'> are substrings | |
7001 | substrings which can be matched by C<"S">, C<"B"> and C<B'> are substrings | |
5653 | which can be matched by C<T>. | |
7002 | which can be matched by C<"T">. | |
5654 | 7003 | |
5655 | 7004 | =end original |
5656 | 7005 | |
5657 | 7006 | 2つの可能なマッチング、C<AB> 及び C<A'B'> を考えます; |
5658 | ここで C<A> 及び C<A'> は C<S> にマッチングする部分文字列、 | |
7007 | ここで C<"A"> 及び C<A'> は C<"S"> にマッチングする部分文字列、 | |
5659 | そして C<B> 及び C<B'> は C<T> にマッチングする部分文字列とします。 | |
7008 | そして C<"B"> 及び C<B'> は C<"T"> にマッチングする部分文字列とします。 | |
5660 | 7009 | |
5661 | 7010 | =begin original |
5662 | 7011 | |
5663 | If C<A> is a better match for C<S> than C<A'>, C<AB> is a better | |
7012 | If C<"A"> is a better match for C<"S"> than C<A'>, C<AB> is a better | |
5664 | 7013 | match than C<A'B'>. |
5665 | 7014 | |
5666 | 7015 | =end original |
5667 | 7016 | |
5668 | もし C<A> が C<S> に対して C<A'> よりも良いマッチングであれば、 | |
7017 | もし C<"A"> が C<"S"> に対して C<A'> よりも良いマッチングであれば、 | |
5669 | 7018 | C<AB> は C<A'B'> よりも良いマッチングです。 |
5670 | 7019 | |
5671 | 7020 | =begin original |
5672 | 7021 | |
5673 | If C<A> and C<A'> coincide: C<AB> is a better match than C<AB'> if | |
7022 | If C<"A"> and C<A'> coincide: C<AB> is a better match than C<AB'> if | |
5674 | C<B> is a better match for C<T> than C<B'>. | |
7023 | C<"B"> is a better match for C<"T"> than C<B'>. | |
5675 | 7024 | |
5676 | 7025 | =end original |
5677 | 7026 | |
5678 | もし C<A> と C<A'> が同じであれば: C<B> が C<T> に対して C<B'> よりも | |
7027 | もし C<"A"> と C<A'> が同じであれば: C<"B"> が C<"T"> に対して C<B'> よりも | |
5679 | 7028 | 良いマッチングであれば C<AB> は C<AB'> よりも良いマッチングです。 |
5680 | 7029 | |
5681 | 7030 | =item C<S|T> |
5682 | 7031 | |
5683 | 7032 | =begin original |
5684 | 7033 | |
5685 | When C<S> can match, it is a better match than when only C<T> can match. | |
7034 | When C<"S"> can match, it is a better match than when only C<"T"> can match. | |
5686 | 7035 | |
5687 | 7036 | =end original |
5688 | 7037 | |
5689 | C<S> がマッチングできる時は C<T> のみがマッチングするよりも良い | |
7038 | C<"S"> がマッチングできる時は C<"T"> のみがマッチングするよりも良い | |
5690 | 7039 | マッチングです。 |
5691 | 7040 | |
5692 | 7041 | =begin original |
5693 | 7042 | |
5694 | Ordering of two matches for C<S> is the same as for C<S>. Similar for | |
7043 | Ordering of two matches for C<"S"> is the same as for C<"S">. Similar for | |
5695 | two matches for C<T>. | |
7044 | two matches for C<"T">. | |
5696 | 7045 | |
5697 | 7046 | =end original |
5698 | 7047 | |
5699 | C<S> に対する2つのマッチングの順序は C<S> と同じです。 | |
7048 | C<"S"> に対する2つのマッチングの順序は C<"S"> と同じです。 | |
5700 | C<T> に対する2つのマッチングも同様です。 | |
7049 | C<"T"> に対する2つのマッチングも同様です。 | |
5701 | 7050 | |
5702 | 7051 | =item C<S{REPEAT_COUNT}> |
5703 | 7052 | |
5704 | 7053 | =begin original |
5705 | 7054 | |
5706 | 7055 | Matches as C<SSS...S> (repeated as many times as necessary). |
5707 | 7056 | |
5708 | 7057 | =end original |
5709 | 7058 | |
5710 | 7059 | C<SSS...S> (必要なだけ繰り返し)としてマッチングします。 |
5711 | 7060 | |
5712 | 7061 | =item C<S{min,max}> |
5713 | 7062 | |
5714 | 7063 | =begin original |
5715 | 7064 | |
5716 | 7065 | Matches as C<S{max}|S{max-1}|...|S{min+1}|S{min}>. |
5717 | 7066 | |
5718 | 7067 | =end original |
5719 | 7068 | |
5720 | 7069 | C<S{max}|S{max-1}|...|S{min+1}|S{min}> としてマッチングします。 |
5721 | 7070 | |
5722 | 7071 | =item C<S{min,max}?> |
5723 | 7072 | |
5724 | 7073 | =begin original |
5725 | 7074 | |
5726 | 7075 | Matches as C<S{min}|S{min+1}|...|S{max-1}|S{max}>. |
5727 | 7076 | |
5728 | 7077 | =end original |
5729 | 7078 | |
5730 | 7079 | C<S{min}|S{min+1}|...|S{max-1}|S{max}> としてマッチングします。 |
5731 | 7080 | |
5732 | 7081 | =item C<S?>, C<S*>, C<S+> |
5733 | 7082 | |
5734 | 7083 | =begin original |
5735 | 7084 | |
5736 | 7085 | Same as C<S{0,1}>, C<S{0,BIG_NUMBER}>, C<S{1,BIG_NUMBER}> respectively. |
5737 | 7086 | |
5738 | 7087 | =end original |
5739 | 7088 | |
5740 | 7089 | それぞれ C<S{0,1}>, C<S{0,BIG_NUMBER}>, C<S{1,BIG_NUMBER}> と同じです。 |
5741 | 7090 | |
5742 | 7091 | =item C<S??>, C<S*?>, C<S+?> |
5743 | 7092 | |
5744 | 7093 | =begin original |
5745 | 7094 | |
5746 | 7095 | Same as C<S{0,1}?>, C<S{0,BIG_NUMBER}?>, C<S{1,BIG_NUMBER}?> respectively. |
5747 | 7096 | |
5748 | 7097 | =end original |
5749 | 7098 | |
5750 | 7099 | それぞれ C<S{0,1}?>, C<S{0,BIG_NUMBER}?>, C<S{1,BIG_NUMBER}?> と同じです。 |
5751 | 7100 | |
5752 | 7101 | =item C<< (?>S) >> |
5753 | 7102 | |
5754 | 7103 | =begin original |
5755 | 7104 | |
5756 | Matches the best match for C<S> and only that. | |
7105 | Matches the best match for C<"S"> and only that. | |
5757 | 7106 | |
5758 | 7107 | =end original |
5759 | 7108 | |
5760 | C<S> の最良のみマッチングします。 | |
7109 | C<"S"> の最良のみマッチングします。 | |
5761 | 7110 | |
5762 | 7111 | =item C<(?=S)>, C<(?<=S)> |
5763 | 7112 | |
5764 | 7113 | =begin original |
5765 | 7114 | |
5766 | Only the best match for C<S> is considered. (This is important only if | |
7115 | Only the best match for C<"S"> is considered. (This is important only if | |
5767 | C<S> has capturing parentheses, and backreferences are used somewhere | |
7116 | C<"S"> has capturing parentheses, and backreferences are used somewhere | |
5768 | 7117 | else in the whole regular expression.) |
5769 | 7118 | |
5770 | 7119 | =end original |
5771 | 7120 | |
5772 | C<S> の最良のマッチングのみが考慮されます。 | |
7121 | C<"S"> の最良のマッチングのみが考慮されます。 | |
5773 | (これは C<S> がキャプチャかっこを持っていて、そして正規表現全体の | |
7122 | (これは C<"S"> がキャプチャかっこを持っていて、そして正規表現全体の | |
5774 | 7123 | どこかで後方参照が使われている時のみ重要です.) |
5775 | 7124 | |
5776 | 7125 | =item C<(?!S)>, C<(?<!S)> |
5777 | 7126 | |
5778 | 7127 | =begin original |
5779 | 7128 | |
5780 | 7129 | For this grouping operator there is no need to describe the ordering, since |
5781 | only whether or not C<S> can match is important. | |
7130 | only whether or not C<"S"> can match is important. | |
5782 | 7131 | |
5783 | 7132 | =end original |
5784 | 7133 | |
5785 | このグループ演算子では、C<S> がマッチングできるかどうかのみが重要なので、 | |
7134 | このグループ演算子では、C<"S"> がマッチングできるかどうかのみが重要なので、 | |
5786 | 7135 | 順序についての説明は必要ありません。 |
5787 | 7136 | |
5788 | =item C<(??{ EXPR })>, C<(?PARNO)> | |
7137 | =item C<(??{ EXPR })>, C<(?I<PARNO>)> | |
5789 | 7138 | |
5790 | 7139 | =begin original |
5791 | 7140 | |
5792 | 7141 | The ordering is the same as for the regular expression which is |
5793 | the result of EXPR, or the pattern contained by capture group PARNO. | |
7142 | the result of EXPR, or the pattern contained by capture group I<PARNO>. | |
5794 | 7143 | |
5795 | 7144 | =end original |
5796 | 7145 | |
5797 | 順序は EXPR の結果の正規表現、または捕捉グループ PARNO に含まれている | |
7146 | 順序は EXPR の結果の正規表現、または捕捉グループ I<PARNO> に含まれている | |
5798 | 7147 | パターンと同じです。 |
5799 | 7148 | |
5800 | 7149 | =item C<(?(condition)yes-pattern|no-pattern)> |
5801 | 7150 | |
5802 | 7151 | =begin original |
5803 | 7152 | |
5804 | 7153 | Recall that which of C<yes-pattern> or C<no-pattern> actually matches is |
5805 | 7154 | already determined. The ordering of the matches is the same as for the |
5806 | 7155 | chosen subexpression. |
5807 | 7156 | |
5808 | 7157 | =end original |
5809 | 7158 | |
5810 | 7159 | 既に決定している C<yes-pattern> または C<no-pattern> を実際に |
5811 | 7160 | マッチングさせます。 |
5812 | 7161 | マッチングの順序は選択された部分式と同じです。 |
5813 | 7162 | |
5814 | 7163 | =back |
5815 | 7164 | |
5816 | 7165 | =begin original |
5817 | 7166 | |
5818 | 7167 | The above recipes describe the ordering of matches I<at a given position>. |
5819 | 7168 | One more rule is needed to understand how a match is determined for the |
5820 | 7169 | whole regular expression: a match at an earlier position is always better |
5821 | 7170 | than a match at a later position. |
5822 | 7171 | |
5823 | 7172 | =end original |
5824 | 7173 | |
5825 | 7174 | ここにあげたレシピはI<与えられた位置での>マッチングの順序について |
5826 | 7175 | 説明しています。 |
5827 | 7176 | 正規表現全体でマッチングがどのように決定されるかを理解するためには |
5828 | 7177 | もう少しルールが必要です: |
5829 | 7178 | より若い位置でのマッチングは後ろの方でのマッチングよりもより良いです。 |
5830 | 7179 | |
5831 | 7180 | =head2 Creating Custom RE Engines |
5832 | 7181 | |
5833 | 7182 | (カスタム RE エンジンの作成) |
5834 | 7183 | |
5835 | 7184 | =begin original |
5836 | 7185 | |
5837 | 7186 | As of Perl 5.10.0, one can create custom regular expression engines. This |
5838 | 7187 | is not for the faint of heart, as they have to plug in at the C level. See |
5839 | 7188 | L<perlreapi> for more details. |
5840 | 7189 | |
5841 | 7190 | =end original |
5842 | 7191 | |
5843 | 7192 | Perl 5.10.0 から、誰でもカスタム正規表現エンジンを作成できます。 |
5844 | 7193 | これは気弱な人向けではありません; C レベルでプラグインする必要があるからです。 |
5845 | 7194 | さらなる詳細については L<perlreapi> を参照して下さい。 |
5846 | 7195 | |
5847 | 7196 | =begin original |
5848 | 7197 | |
5849 | 7198 | As an alternative, overloaded constants (see L<overload>) provide a simple |
5850 | 7199 | way to extend the functionality of the RE engine, by substituting one |
5851 | 7200 | pattern for another. |
5852 | 7201 | |
5853 | 7202 | =end original |
5854 | 7203 | |
5855 | 7204 | 代替案として、オーバーロードされた定数(L<overload> 参照)は |
5856 | 7205 | あるパターンを別のパターンに置き換えることで、RE エンジンの機能を |
5857 | 7206 | 拡張する簡単な方法を提供します。 |
5858 | 7207 | |
5859 | 7208 | =begin original |
5860 | 7209 | |
5861 | 7210 | Suppose that we want to enable a new RE escape-sequence C<\Y|> which |
5862 | 7211 | matches at a boundary between whitespace characters and non-whitespace |
5863 | 7212 | characters. Note that C<(?=\S)(?<!\S)|(?!\S)(?<=\S)> matches exactly |
5864 | 7213 | at these positions, so we want to have each C<\Y|> in the place of the |
5865 | 7214 | more complicated version. We can create a module C<customre> to do |
5866 | 7215 | this: |
5867 | 7216 | |
5868 | 7217 | =end original |
5869 | 7218 | |
5870 | 7219 | 新しい正規表現エスケープシーケンス、空白文字と非空白文字との |
5871 | 7220 | 境界にマッチングする C<\Y|> を作ってみることにします。 |
5872 | 7221 | この位置には実際には C<(?=\S)(?<!\S)|(?!\S)(?<=\S)> がマッチングするので、 |
5873 | 7222 | この複雑なバージョンを C<\Y|> で置き換えたいとします。 |
5874 | 7223 | このために C<customre> モジュールを作ります: |
5875 | 7224 | |
5876 | 7225 | package customre; |
5877 | 7226 | use overload; |
5878 | 7227 | |
5879 | 7228 | sub import { |
5880 | 7229 | shift; |
5881 | 7230 | die "No argument to customre::import allowed" if @_; |
5882 | 7231 | overload::constant 'qr' => \&convert; |
5883 | 7232 | } |
5884 | 7233 | |
5885 | 7234 | sub invalid { die "/$_[0]/: invalid escape '\\$_[1]'"} |
5886 | 7235 | |
5887 | 7236 | # We must also take care of not escaping the legitimate \\Y| |
5888 | 7237 | # sequence, hence the presence of '\\' in the conversion rules. |
5889 | 7238 | my %rules = ( '\\' => '\\\\', |
5890 | 7239 | 'Y|' => qr/(?=\S)(?<!\S)|(?!\S)(?<=\S)/ ); |
5891 | 7240 | sub convert { |
5892 | 7241 | my $re = shift; |
5893 | 7242 | $re =~ s{ |
5894 | 7243 | \\ ( \\ | Y . ) |
5895 | 7244 | } |
5896 | 7245 | { $rules{$1} or invalid($re,$1) }sgex; |
5897 | 7246 | return $re; |
5898 | 7247 | } |
5899 | 7248 | |
5900 | 7249 | =begin original |
5901 | 7250 | |
5902 | 7251 | Now C<use customre> enables the new escape in constant regular |
5903 | expressions, i.e., those without any runtime variable interpolations. | |
7252 | expressions, I<i.e.>, those without any runtime variable interpolations. | |
5904 | 7253 | As documented in L<overload>, this conversion will work only over |
5905 | 7254 | literal parts of regular expressions. For C<\Y|$re\Y|> the variable |
5906 | 7255 | part of this regular expression needs to be converted explicitly |
5907 | (but only if the special meaning of C<\Y|> should be enabled inside $re): | |
7256 | (but only if the special meaning of C<\Y|> should be enabled inside C<$re>): | |
5908 | 7257 | |
5909 | 7258 | =end original |
5910 | 7259 | |
5911 | 7260 | これで C<use customre> することで正規表現定数の中で新しいエスケープを |
5912 | 7261 | 使うことが出来ます; すなわち、これには何の実行時変数の埋め込みもいりません。 |
5913 | 7262 | L<overload> に書かれているように、この変換は正規表現のリテラル部分にのみ |
5914 | 7263 | 動作します。 |
5915 | 7264 | C<\Y|$re\Y|> であればこの正規表現の変数部分は明示的に変換する |
5916 | 必要があります(とはいえ $re の中でも C<\Y|> を有効にしたい時のみ)。 | |
7265 | 必要があります(とはいえ C<$re> の中でも C<\Y|> を有効にしたい時のみ)。 | |
5917 | 7266 | |
5918 | 7267 | use customre; |
5919 | 7268 | $re = <>; |
5920 | 7269 | chomp $re; |
5921 | 7270 | $re = customre::convert $re; |
5922 | 7271 | /\Y|$re\Y|/; |
5923 | 7272 | |
7273 | =head2 Embedded Code Execution Frequency | |
7274 | ||
7275 | (組み込みコードの実行頻度) | |
7276 | ||
7277 | =begin original | |
7278 | ||
7279 | The exact rules for how often C<(??{})> and C<(?{})> are executed in a pattern | |
7280 | are unspecified. In the case of a successful match you can assume that | |
7281 | they DWIM and will be executed in left to right order the appropriate | |
7282 | number of times in the accepting path of the pattern as would any other | |
7283 | meta-pattern. How non-accepting pathways and match failures affect the | |
7284 | number of times a pattern is executed is specifically unspecified and | |
7285 | may vary depending on what optimizations can be applied to the pattern | |
7286 | and is likely to change from version to version. | |
7287 | ||
7288 | =end original | |
7289 | ||
7290 | パターン中で C<(??{})> と C<(?{})> がどれくらいの頻度で実行されるかの | |
7291 | 正確な規則は未規定です。 | |
7292 | マッチングが成功した場合、それらは DWIM を行い、 | |
7293 | 他のメタパターンと同様、 | |
7294 | パターンの受け入れられたパスの中で左から右の順序で適切な回数 | |
7295 | 実行されることを仮定できます。 | |
7296 | 受け入れられなかったパスとマッチングの失敗がどれくらいパターンの実行回数に | |
7297 | 影響を与えるかは明確に非規定で、 | |
7298 | パターンにどの最適化が適用できるかに依存し、バージョン毎に | |
7299 | 変わる可能性が高いです。 | |
7300 | ||
7301 | =begin original | |
7302 | ||
7303 | For instance in | |
7304 | ||
7305 | =end original | |
7306 | ||
7307 | 例えば: | |
7308 | ||
7309 | "aaabcdeeeee"=~/a(?{print "a"})b(?{print "b"})cde/; | |
7310 | ||
7311 | =begin original | |
7312 | ||
7313 | the exact number of times "a" or "b" are printed out is unspecified for | |
7314 | failure, but you may assume they will be printed at least once during | |
7315 | a successful match, additionally you may assume that if "b" is printed, | |
7316 | it will be preceded by at least one "a". | |
7317 | ||
7318 | =end original | |
7319 | ||
7320 | 失敗時に "a" や "b" が何回表示されるかは未規定ですが、 | |
7321 | マッチングに成功したときに少なくとも 1 回表示されることは仮定でき、 | |
7322 | さらに "b" が表示されるとき、その前には少なくとも 1 回 "a" が | |
7323 | 表示されることも仮定できます。 | |
7324 | ||
7325 | =begin original | |
7326 | ||
7327 | In the case of branching constructs like the following: | |
7328 | ||
7329 | =end original | |
7330 | ||
7331 | 次のような分岐構文の場合: | |
7332 | ||
7333 | /a(b|(?{ print "a" }))c(?{ print "c" })/; | |
7334 | ||
7335 | =begin original | |
7336 | ||
7337 | you can assume that the input "ac" will output "ac", and that "abc" | |
7338 | will output only "c". | |
7339 | ||
7340 | =end original | |
7341 | ||
7342 | 入力が "ac" なら出力は "ac"、入力が "abc" なら出力は | |
7343 | "c" だけと仮定できます。 | |
7344 | ||
7345 | =begin original | |
7346 | ||
7347 | When embedded code is quantified, successful matches will call the | |
7348 | code once for each matched iteration of the quantifier. For | |
7349 | example: | |
7350 | ||
7351 | =end original | |
7352 | ||
7353 | 組み込みコードが量指定された場合、マッチングに成功すると | |
7354 | 量指定子のそれぞれのマッチングした反復毎に 1 回コードを呼び出します。 | |
7355 | 例えば: | |
7356 | ||
7357 | "good" =~ /g(?:o(?{print "o"}))*d/; | |
7358 | ||
7359 | =begin original | |
7360 | ||
7361 | will output "o" twice. | |
7362 | ||
7363 | =end original | |
7364 | ||
7365 | これは "o" を 2 回出力します。 | |
7366 | ||
5924 | 7367 | =head2 PCRE/Python Support |
5925 | 7368 | |
5926 | 7369 | (PCRE/Python サポート) |
5927 | 7370 | |
5928 | 7371 | =begin original |
5929 | 7372 | |
5930 | 7373 | As of Perl 5.10.0, Perl supports several Python/PCRE-specific extensions |
5931 | 7374 | to the regex syntax. While Perl programmers are encouraged to use the |
5932 | 7375 | Perl-specific syntax, the following are also accepted: |
5933 | 7376 | |
5934 | 7377 | =end original |
5935 | 7378 | |
5936 | 7379 | Perl 5.10.0 時点では Perl は幾つかの Python/PCRE 的な正規表現構文拡張を |
5937 | 7380 | サポートします。 |
5938 | 7381 | Perl プログラマはこれらの Perl としての構文を推奨しますが、以下のものも |
5939 | 7382 | 受理されます: |
5940 | 7383 | |
5941 | 7384 | =over 4 |
5942 | 7385 | |
5943 | 7386 | =item C<< (?PE<lt>NAMEE<gt>pattern) >> |
5944 | 7387 | |
5945 | 7388 | =begin original |
5946 | 7389 | |
5947 | 7390 | Define a named capture group. Equivalent to C<< (?<NAME>pattern) >>. |
5948 | 7391 | |
5949 | 7392 | =end original |
5950 | 7393 | |
5951 | 7394 | 名前付の捕捉グループの定義。 |
5952 | 7395 | C<< (?<NAME>pattern) >> と等価。 |
5953 | 7396 | |
5954 | 7397 | =item C<< (?P=NAME) >> |
5955 | 7398 | |
5956 | 7399 | =begin original |
5957 | 7400 | |
5958 | 7401 | Backreference to a named capture group. Equivalent to C<< \g{NAME} >>. |
5959 | 7402 | |
5960 | 7403 | =end original |
5961 | 7404 | |
5962 | 7405 | 名前付捕捉グループへの後方参照。 |
5963 | 7406 | C<< \g{NAME} >> と等価。 |
5964 | 7407 | |
5965 | 7408 | =item C<< (?P>NAME) >> |
5966 | 7409 | |
5967 | 7410 | =begin original |
5968 | 7411 | |
5969 | 7412 | Subroutine call to a named capture group. Equivalent to C<< (?&NAME) >>. |
5970 | 7413 | |
5971 | 7414 | =end original |
5972 | 7415 | |
5973 | 7416 | 名前付き捕捉グループへの関数呼び出し。 |
5974 | 7417 | C<< (?&NAME) >> と等価。 |
5975 | 7418 | |
5976 | 7419 | =back |
5977 | 7420 | |
5978 | 7421 | =head1 BUGS |
5979 | 7422 | |
5980 | 7423 | =begin original |
5981 | 7424 | |
5982 | Many regular expression constructs don't work on EBCDIC platforms. | |
5983 | ||
5984 | =end original | |
5985 | ||
5986 | 多くの正規表現構文は EBCDIC プラットフォームでは動作しません。 | |
5987 | ||
5988 | =begin original | |
5989 | ||
5990 | 7425 | There are a number of issues with regard to case-insensitive matching |
5991 | in Unicode rules. See C<i> under L</Modifiers> above. | |
7426 | in Unicode rules. See C<"i"> under L</Modifiers> above. | |
5992 | 7427 | |
5993 | 7428 | =end original |
5994 | 7429 | |
5995 | 7430 | Unicode ルールでの大文字小文字を無視したマッチングには多くの問題が |
5996 | 7431 | あります。 |
5997 | 上述の L</Modifiers> の C<i> を参照してください。 | |
7432 | 上述の L</Modifiers> の C<"i"> を参照してください。 | |
5998 | 7433 | |
5999 | 7434 | =begin original |
6000 | 7435 | |
6001 | 7436 | This document varies from difficult to understand to completely |
6002 | 7437 | and utterly opaque. The wandering prose riddled with jargon is |
6003 | 7438 | hard to fathom in several places. |
6004 | 7439 | |
6005 | 7440 | =end original |
6006 | 7441 | |
6007 | 7442 | この文書は、理解が困難なところから、完全かつ徹底的に不明瞭なところまで |
6008 | 7443 | さまざまです。 |
6009 | 7444 | jargon に満ちたとりとめのない散文は幾つかの箇所で理解するのに |
6010 | 7445 | 難儀ではあるでしょう。 |
6011 | 7446 | |
6012 | 7447 | =begin original |
6013 | 7448 | |
6014 | 7449 | This document needs a rewrite that separates the tutorial content |
6015 | 7450 | from the reference content. |
6016 | 7451 | |
6017 | 7452 | =end original |
6018 | 7453 | |
6019 | 7454 | この文書はリファレンス的な内容からチュートリアル的な内容を分離して |
6020 | 7455 | 書き直す必要があります。 |
6021 | 7456 | |
6022 | 7457 | =head1 SEE ALSO |
7458 | ||
7459 | =begin original | |
7460 | ||
7461 | The syntax of patterns used in Perl pattern matching evolved from those | |
7462 | supplied in the Bell Labs Research Unix 8th Edition (Version 8) regex | |
7463 | routines. (The code is actually derived (distantly) from Henry | |
7464 | Spencer's freely redistributable reimplementation of those V8 routines.) | |
7465 | ||
7466 | =end original | |
7467 | ||
7468 | Perl のパターンマッチングで使われる文法は、 | |
7469 | the Bell Labs Research Unix 8th Edition (Version 8) 正規表現ルーチンで | |
7470 | 提供されているものからの派生です。 | |
7471 | (コードは実際には Henry Spencer の自由に再配布可能な V8 ルーチンの再実装から | |
7472 | (遠く)派生しています)。 | |
6023 | 7473 | |
6024 | 7474 | L<perlrequick>. |
6025 | 7475 | |
6026 | 7476 | L<perlretut>. |
6027 | 7477 | |
6028 | 7478 | L<perlop/"Regexp Quote-Like Operators">. |
6029 | 7479 | |
6030 | 7480 | L<perlop/"Gory details of parsing quoted constructs">. |
6031 | 7481 | |
6032 | 7482 | L<perlfaq6>. |
6033 | 7483 | |
6034 | 7484 | L<perlfunc/pos>. |
6035 | 7485 | |
6036 | 7486 | L<perllocale>. |
6037 | 7487 | |
6038 | 7488 | L<perlebcdic>. |
6039 | 7489 | |
6040 | 7490 | =begin original |
6041 | 7491 | |
6042 | 7492 | I<Mastering Regular Expressions> by Jeffrey Friedl, published |
6043 | 7493 | by O'Reilly and Associates. |
6044 | 7494 | |
6045 | 7495 | =end original |
6046 | 7496 | |
6047 | 7497 | O'Reilly and Associates から出版されている、Jeffrey Friedl による |
6048 | 7498 | I<Mastering Regular Expressions> (詳説 正規表現) |
6049 | 7499 | |
6050 | 7500 | =begin meta |
6051 | 7501 | |
6052 | 7502 | Translate: 山科 氷魚 (YAMASHINA Hio) <hio@hio.jp> (5.10.0) |
6053 | 7503 | Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.1-) |
6054 | 7504 | Status: completed |
6055 | 7505 | |
6056 | 7506 | =end meta |