perlre >
5.16.1
との差分
perlre 5.16.1 と 5.30.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 | original's rules continue to apply to it, and on | |
1378 | original's rules continue to apply to it, and don't affect the other | |
1379 | parts. | |
479 | 1380 | |
480 | 1381 | =end original |
481 | 1382 | |
482 | 1383 | 任意のある瞬間において、これらの修飾子の内正確に一つだけが有効になります。 |
483 | 1384 | これにより、 |
484 | 1385 | それが実際に実行されるときにどの規則が有効かに関わらず、 |
485 | 1386 | Perl が元々コンパイルされた正規表現の振る舞いを保存できるようにします。 |
486 | そしてそれがより大きな正規表現に展開された場合、元の規則 | |
1387 | そしてそれがより大きな正規表現に展開された場合、元の規則は | |
487 | 適用され続けま | |
1388 | その部分にだけ適用され続け、他の部分には影響を与えません。 | |
488 | 1389 | |
489 | 1390 | =begin original |
490 | 1391 | |
491 | 1392 | The C</l> and C</u> modifiers are automatically selected for |
492 | 1393 | regular expressions compiled within the scope of various pragmas, |
493 | 1394 | and we recommend that in general, you use those pragmas instead of |
494 | 1395 | specifying these modifiers explicitly. For one thing, the modifiers |
495 | 1396 | affect only pattern matching, and do not extend to even any replacement |
496 | done, whereas using the pragmas give consistent results for all | |
1397 | done, whereas using the pragmas gives consistent results for all | |
497 | 1398 | appropriate operations within their scopes. For example, |
498 | 1399 | |
499 | 1400 | =end original |
500 | 1401 | |
501 | 1402 | C</l> と C</u> の修飾子は、様々なプラグマのスコープ内でコンパイルされた |
502 | 1403 | 正規表現で自動的に選択されます; |
503 | 1404 | 一般的にはこれらの修飾子を明示的に使うのではなく、これらのプラグマを |
504 | 1405 | 使うことを勧めます。 |
505 | 1406 | 一例を挙げると、修飾子はパターンマッチングに対してのみ影響を与え、 |
506 | 1407 | 置換には拡張されないことに注意してください; |
507 | 1408 | いっぽうプラグマを使うと、そのスコープ内の全ての適切な操作について |
508 | 1409 | 一貫した結果となります。 |
509 | 1410 | 例えば: |
510 | 1411 | |
511 | 1412 | s/foo/\Ubar/il |
512 | 1413 | |
513 | 1414 | =begin original |
514 | 1415 | |
515 | 1416 | will match "foo" using the locale's rules for case-insensitive matching, |
516 | 1417 | but the C</l> does not affect how the C<\U> operates. Most likely you |
517 | 1418 | want both of them to use locale rules. To do this, instead compile the |
518 | 1419 | 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 | |
1420 | 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. | |
1421 | lesson is to C<use locale>, and not C</l> explicitly. | |
521 | 1422 | |
522 | 1423 | =end original |
523 | 1424 | |
524 | 1425 | これは大文字小文字マッチングにロケールの規則を使って "foo" に |
525 | 1426 | マッチングしますが、C</l> は C<\U> がどう処理を行うかに影響を与えません。 |
526 | 1427 | あなたはほぼ確実にこれら二つにロケールの規則を使うことを臨むはずです。 |
527 | 1428 | これをするためには、代わりに |
528 | 1429 | C<use locale> のスコープ内で正規表現をコンパイルします。 |
529 | これらは両方とも暗黙に C</l> を追加し、C<\U> にロケールの規則を適用します。 | |
1430 | これらは両方とも暗黙に C</l> を追加し、C<\U> にロケールの規則を適用します。 | |
530 | 1431 | ここで学ぶべきことは、C<use locale> を使って、明示的に C</l> を |
531 | 1432 | 使わないことです。 |
532 | 1433 | |
533 | 1434 | =begin original |
534 | 1435 | |
535 | 1436 | Similarly, it would be better to use C<use feature 'unicode_strings'> |
536 | 1437 | instead of, |
537 | 1438 | |
538 | 1439 | =end original |
539 | 1440 | |
540 | 1441 | 同様に、Unicode の規則にするには次のようにする代わりに |
541 | 1442 | C<use feature 'unicode_strings'> を使った方が良いです: |
542 | 1443 | |
543 | 1444 | s/foo/\Lbar/iu |
544 | 1445 | |
545 | 1446 | =begin original |
546 | 1447 | |
547 | 1448 | to get Unicode rules, as the C<\L> in the former (but not necessarily |
548 | 1449 | the latter) would also use Unicode rules. |
549 | 1450 | |
550 | 1451 | =end original |
551 | 1452 | |
552 | 1453 | 前者の C<\L> (しかし後者は必要ではないです) も Unicode の規則を |
553 | 1454 | 使うからです。 |
554 | 1455 | |
555 | 1456 | =begin original |
556 | 1457 | |
557 | 1458 | More detail on each of the modifiers follows. Most likely you don't |
558 | 1459 | need to know this detail for C</l>, C</u>, and C</d>, and can skip ahead |
559 | 1460 | to L<E<sol>a|/E<sol>a (and E<sol>aa)>. |
560 | 1461 | |
561 | 1462 | =end original |
562 | 1463 | |
563 | 1464 | それぞれの修飾子に関するさらなる詳細は後述します。 |
564 | 1465 | ほとんど確実にあなたは C</l>, C</u>, C</d> の詳細を知る必要はなく、 |
565 | 1466 | L<E<sol>a|/E<sol>a (and E<sol>aa)> まで読み飛ばせます。 |
566 | 1467 | |
567 | 1468 | =head4 /l |
568 | 1469 | |
569 | 1470 | =begin original |
570 | 1471 | |
571 | 1472 | means to use the current locale's rules (see L<perllocale>) when pattern |
572 | 1473 | matching. For example, C<\w> will match the "word" characters of that |
573 | 1474 | locale, and C<"/i"> case-insensitive matching will match according to |
574 | 1475 | the locale's case folding rules. The locale used will be the one in |
575 | 1476 | effect at the time of execution of the pattern match. This may not be |
576 | 1477 | the same as the compilation-time locale, and can differ from one match |
577 | 1478 | to another if there is an intervening call of the |
578 | 1479 | L<setlocale() function|perllocale/The setlocale function>. |
579 | 1480 | |
580 | 1481 | =end original |
581 | 1482 | |
582 | 1483 | これはパターンマッチングのときに現在のロケールの規則 (L<perllocale> 参照) を |
583 | 1484 | 使うことを意味します。 |
584 | 1485 | 例えば、C<\w> はこのロケールの「単語」文字にマッチングし、 |
585 | 1486 | C<"/i"> の大文字小文字マッチングは、現在のロケールの大文字小文字畳み込み規則に |
586 | 1487 | 従ってマッチングします。 |
587 | 1488 | 使われるロケールはパターンマッチングが実行される時点で有効なものです。 |
588 | 1489 | これはコンパイル時のロケールと同じではないかもしれませんし、 |
589 | 1490 | L<setlocale() 関数|perllocale/The setlocale function> |
590 | 1491 | の呼び出しが間に入ると、呼び出し毎に異なることもあります。 |
591 | 1492 | |
592 | 1493 | =begin original |
593 | 1494 | |
594 | Perl | |
1495 | Prior to v5.20, Perl did not support multi-byte locales. Starting then, | |
595 | ||
1496 | UTF-8 locales are supported. No other multi byte locales are ever | |
1497 | likely to be supported. However, in all locales, one can have code | |
1498 | points above 255 and these will always be treated as Unicode no matter | |
1499 | what locale is in effect. | |
1500 | ||
1501 | =end original | |
1502 | ||
1503 | v5.20 より前では、Perl は複数バイトロケールに対応していませんでした。 | |
1504 | v5.20 から、UTF-8 ロケールに対応しました。 | |
1505 | 他の複数バイトロケールに対応することはおそらくありません。 | |
1506 | しかし、全てのロケールで、255 を越える符号位置を保持することができ、 | |
1507 | これはどのロケールが有効であるかに関わらず常に | |
1508 | Unicode として扱われます。 | |
1509 | ||
1510 | =begin original | |
1511 | ||
596 | 1512 | Under Unicode rules, there are a few case-insensitive matches that cross |
597 | the 255/256 boundary. | |
1513 | the 255/256 boundary. Except for UTF-8 locales in Perls v5.20 and | |
598 | ||
1514 | later, these are disallowed under C</l>. For example, 0xFF (on ASCII | |
599 | ||
1515 | platforms) does not caselessly match the character at 0x178, C<LATIN | |
600 | C | |
1516 | CAPITAL LETTER Y WITH DIAERESIS>, because 0xFF may not be C<LATIN SMALL | |
601 | ||
1517 | LETTER Y WITH DIAERESIS> in the current locale, and Perl has no way of | |
602 | ||
1518 | knowing if that character even exists in the locale, much less what code | |
1519 | point it is. | |
603 | 1520 | |
604 | 1521 | =end original |
605 | 1522 | |
606 | Perl は単一バイトロケールのみに対応します。 | |
607 | つまり、255 を越える符号位置は、どのロケールが有効であるかに関わらず | |
608 | Unicode として扱われるということです。 | |
609 | 1523 | Unicode の規則の基では、255/256 の境界をまたぐ大文字小文字を無視した |
610 | 1524 | マッチングがいくつかあります。 |
1525 | Perl v5.20 以降での UTF-8 ロケールを除いて、 | |
611 | 1526 | これらは C</l> の基では認められません。 |
612 | 1527 | 例えば、(ASCII プラットフォームで) 0xFF は 0x178, |
613 | 1528 | C<LATIN CAPITAL LETTER Y WITH DIAERESIS> と大文字小文字を無視した |
614 | 1529 | マッチングをしません; なぜなら 0xFF は現在のロケールでは |
615 | 1530 | C<LATIN SMALL LETTER Y WITH DIAERESIS> ではないかもしれず、Perl は |
616 | 1531 | このロケールでこの文字があるかどうかすら知る方法がなく、ましてや |
617 | 1532 | これがどの符号位置かを知る方法もないからです。 |
618 | 1533 | |
619 | 1534 | =begin original |
620 | 1535 | |
1536 | In a UTF-8 locale in v5.20 and later, the only visible difference | |
1537 | between locale and non-locale in regular expressions should be tainting | |
1538 | (see L<perlsec>). | |
1539 | ||
1540 | =end original | |
1541 | ||
1542 | v5.20 以降の UTF-8 ロケールでは、 | |
1543 | 正規表現でのロケールと非ロケールの目に見える唯一の違いは | |
1544 | 汚染だけのはずです | |
1545 | (L<perlsec> 参照)。 | |
1546 | ||
1547 | =begin original | |
1548 | ||
621 | 1549 | This modifier may be specified to be the default by C<use locale>, but |
622 | 1550 | see L</Which character set modifier is in effect?>. |
623 | 1551 | X</l> |
624 | 1552 | |
625 | 1553 | =end original |
626 | 1554 | |
627 | 1555 | この修飾子は C<use locale> によってデフォルトで設定されますが、 |
628 | 1556 | L</Which character set modifier is in effect?> を参照してください。 |
629 | 1557 | X</l> |
630 | 1558 | |
631 | 1559 | =head4 /u |
632 | 1560 | |
633 | 1561 | =begin original |
634 | 1562 | |
635 | 1563 | means to use Unicode rules when pattern matching. On ASCII platforms, |
636 | 1564 | this means that the code points between 128 and 255 take on their |
637 | 1565 | Latin-1 (ISO-8859-1) meanings (which are the same as Unicode's). |
638 | 1566 | (Otherwise Perl considers their meanings to be undefined.) Thus, |
639 | 1567 | under this modifier, the ASCII platform effectively becomes a Unicode |
640 | 1568 | platform; and hence, for example, C<\w> will match any of the more than |
641 | 1569 | 100_000 word characters in Unicode. |
642 | 1570 | |
643 | 1571 | =end original |
644 | 1572 | |
645 | 1573 | これはパターンマッチングのときに Unicode の規則を使うことを意味します。 |
646 | 1574 | ASCII プラットフォームでは、これは符号位置 128 から 255 は |
647 | 1575 | Latin-1 (ISO-8859-1) という意味になります (これは Unicode と同じです)。 |
648 | 1576 | (さもなければ Perl はこれらの意味は未定義として扱います。) |
649 | 1577 | 従って、この修飾子の基では、ASCII プラットフォームは |
650 | 1578 | 事実上 Unicode プラットフォームになります; 従って、 |
651 | 1579 | 例えば、C<\w> は Unicode の 100,000 以上の単語文字のどれにも |
652 | 1580 | マッチングします。 |
653 | 1581 | |
654 | 1582 | =begin original |
655 | 1583 | |
656 | 1584 | Unlike most locales, which are specific to a language and country pair, |
657 | 1585 | Unicode classifies all the characters that are letters I<somewhere> in |
658 | 1586 | the world as |
659 | 1587 | C<\w>. For example, your locale might not think that C<LATIN SMALL |
660 | 1588 | LETTER ETH> is a letter (unless you happen to speak Icelandic), but |
661 | 1589 | Unicode does. Similarly, all the characters that are decimal digits |
662 | 1590 | somewhere in the world will match C<\d>; this is hundreds, not 10, |
663 | 1591 | possible matches. And some of those digits look like some of the 10 |
664 | 1592 | ASCII digits, but mean a different number, so a human could easily think |
665 | 1593 | a number is a different quantity than it really is. For example, |
666 | 1594 | C<BENGALI DIGIT FOUR> (U+09EA) looks very much like an |
667 | C<ASCII DIGIT EIGHT> (U+0038) | |
1595 | C<ASCII DIGIT EIGHT> (U+0038), and C<LEPCHA DIGIT SIX> (U+1C46) looks | |
668 | ||
1596 | very much like an C<ASCII DIGIT FIVE> (U+0035). And, C<\d+>, may match | |
669 | is | |
1597 | strings of digits that are a mixture from different writing systems, | |
670 | t | |
1598 | creating a security issue. A fraudulent website, for example, could | |
671 | ||
1599 | display the price of something using U+1C46, and it would appear to the | |
1600 | user that something cost 500 units, but it really costs 600. A browser | |
1601 | that enforced script runs (L</Script Runs>) would prevent that | |
1602 | fraudulent display. L<Unicode::UCD/num()> can also be used to sort this | |
1603 | out. Or the C</a> modifier can be used to force C<\d> to match just the | |
1604 | ASCII 0 through 9. | |
672 | 1605 | |
673 | 1606 | =end original |
674 | 1607 | |
675 | 1608 | 特定の言語と国に固有であるほとんどのロケールと異なり、 |
676 | 1609 | Unicode は世界の I<どこか> で字(letter)として扱われている全ての |
677 | 1610 | 文字(character)を C<\w> に分類します。 |
678 | 1611 | 例えば、あなたのロケールは (あなたがたまたまアイスランド語を話さない限り) |
679 | 1612 | C<LATIN SMALL LETTER ETH> を字として考えないかもしれません。 |
680 | 1613 | 同様に、世界のどこかで数字である全ての文字は C<\d> にマッチングします; |
681 | 1614 | これは 10 ではなく 100 のマッチングの可能性があります。 |
682 | 1615 | さらにこれらの数字の一部は 10 の ASCII 数字と似ていますが、 |
683 | 1616 | 異なる数字を意味するため、人間はその数字が実際と異なる量であると |
684 | 1617 | 簡単に考えてしまいます。 |
685 | 1618 | 例えば、 C<BENGALI DIGIT FOUR> (U+09EA) は C<ASCII DIGIT EIGHT> (U+0038) に |
1619 | とてもよく似ていて、 | |
1620 | C<LEPCHA DIGIT SIX> (U+1C46) は C<ASCII DIGIT FIVE> (U+0035) に | |
686 | 1621 | とてもよく似ています。 |
687 | 1622 | C<\d+> は、異なる記法から混ぜた数字の文字列にマッチングするので、 |
688 | 1623 | セキュリティ上の問題を作ります。 |
689 | ||
1624 | 例えば、詐欺サイトは、何か U+1C46 を使った価格を表示し、 | |
1625 | 何か 500 ユニットのコストであるかのようにユーザーに見えるけれども、 | |
1626 | 実際のコストは 600 にできます。 | |
1627 | 用字連続 (L</Script Runs>) を矯正するブラウザはこのような | |
1628 | 詐欺的な表示を防ぎます。 | |
1629 | これを整理するために L<Unicode::UCD/num()> も使えます。 | |
690 | 1630 | あるいは C</a> 修飾子は、C<\d> が単に ASCII の 0 から 9 に |
691 | 1631 | マッチングすることを強制するために使えます。 |
692 | 1632 | |
693 | 1633 | =begin original |
694 | 1634 | |
695 | 1635 | Also, under this modifier, case-insensitive matching works on the full |
696 | 1636 | set of Unicode |
697 | 1637 | characters. The C<KELVIN SIGN>, for example matches the letters "k" and |
698 | 1638 | "K"; and C<LATIN SMALL LIGATURE FF> matches the sequence "ff", which, |
699 | 1639 | if you're not prepared, might make it look like a hexadecimal constant, |
700 | 1640 | presenting another potential security issue. See |
701 | 1641 | L<http://unicode.org/reports/tr36> for a detailed discussion of Unicode |
702 | 1642 | security issues. |
703 | 1643 | |
704 | 1644 | =end original |
705 | 1645 | |
706 | 1646 | また、この修飾子の基では、大文字小文字を無視したマッチングは Unicode の |
707 | 1647 | 完全な集合で動作します。 |
708 | 1648 | 例えば C<KELVIN SIGN> は "k" と "K" にマッチングします; |
709 | 1649 | C<LATIN SMALL LIGATURE FF> は、準備していなければ 16 進数定数のように |
710 | 1650 | 見えるかもしれない並び "ff" にマッチングし、もう一つの潜在的な |
711 | 1651 | セキュリティ問題になります。 |
712 | 1652 | Unicode のセキュリティ問題に関する詳細な議論については |
713 | 1653 | L<http://unicode.org/reports/tr36> を参照してください。 |
714 | 1654 | |
715 | 1655 | =begin original |
716 | 1656 | |
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 | 1657 | This modifier may be specified to be the default by C<use feature |
740 | 1658 | 'unicode_strings>, C<use locale ':not_characters'>, or |
741 | 1659 | C<L<use 5.012|perlfunc/use VERSION>> (or higher), |
742 | 1660 | but see L</Which character set modifier is in effect?>. |
743 | 1661 | X</u> |
744 | 1662 | |
745 | 1663 | =end original |
746 | 1664 | |
747 | 1665 | この修飾子は C<use feature 'unicode_strings>, |
748 | 1666 | C<use locale ':not_characters'>, C<L<use 5.012|perlfunc/use VERSION>> |
749 | 1667 | (またはそれ以上) によってデフォルトに |
750 | 1668 | 設定されますが、L</Which character set modifier is in effect?> を |
751 | 1669 | 参照してください。 |
752 | 1670 | X</u> |
753 | 1671 | |
754 | 1672 | =head4 /d |
755 | 1673 | |
756 | 1674 | =begin original |
757 | 1675 | |
758 | 1676 | This modifier means to use the "Default" native rules of the platform |
759 | 1677 | except when there is cause to use Unicode rules instead, as follows: |
760 | 1678 | |
761 | 1679 | =end original |
762 | 1680 | |
763 | 1681 | この修飾子は、以下のように Unicode の規則が使われる場合を除いて、 |
764 | 1682 | プラットフォームの「デフォルトの」(Default) ネイティブな規則を使うことを |
765 | 1683 | 意味します: |
766 | 1684 | |
767 | 1685 | =over 4 |
768 | 1686 | |
769 | 1687 | =item 1 |
770 | 1688 | |
771 | 1689 | =begin original |
772 | 1690 | |
773 | 1691 | the target string is encoded in UTF-8; or |
774 | 1692 | |
775 | 1693 | =end original |
776 | 1694 | |
777 | 1695 | ターゲット文字列が UTF-8 でエンコードされている; または |
778 | 1696 | |
779 | 1697 | =item 2 |
780 | 1698 | |
781 | 1699 | =begin original |
782 | 1700 | |
783 | 1701 | the pattern is encoded in UTF-8; or |
784 | 1702 | |
785 | 1703 | =end original |
786 | 1704 | |
787 | 1705 | パターンが UTF-8 でエンコードされている; または |
788 | 1706 | |
789 | 1707 | =item 3 |
790 | 1708 | |
791 | 1709 | =begin original |
792 | 1710 | |
793 | 1711 | the pattern explicitly mentions a code point that is above 255 (say by |
794 | 1712 | C<\x{100}>); or |
795 | 1713 | |
796 | 1714 | =end original |
797 | 1715 | |
798 | 1716 | パターンが、(C<\x{100}> のような形で)255 を超える符号位置に明示的に |
799 | 1717 | 言及している; または |
800 | 1718 | |
801 | 1719 | =item 4 |
802 | 1720 | |
803 | 1721 | =begin original |
804 | 1722 | |
805 | 1723 | the pattern uses a Unicode name (C<\N{...}>); or |
806 | 1724 | |
807 | 1725 | =end original |
808 | 1726 | |
809 | 1727 | パターンが Unicode 名 (C<\N{...}>) を使っている; または |
810 | 1728 | |
811 | 1729 | =item 5 |
812 | 1730 | |
813 | 1731 | =begin original |
814 | 1732 | |
815 | the pattern uses a Unicode property (C<\p{...}>) | |
1733 | the pattern uses a Unicode property (C<\p{...}> or C<\P{...}>); or | |
816 | 1734 | |
817 | 1735 | =end original |
818 | 1736 | |
819 | パターンが Unicode 特性 (C<\p{...}>) を使っている | |
1737 | パターンが Unicode 特性 (C<\p{...}>) や C<\P{...}>) を使っている; または | |
820 | 1738 | |
1739 | =item 6 | |
1740 | ||
1741 | =begin original | |
1742 | ||
1743 | the pattern uses a Unicode break (C<\b{...}> or C<\B{...}>); or | |
1744 | ||
1745 | =end original | |
1746 | ||
1747 | パターンが Unicode 単語境界 (C<\b{...}> または C<\B{...}>) を使っている; | |
1748 | または | |
1749 | ||
1750 | =item 7 | |
1751 | ||
1752 | =begin original | |
1753 | ||
1754 | the pattern uses L</C<(?[ ])>> | |
1755 | ||
1756 | =end original | |
1757 | ||
1758 | パターンが L</C<(?[ ])>> を使っている | |
1759 | ||
1760 | =item 8 | |
1761 | ||
1762 | =begin original | |
1763 | ||
1764 | the pattern uses L<C<(*script_run: ...)>|/Script Runs> | |
1765 | ||
1766 | =end original | |
1767 | ||
1768 | パターンが L<C<(*script_run: ...)>|/Script Runs> を使っている | |
1769 | ||
821 | 1770 | =back |
822 | 1771 | |
823 | 1772 | =begin original |
824 | 1773 | |
825 | 1774 | Another mnemonic for this modifier is "Depends", as the rules actually |
826 | 1775 | used depend on various things, and as a result you can get unexpected |
827 | 1776 | results. See L<perlunicode/The "Unicode Bug">. The Unicode Bug has |
828 | become rather infamous, leading to yet another ( | |
1777 | become rather infamous, leading to yet another (without swearing) name | |
829 | modifier, "Dodgy". | |
1778 | for this modifier, "Dodgy". | |
830 | 1779 | |
831 | 1780 | =end original |
832 | 1781 | |
833 | 1782 | この修飾子のもう一つの記憶法は「依存」(Depends)です; 規則は実際には |
834 | 1783 | 様々なことに依存していること、また結果として予想外の |
835 | 1784 | 結果になるかもしれないからです。 |
836 | 1785 | L<perlunicode/The "Unicode Bug"> を参照してください。 |
837 | Unicode バグは、悪名高くなり、この修飾子のもう一つの( | |
1786 | Unicode バグは、悪名高くなり、この修飾子のもう一つの(罵りのない) | |
838 | 1787 | 名前 "Dodgy" を引き起こしています。 |
839 | 1788 | |
840 | 1789 | =begin original |
841 | 1790 | |
842 | ||
1791 | Unless the pattern or string are encoded in UTF-8, only ASCII characters | |
843 | ||
1792 | can match positively. | |
844 | 1793 | |
845 | 1794 | =end original |
846 | 1795 | |
847 | ||
1796 | パターンや文字列が UTF-8 でエンコードされていない限り、 | |
848 | ||
1797 | ASCII 文字のみが肯定的にマッチングします。 | |
849 | 1798 | |
850 | 1799 | =begin original |
851 | 1800 | |
852 | 1801 | Here are some examples of how that works on an ASCII platform: |
853 | 1802 | |
854 | 1803 | =end original |
855 | 1804 | |
856 | 1805 | 以下は ASCII プラットフォームでどのように動作するかの例です: |
857 | 1806 | |
858 | 1807 | $str = "\xDF"; # $str is not in UTF-8 format. |
859 | 1808 | $str =~ /^\w/; # No match, as $str isn't in UTF-8 format. |
860 | 1809 | $str .= "\x{0e0b}"; # Now $str is in UTF-8 format. |
861 | 1810 | $str =~ /^\w/; # Match! $str is now in UTF-8 format. |
862 | 1811 | chop $str; |
863 | 1812 | $str =~ /^\w/; # Still a match! $str remains in UTF-8 format. |
864 | 1813 | |
865 | 1814 | =begin original |
866 | 1815 | |
867 | 1816 | This modifier is automatically selected by default when none of the |
868 | 1817 | others are, so yet another name for it is "Default". |
869 | 1818 | |
870 | 1819 | =end original |
871 | 1820 | |
872 | 1821 | この修飾子は他のものが指定されなかった場合にデフォルトとして自動的に |
873 | 1822 | 洗濯されるので、これのもう一つの名前は "Default" です。 |
874 | 1823 | |
875 | 1824 | =begin original |
876 | 1825 | |
877 | 1826 | Because of the unexpected behaviors associated with this modifier, you |
878 | probably should only use it to maintain weird backward | |
1827 | probably should only explicitly use it to maintain weird backward | |
1828 | compatibilities. | |
879 | 1829 | |
880 | 1830 | =end original |
881 | 1831 | |
882 | 1832 | この修飾子に関する想定外の振る舞いにより、おそらくおかしな後方互換性を |
883 | 維持するためだけにこれを使うべきでしょう。 | |
1833 | 維持するためだけにこれを明示的に使うべきでしょう。 | |
884 | 1834 | |
885 | 1835 | =head4 /a (and /aa) |
886 | 1836 | |
887 | 1837 | (/a (と /aa)) |
888 | 1838 | |
889 | 1839 | =begin original |
890 | 1840 | |
891 | This modifier stands for ASCII-restrict (or ASCII-safe). This modifier | |
1841 | This modifier stands for ASCII-restrict (or ASCII-safe). This modifier | |
892 | ||
1842 | may be doubled-up to increase its effect. | |
893 | 1843 | |
894 | 1844 | =end original |
895 | 1845 | |
896 | 1846 | この修飾子は ASCII 制限 (あるいは ASCII セーフ) を意味します。 |
897 | この修飾子は、 | |
1847 | この修飾子は、2 倍にすることで効果が増します。 | |
898 | 1848 | |
899 | 1849 | =begin original |
900 | 1850 | |
901 | 1851 | When it appears singly, it causes the sequences C<\d>, C<\s>, C<\w>, and |
902 | 1852 | the Posix character classes to match only in the ASCII range. They thus |
903 | 1853 | revert to their pre-5.6, pre-Unicode meanings. Under C</a>, C<\d> |
904 | 1854 | always means precisely the digits C<"0"> to C<"9">; C<\s> means the five |
905 | characters C<[ \f\n\r\t]> | |
1855 | characters C<[ \f\n\r\t]>, and starting in Perl v5.18, the vertical tab; | |
1856 | C<\w> means the 63 characters | |
906 | 1857 | C<[A-Za-z0-9_]>; and likewise, all the Posix classes such as |
907 | 1858 | C<[[:print:]]> match only the appropriate ASCII-range characters. |
908 | 1859 | |
909 | 1860 | =end original |
910 | 1861 | |
911 | 1862 | これが単体で使われると、C<\d>, C<\s>, C<\w>, Posix 文字クラスは |
912 | 1863 | ASCII の範囲のみにマッチングするようになります。 |
913 | 1864 | 従って、これらは 5.6 以前の、Unicode 以前の意味に戻します。 |
914 | 1865 | C</a> の基では、C<\d> は常に正確に数字 C<"0"> から C<"9"> を意味します; |
915 | C<\s> は C<[ \f\n\r\t]> の 5 文字 | |
1866 | C<\s> は C<[ \f\n\r\t]> の 5 文字、および Perl v5.18 から垂直タブ、 | |
1867 | を意味します; | |
916 | 1868 | C<\w> は C<[A-Za-z0-9_]> の 63 文字を意味します; |
917 | 1869 | 同様に、C<[[:print:]]> のような全ての Posix クラスは |
918 | 1870 | 適切な ASCII の範囲の文字にのみマッチングします。 |
919 | 1871 | |
920 | 1872 | =begin original |
921 | 1873 | |
922 | 1874 | This modifier is useful for people who only incidentally use Unicode, |
923 | 1875 | and who do not wish to be burdened with its complexities and security |
924 | 1876 | concerns. |
925 | 1877 | |
926 | 1878 | =end original |
927 | 1879 | |
928 | 1880 | この修飾子は、偶然 Unicode を使っている人々で、その複雑さと |
929 | 1881 | セキュリティの問題に関する重荷を背負いたくない人々にとっては有用です。 |
930 | 1882 | |
931 | 1883 | =begin original |
932 | 1884 | |
933 | 1885 | With C</a>, one can write C<\d> with confidence that it will only match |
934 | 1886 | ASCII characters, and should the need arise to match beyond ASCII, you |
935 | 1887 | can instead use C<\p{Digit}> (or C<\p{Word}> for C<\w>). There are |
936 | 1888 | similar C<\p{...}> constructs that can match beyond ASCII both white |
937 | 1889 | space (see L<perlrecharclass/Whitespace>), and Posix classes (see |
938 | 1890 | L<perlrecharclass/POSIX Character Classes>). Thus, this modifier |
939 | 1891 | doesn't mean you can't use Unicode, it means that to get Unicode |
940 | 1892 | matching you must explicitly use a construct (C<\p{}>, C<\P{}>) that |
941 | 1893 | signals Unicode. |
942 | 1894 | |
943 | 1895 | =end original |
944 | 1896 | |
945 | 1897 | C</a> を使うと、ASCII 文字だけにマッチングすることに自信を持って |
946 | 1898 | 書くことができ、ASCII を超えてマッチングする必要が発生したときには、 |
947 | 1899 | 代わりに C<\p{Digit}> (または C<\w> として C<\p{Word}>) が使えます。 |
948 | 1900 | ASCII を超えたスペース (L<perlrecharclass/Whitespace> を参照してください) |
949 | 1901 | と Posix クラス (L<perlrecharclass/POSIX Character Classes> を |
950 | 1902 | 参照してください) の両方にマッチングする似たような C<\p{...}> 構文があります。 |
951 | 1903 | 従って、この修飾子は Unicode が使えなくなるということではなく、 |
952 | 1904 | Unicode のマッチングには明示的に Unicode を意味する構文 |
953 | 1905 | (C<\p{}>, C<\P{}>) を使わないといけないということです。 |
954 | 1906 | |
955 | 1907 | =begin original |
956 | 1908 | |
957 | 1909 | As you would expect, this modifier causes, for example, C<\D> to mean |
958 | 1910 | the same thing as C<[^0-9]>; in fact, all non-ASCII characters match |
959 | 1911 | C<\D>, C<\S>, and C<\W>. C<\b> still means to match at the boundary |
960 | 1912 | between C<\w> and C<\W>, using the C</a> definitions of them (similarly |
961 | 1913 | for C<\B>). |
962 | 1914 | |
963 | 1915 | =end original |
964 | 1916 | |
965 | 1917 | 予想できるとおり、 この修飾子は、例えば、C<\D> を C<[^0-9]> と |
966 | 1918 | 同じことにします; |
967 | 1919 | 実際、全ての非 ASCII 文字は C<\D>, C<\S>, C<\W> にマッチングします。 |
968 | 1920 | C<\b> はまだ C<\w> と C<\W> の境界にマッチングします; |
969 | 1921 | これらのために (C<\B> と同様) C</a> の定義を使います。 |
970 | 1922 | |
971 | 1923 | =begin original |
972 | 1924 | |
973 | 1925 | Otherwise, C</a> behaves like the C</u> modifier, in that |
974 | case-insensitive matching uses Unicode | |
1926 | case-insensitive matching uses Unicode rules; for example, "k" will | |
975 | 1927 | match the Unicode C<\N{KELVIN SIGN}> under C</i> matching, and code |
976 | 1928 | points in the Latin1 range, above ASCII will have Unicode rules when it |
977 | 1929 | comes to case-insensitive matching. |
978 | 1930 | |
979 | 1931 | =end original |
980 | 1932 | |
981 | 1933 | さもなければ、C</a> は C</u> 修飾子のように振る舞います; |
982 | 大文字小文字を無視したマッチングには Unicode の | |
1934 | 大文字小文字を無視したマッチングには Unicode の規則を使います; | |
983 | 1935 | 例えば、"k" は C</i> の基では C<\N{KELVIN SIGN}> にマッチングし、 |
984 | 1936 | ASCII の範囲を超える Latin1 の範囲の符号位置は、大文字小文字を無視した |
985 | 1937 | マッチングで使われる場合は Unicode の規則を使います。 |
986 | 1938 | |
987 | 1939 | =begin original |
988 | 1940 | |
989 | 1941 | 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 | |
1942 | 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 | |
1943 | occurrence of C<"a"> restricts the C<\d>, I<etc>., and the second occurrence | |
992 | 1944 | adds the C</i> restrictions.) But, note that code points outside the |
993 | 1945 | ASCII range will use Unicode rules for C</i> matching, so the modifier |
994 | 1946 | doesn't really restrict things to just ASCII; it just forbids the |
995 | 1947 | intermixing of ASCII and non-ASCII. |
996 | 1948 | |
997 | 1949 | =end original |
998 | 1950 | |
999 | 1951 | ("k" と C<\N{KELVIN SIGN}> のような) ASCII/非-ASCII マッチングを禁止するには、 |
1000 | C</aai> や C</aia> のように "a" を 2 回指定します。 | |
1952 | C</aai> や C</aia> のように C<"a"> を 2 回指定します。 | |
1001 | (最初の "a" は C<\d> などを制限し、2 番目は C</i> の制限を追加します。) | |
1953 | (最初の C<"a"> は C<\d> などを制限し、2 番目は C</i> の制限を追加します。) | |
1002 | 1954 | しかし、 ASCII の範囲外の符号位置は C</i> マッチングに Unicode 規則を |
1003 | 1955 | 使うので、この修飾子は実際には単に ASCII に制限するものではないことに |
1004 | 1956 | 注意してください; これは単に ASCII と非 ASCII を混ぜることを禁止します。 |
1005 | 1957 | |
1006 | 1958 | =begin original |
1007 | 1959 | |
1008 | 1960 | To summarize, this modifier provides protection for applications that |
1009 | 1961 | don't wish to be exposed to all of Unicode. Specifying it twice |
1010 | 1962 | gives added protection. |
1011 | 1963 | |
1012 | 1964 | =end original |
1013 | 1965 | |
1014 | 1966 | まとめると、この修飾子は全ての Unicode に対して曝されることを望んでいない |
1015 | 1967 | アプリケーションに対する保護を提供します。 |
1016 | 1968 | 2 回指定することで追加の保護を提供します。 |
1017 | 1969 | |
1018 | 1970 | =begin original |
1019 | 1971 | |
1020 | 1972 | This modifier may be specified to be the default by C<use re '/a'> |
1021 | 1973 | 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 | |
1974 | the C</u> modifier explicitly if there are a few regular expressions | |
1023 | 1975 | where you do want full Unicode rules (but even here, it's best if |
1024 | 1976 | everything were under feature C<"unicode_strings">, along with the |
1025 | 1977 | C<use re '/aa'>). Also see L</Which character set modifier is in |
1026 | 1978 | effect?>. |
1027 | 1979 | X</a> |
1028 | 1980 | X</aa> |
1029 | 1981 | |
1030 | 1982 | =end original |
1031 | 1983 | |
1032 | 1984 | この修飾子は C<use re '/a'> または C<use re '/aa'> でデフォルトに |
1033 | 1985 | 設定されます。 |
1034 | 1986 | そうすると、もし完全な Unicode 規則を使いたい正規表現がある場合は、 |
1035 | 1987 | C</u> 修飾子を明示的に使う機会があるかもしれません |
1036 | 1988 | (その場合でも、全てが C<"unicode_strings"> の基なら、 |
1037 | 1989 | C<use re '/aa'> と共にするのが最良です)。 |
1038 | 1990 | L</Which character set modifier is in effect?> も参照してください。 |
1039 | 1991 | X</a> |
1040 | 1992 | X</aa> |
1041 | 1993 | |
1042 | 1994 | =head4 Which character set modifier is in effect? |
1043 | 1995 | |
1044 | 1996 | (どの文字集合修飾子が有効?) |
1045 | 1997 | |
1046 | 1998 | =begin original |
1047 | 1999 | |
1048 | 2000 | Which of these modifiers is in effect at any given point in a regular |
1049 | 2001 | expression depends on a fairly complex set of interactions. These have |
1050 | 2002 | been designed so that in general you don't have to worry about it, but |
1051 | 2003 | this section gives the gory details. As |
1052 | 2004 | explained below in L</Extended Patterns> it is possible to explicitly |
1053 | 2005 | specify modifiers that apply only to portions of a regular expression. |
1054 | 2006 | The innermost always has priority over any outer ones, and one applying |
1055 | 2007 | to the whole expression has priority over any of the default settings that are |
1056 | 2008 | described in the remainder of this section. |
1057 | 2009 | |
1058 | 2010 | =end original |
1059 | 2011 | |
1060 | 2012 | ある正規表現のあるポイントでどの修飾子が有効かは、かなり複雑な相互作用に |
1061 | 2013 | 依存します。 |
1062 | 2014 | これらは、基本的にはあなたがこれらについて心配しなくて良いように |
1063 | 2015 | 設計されています。 |
1064 | 2016 | しかし、この節は詳細を記述します。 |
1065 | 2017 | L</Extended Patterns> で後述するとおり、正規表現の一部にだけ |
1066 | 2018 | 適用する修飾子を明示的に指定することが可能です。 |
1067 | 2019 | 一番内側のものは常により外側のものより優先され、式全体に適用されるものは |
1068 | 2020 | この節の残りで記述されるデフォルト設定より優先されます。 |
1069 | 2021 | |
1070 | 2022 | =begin original |
1071 | 2023 | |
1072 | 2024 | The C<L<use re 'E<sol>foo'|re/"'/flags' mode">> pragma can be used to set |
1073 | 2025 | default modifiers (including these) for regular expressions compiled |
1074 | 2026 | within its scope. This pragma has precedence over the other pragmas |
1075 | 2027 | listed below that also change the defaults. |
1076 | 2028 | |
1077 | 2029 | =end original |
1078 | 2030 | |
1079 | 2031 | C<L<use re 'E<sol>foo'|re/"'/flags' mode">> プラグマは、 |
1080 | 2032 | このスコープ内でコンパイルされる正規表現に対して(これらを含む) |
1081 | 2033 | デフォルトの修飾子を設定するのに使えます。 |
1082 | 2034 | このプラグマは、デフォルトを変更する後述するその他のプラグマに優先します。 |
1083 | 2035 | |
1084 | 2036 | =begin original |
1085 | 2037 | |
1086 | 2038 | Otherwise, C<L<use locale|perllocale>> sets the default modifier to C</l>; |
1087 | 2039 | and C<L<use feature 'unicode_strings|feature>>, or |
1088 | 2040 | C<L<use 5.012|perlfunc/use VERSION>> (or higher) set the default to |
1089 | 2041 | C</u> when not in the same scope as either C<L<use locale|perllocale>> |
1090 | 2042 | or C<L<use bytes|bytes>>. |
1091 | 2043 | (C<L<use locale ':not_characters'|perllocale/Unicode and UTF-8>> also |
1092 | 2044 | sets the default to C</u>, overriding any plain C<use locale>.) |
1093 | 2045 | Unlike the mechanisms mentioned above, these |
1094 | 2046 | affect operations besides regular expressions pattern matching, and so |
1095 | 2047 | give more consistent results with other operators, including using |
1096 | C<\U>, C<\l>, etc. in substitution replacements. | |
2048 | C<\U>, C<\l>, I<etc>. in substitution replacements. | |
1097 | 2049 | |
1098 | 2050 | =end original |
1099 | 2051 | |
1100 | 2052 | さもなければ、C<L<use locale|perllocale>> はデフォルト修飾子を C</l> に |
1101 | 2053 | 設定します; そして、C<L<use feature 'unicode_strings|feature>> か |
1102 | 2054 | C<L<use 5.012|perlfunc/use VERSION>> (またはそれ以上) は、 |
1103 | 2055 | 同じスコープに C<L<use locale|perllocale>> や C<L<use bytes|bytes>> が |
1104 | 2056 | なければ、デフォルトを C</u> に設定します。 |
1105 | 2057 | (C<L<use locale ':not_characters'|perllocale/Unicode and UTF-8>> はまた |
1106 | 2058 | デフォルトを C</u> に設定し、普通の C<use locale> を上書きします。) |
1107 | 2059 | 前述した機構と異なり、これらは正規表現パターンマッチング以外の操作に |
1108 | 2060 | 影響するので、置換での C<\U>, C<\l> を使うことを含むその他の操作と |
1109 | 2061 | より一貫性のある結果になります。 |
1110 | 2062 | |
1111 | 2063 | =begin original |
1112 | 2064 | |
1113 | 2065 | If none of the above apply, for backwards compatibility reasons, the |
1114 | 2066 | C</d> modifier is the one in effect by default. As this can lead to |
1115 | 2067 | unexpected results, it is best to specify which other rule set should be |
1116 | 2068 | used. |
1117 | 2069 | |
1118 | 2070 | =end original |
1119 | 2071 | |
1120 | 2072 | 前述のどれも適用されない場合、後方互換性のために、 |
1121 | 2073 | C</d> 修飾子がデフォルトで有効になります。 |
1122 | 2074 | これは想定外の結果になることがあるので、 |
1123 | 2075 | その他の規則集合が使われるように指定するのが最良です。 |
1124 | 2076 | |
1125 | 2077 | =head4 Character set modifier behavior prior to Perl 5.14 |
1126 | 2078 | |
1127 | 2079 | (Perl 5.14 より前の文字集合修飾子の振る舞い) |
1128 | 2080 | |
1129 | 2081 | =begin original |
1130 | 2082 | |
1131 | 2083 | Prior to 5.14, there were no explicit modifiers, but C</l> was implied |
1132 | 2084 | for regexes compiled within the scope of C<use locale>, and C</d> was |
1133 | 2085 | implied otherwise. However, interpolating a regex into a larger regex |
1134 | 2086 | would ignore the original compilation in favor of whatever was in effect |
1135 | 2087 | at the time of the second compilation. There were a number of |
1136 | 2088 | inconsistencies (bugs) with the C</d> modifier, where Unicode rules |
1137 | 2089 | would be used when inappropriate, and vice versa. C<\p{}> did not imply |
1138 | 2090 | Unicode rules, and neither did all occurrences of C<\N{}>, until 5.12. |
1139 | 2091 | |
1140 | 2092 | =end original |
1141 | 2093 | |
1142 | 2094 | 5.14 より前では、明示的な修飾子はありませんが、 |
1143 | 2095 | C<use locale> のスコープ内でコンパイルされた正規表現に関しては |
1144 | 2096 | C</l> が仮定され、さもなければ C</d> が仮定されます。 |
1145 | 2097 | しかし、ある正規表現をより大きな正規表現に展開した場合、元のコンパイル時の |
1146 | 2098 | 状況は、2 回目のコンパイル時点で有効なもので上書きされます。 |
1147 | 2099 | C</d> 演算子には、不適切なときに Unicode 規則が使われる、あるいはその逆の |
1148 | 2100 | 多くの非一貫性(バグ)があります。 |
1149 | 2101 | C<\p{}> および C<\N{}> は 5.12 まで Unicode 規則を仮定していません。 |
1150 | 2102 | |
1151 | 2103 | =head2 Regular Expressions |
1152 | 2104 | |
1153 | 2105 | (正規表現) |
1154 | 2106 | |
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 | 2107 | =head3 Quantifiers |
1253 | 2108 | |
1254 | 2109 | (量指定子) |
1255 | 2110 | |
1256 | 2111 | =begin original |
1257 | 2112 | |
1258 | ||
2113 | Quantifiers are used when a particular portion of a pattern needs to | |
2114 | match a certain number (or numbers) of times. If there isn't a | |
2115 | quantifier the number of times to match is exactly one. The following | |
2116 | standard quantifiers are recognized: | |
1259 | 2117 | X<metacharacter> X<quantifier> X<*> X<+> X<?> X<{n}> X<{n,}> X<{n,m}> |
1260 | 2118 | |
1261 | 2119 | =end original |
1262 | 2120 | |
2121 | Quantifiers are used when a particular portion of a pattern needs to | |
2122 | match a certain number (or numbers) of times. If there isn't a | |
2123 | quantifier the number of times to match is exactly one. | |
1263 | 2124 | 以下の標準的な量指定子を使えます: |
1264 | 2125 | X<metacharacter> X<quantifier> X<*> X<+> X<?> X<{n}> X<{n,}> X<{n,m}> |
1265 | 2126 | |
1266 | 2127 | =begin original |
1267 | 2128 | |
1268 | 2129 | * Match 0 or more times |
1269 | 2130 | + Match 1 or more times |
1270 | 2131 | ? Match 1 or 0 times |
1271 | 2132 | {n} Match exactly n times |
1272 | 2133 | {n,} Match at least n times |
1273 | 2134 | {n,m} Match at least n but not more than m times |
1274 | 2135 | |
1275 | 2136 | =end original |
1276 | 2137 | |
1277 | 2138 | * 0 回以上マッチング |
1278 | 2139 | + 1 回以上マッチング |
1279 | 2140 | ? 0 回または 1 回マッチング |
1280 | 2141 | {n} 正確に n 回マッチング |
1281 | 2142 | {n,} 最低 n 回マッチング |
1282 | 2143 | {n,m} n 回以上 m 回以下マッチング |
1283 | 2144 | |
1284 | 2145 | =begin original |
1285 | 2146 | |
1286 | (If a curly bracket occurs in a | |
2147 | (If a non-escaped curly bracket occurs in a context other than one of | |
1287 | ||
2148 | the quantifiers listed above, where it does not form part of a | |
1288 | as | |
2149 | backslashed sequence like C<\x{...}>, it is either a fatal syntax error, | |
1289 | ||
2150 | or treated as a regular character, generally with a deprecation warning | |
1290 | ||
2151 | raised. To escape it, you can precede it with a backslash (C<"\{">) or | |
1291 | ||
2152 | enclose it within square brackets (C<"[{]">). | |
1292 | ||
2153 | This change will allow for future syntax extensions (like making the | |
1293 | or | |
2154 | lower bound of a quantifier optional), and better error checking of | |
1294 | ||
2155 | 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 | 2156 | |
1298 | ||
1299 | 2157 | =end original |
1300 | 2158 | |
1301 | ( | |
2159 | (前述した量指定子の一つ以外のコンテキストでエスケープされない中かっこが | |
1302 | ||
2160 | 使われて、C<\x{...}> のような逆スラッシュ付き並びの一部ではないときには、 | |
1303 | ||
2161 | 普通の文字として扱われるか、致命的エラーになり、どちらでも | |
1304 | ||
2162 | 一般的には廃止予定警告が発生します。 | |
1305 | ||
2163 | これをエスケープするには、逆スラッシュを前置したり (C<"\{">) | |
1306 | ||
2164 | 大かっこで囲んだり (C<"[{]">) できます。 | |
1307 | 大かっこで囲む (C<"\{"> または C<"[{]">) ことでエスケープすることが | |
1308 | 要求されるようになる予定です。 | |
1309 | 2165 | この変更により、(量指定子の加減をオプションにするような) 将来の |
1310 | 文法の拡張ができるようになり、量指定子 | |
2166 | 文法の拡張ができるようになり、量指定子のより良いエラーチェックが | |
1311 | 2167 | できるようになります。 |
1312 | 現在のところ、量指定子のタイプミスは警告なしにリテラルな文字の並びとして | |
1313 | 扱われます。 | |
1314 | 例えば: | |
1315 | 2168 | |
1316 | /o{4,3}/ | |
1317 | ||
1318 | 2169 | =begin original |
1319 | 2170 | |
1320 | ||
2171 | The C<"*"> quantifier is equivalent to C<{0,}>, the C<"+"> | |
1321 | ||
2172 | 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 | 2173 | to non-negative integral values less than a preset limit defined when perl is built. |
1335 | 2174 | This is usually 32766 on the most common platforms. The actual limit can |
1336 | 2175 | be seen in the error message generated by code such as this: |
1337 | 2176 | |
1338 | 2177 | =end original |
1339 | 2178 | |
1340 | "*" 量指定子は C<{0,}> と、"+" 量指定子は C<{1,}> と、 | |
2179 | C<"*"> 量指定子は C<{0,}> と、C<"+"> 量指定子は C<{1,}> と、 | |
1341 | "?" 量指定子は C<{0,1}> と等価です。 | |
2180 | C<"?"> 量指定子は C<{0,1}> と等価です。 | |
1342 | n | |
2181 | I<n> と I<m> は perl をビルドしたときに定義した既定の制限より小さな | |
1343 | 制限されます。 | |
2182 | 非負整数回に制限されます。 | |
1344 | 2183 | これは大抵のプラットフォームでは 32766 回になっています。 |
1345 | 2184 | 実際の制限は次のようなコードを実行すると生成されるエラーメッセージで |
1346 | 2185 | 見ることができます: |
1347 | 2186 | |
1348 | 2187 | $_ **= $_ , / {$_} / for 2 .. 42; |
1349 | 2188 | |
1350 | 2189 | =begin original |
1351 | 2190 | |
1352 | 2191 | By default, a quantified subpattern is "greedy", that is, it will match as |
1353 | 2192 | many times as possible (given a particular starting location) while still |
1354 | 2193 | 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 | |
2194 | minimum number of times possible, follow the quantifier with a C<"?">. Note | |
1356 | 2195 | that the meanings don't change, just the "greediness": |
1357 | 2196 | X<metacharacter> X<greedy> X<greediness> |
1358 | 2197 | X<?> X<*?> X<+?> X<??> X<{n}?> X<{n,}?> X<{n,m}?> |
1359 | 2198 | |
1360 | 2199 | =end original |
1361 | 2200 | |
1362 | 2201 | デフォルトでは、パターンで行われる量指定は「貪欲」です; |
1363 | 2202 | つまりそれはパターンの残りの部分が可能な範囲で、 |
1364 | 2203 | (始めた地点から)可能な限り多くを先にあるパターンでマッチングさせます。 |
1365 | 2204 | もし最小回数でのマッチングを行いたいのであれば、量指定子の後ろに |
1366 | "?" を続けます。 | |
2205 | C<"?"> を続けます。 | |
1367 | 2206 | 意味は変更されずに「貪欲さ」だけを変更できます: |
1368 | 2207 | X<metacharacter> X<greedy> X<greediness> |
1369 | 2208 | X<?> X<*?> X<+?> X<??> X<{n}?> X<{n,}?> X<{n,m}?> |
1370 | 2209 | |
1371 | 2210 | =begin original |
1372 | 2211 | |
1373 | 2212 | *? Match 0 or more times, not greedily |
1374 | 2213 | +? Match 1 or more times, not greedily |
1375 | 2214 | ?? Match 0 or 1 time, not greedily |
1376 | 2215 | {n}? Match exactly n times, not greedily (redundant) |
1377 | 2216 | {n,}? Match at least n times, not greedily |
1378 | 2217 | {n,m}? Match at least n but not more than m times, not greedily |
1379 | 2218 | |
1380 | 2219 | =end original |
1381 | 2220 | |
1382 | 2221 | *? 0 回以上の貪欲でないマッチング |
1383 | 2222 | +? 1 回以上の貪欲でないマッチング |
1384 | 2223 | ?? 0 回または 1 回の貪欲でないマッチング |
1385 | 2224 | {n}? ちょうど n 回の貪欲でないマッチング (冗長) |
1386 | 2225 | {n,}? n 回以上の貪欲でないマッチング |
1387 | 2226 | {n,m}? n 回以上 m 回以下の貪欲でないマッチング |
1388 | 2227 | |
1389 | 2228 | =begin original |
1390 | 2229 | |
1391 | ||
2230 | Normally when a quantified subpattern does not allow the rest of the | |
1392 | 2231 | overall pattern to match, Perl will backtrack. However, this behaviour is |
1393 | 2232 | sometimes undesirable. Thus Perl provides the "possessive" quantifier form |
1394 | 2233 | as well. |
1395 | 2234 | |
1396 | 2235 | =end original |
1397 | 2236 | |
1398 | ||
2237 | 通常、パターンのうちの量指定された一部によってパターン全体が | |
1399 | 2238 | マッチングに失敗したとき、Perl はバックトラックを行います。 |
1400 | 2239 | しかしこの振る舞いは望まれないこともあります。 |
1401 | 2240 | そのため、Perl は「絶対最大量(possessive)」量指定形式も提供しています。 |
1402 | 2241 | |
1403 | 2242 | =begin original |
1404 | 2243 | |
1405 | 2244 | *+ Match 0 or more times and give nothing back |
1406 | 2245 | ++ Match 1 or more times and give nothing back |
1407 | 2246 | ?+ Match 0 or 1 time and give nothing back |
1408 | 2247 | {n}+ Match exactly n times and give nothing back (redundant) |
1409 | 2248 | {n,}+ Match at least n times and give nothing back |
1410 | 2249 | {n,m}+ Match at least n but not more than m times and give nothing back |
1411 | 2250 | |
1412 | 2251 | =end original |
1413 | 2252 | |
1414 | 2253 | *+ 0 回以上マッチングして何も返さない |
1415 | 2254 | ++ 1 回以上マッチングして何も返さない |
1416 | 2255 | ?+ 0 回または 1 回マッチングして何も返さない |
1417 | 2256 | {n}+ ちょうど n 回のマッチングして何も返さない (冗長) |
1418 | 2257 | {n,}+ n 回以上のマッチングして何も返さない |
1419 | 2258 | {n,m}+ n 回以上 m 回以下マッチングして何も返さない |
1420 | 2259 | |
1421 | 2260 | =begin original |
1422 | 2261 | |
1423 | 2262 | For instance, |
1424 | 2263 | |
1425 | 2264 | =end original |
1426 | 2265 | |
1427 | 2266 | 例えば、 |
1428 | 2267 | |
1429 | 2268 | 'aaaa' =~ /a++a/ |
1430 | 2269 | |
1431 | 2270 | =begin original |
1432 | 2271 | |
1433 | will never match, as the C<a++> will gobble up all the C<a>'s in the | |
2272 | will never match, as the C<a++> will gobble up all the C<"a">'s in the | |
1434 | 2273 | string and won't leave any for the remaining part of the pattern. This |
1435 | 2274 | feature can be extremely useful to give perl hints about where it |
1436 | 2275 | shouldn't backtrack. For instance, the typical "match a double-quoted |
1437 | 2276 | string" problem can be most efficiently performed when written as: |
1438 | 2277 | |
1439 | 2278 | =end original |
1440 | 2279 | |
1441 | は、C<a++> が文字列中の全ての C<a> を飲み込んでしまい、 | |
2280 | は、C<a++> が文字列中の全ての C<"a"> を飲み込んでしまい、 | |
1442 | 2281 | 後に何も残さないためマッチングしません。 |
1443 | 2282 | この機能はバックトラックするべきでない場所のヒントを perl に |
1444 | 2283 | 与えるのに非常に便利です。 |
1445 | 2284 | 例えば、典型的な「ダブルクォート文字列のマッチング」問題で次のように |
1446 | 2285 | 書くととても効率的になります: |
1447 | 2286 | |
1448 | 2287 | /"(?:[^"\\]++|\\.)*+"/ |
1449 | 2288 | |
1450 | 2289 | =begin original |
1451 | 2290 | |
1452 | 2291 | as we know that if the final quote does not match, backtracking will not |
1453 | 2292 | help. See the independent subexpression |
1454 | L</C<< (?>pattern) >>> for more details; | |
2293 | L</C<< (?>I<pattern>) >>> for more details; | |
1455 | 2294 | possessive quantifiers are just syntactic sugar for that construct. For |
1456 | 2295 | instance the above example could also be written as follows: |
1457 | 2296 | |
1458 | 2297 | =end original |
1459 | 2298 | |
1460 | 2299 | 見ての通り最後のクォートがマッチングしなかったとき、バックトラックは |
1461 | 2300 | 役に立ちません。 |
1462 | 詳細は独立したサブパターン L</C<< (?>pattern) >>> を参照してください; | |
2301 | 詳細は独立したサブパターン L</C<< (?>I<pattern>) >>> を参照してください; | |
1463 | 2302 | 絶対最大量指定子はまさにその構文糖です。 |
1464 | 2303 | 例えばこの例は次のようにも書けます: |
1465 | 2304 | |
1466 | 2305 | /"(?>(?:(?>[^"\\]+)|\\.)*)"/ |
1467 | 2306 | |
2307 | =begin original | |
2308 | ||
2309 | Note that the possessive quantifier modifier can not be combined | |
2310 | with the non-greedy modifier. This is because it would make no sense. | |
2311 | Consider the follow equivalency table: | |
2312 | ||
2313 | =end original | |
2314 | ||
2315 | 絶対最大量指定修飾子は非貪欲修飾子と結合できないことに注意してください。 | |
2316 | これは無意味だからです。 | |
2317 | 次の等価性表を考慮してください: | |
2318 | ||
2319 | Illegal Legal | |
2320 | ------------ ------ | |
2321 | X??+ X{0} | |
2322 | X+?+ X{1} | |
2323 | X{min,max}?+ X{min} | |
2324 | ||
1468 | 2325 | =head3 Escape sequences |
1469 | 2326 | |
1470 | 2327 | (エスケープシーケンス) |
1471 | 2328 | |
1472 | 2329 | =begin original |
1473 | 2330 | |
1474 | 2331 | Because patterns are processed as double-quoted strings, the following |
1475 | 2332 | also work: |
1476 | 2333 | |
1477 | 2334 | =end original |
1478 | 2335 | |
1479 | 2336 | パターンはダブルクォート文字列として処理されるため、 |
1480 | 2337 | 以下のエスケープ文字も動作します: |
1481 | 2338 | 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 | 2339 | X<\0> X<\c> X<\N{}> X<\x> |
1483 | 2340 | |
1484 | 2341 | =begin original |
1485 | 2342 | |
1486 | 2343 | \t tab (HT, TAB) |
1487 | 2344 | \n newline (LF, NL) |
1488 | 2345 | \r return (CR) |
1489 | 2346 | \f form feed (FF) |
1490 | 2347 | \a alarm (bell) (BEL) |
1491 | 2348 | \e escape (think troff) (ESC) |
1492 | 2349 | \cK control char (example: VT) |
1493 | 2350 | \x{}, \x00 character whose ordinal is the given hexadecimal number |
1494 | 2351 | \N{name} named Unicode character or character sequence |
1495 | 2352 | \N{U+263D} Unicode character (example: FIRST QUARTER MOON) |
1496 | 2353 | \o{}, \000 character whose ordinal is the given octal number |
1497 | 2354 | \l lowercase next char (think vi) |
1498 | 2355 | \u uppercase next char (think vi) |
1499 | \L lowercase ti | |
2356 | \L lowercase until \E (think vi) | |
1500 | \U uppercase ti | |
2357 | \U uppercase until \E (think vi) | |
1501 | \Q quote (disable) pattern metacharacters ti | |
2358 | \Q quote (disable) pattern metacharacters until \E | |
1502 | 2359 | \E end either case modification or quoted section, think vi |
1503 | 2360 | |
1504 | 2361 | =end original |
1505 | 2362 | |
1506 | 2363 | \t タブ (水平タブ;HT、TAB) |
1507 | 2364 | \n 改行 (LF、NL) |
1508 | 2365 | \r 復帰 (CR) |
1509 | 2366 | \f フォームフィード (FF) |
1510 | 2367 | \a アラーム (ベル) (BEL) |
1511 | 2368 | \e エスケープ (troff 的) (ESC) |
1512 | 2369 | \cK 制御文字 (例: VT) |
1513 | 2370 | \x{}, \x00 16 進数で番号指定された文字 |
1514 | 2371 | \N{name} 名前付きユニコード文字または文字並び |
1515 | 2372 | \N{U+263D} Unicode 文字 (例: FIRST QUARTER MOON) |
1516 | 2373 | \o{}, \000 8 進数で番号指定された文字 |
1517 | 2374 | \l 次の文字を小文字に (vi 的) |
1518 | 2375 | \u 次の文字を大文字に (vi 的) |
1519 | 2376 | \L \E まで小文字に (vi 的) |
1520 | 2377 | \U \E まで大文字に (vi 的) |
1521 | 2378 | \Q \E までパターンメタ文字の無効化(Quote) |
1522 | 2379 | \E 大文字小文字変更またはクォートの終端 (vi 的) |
1523 | 2380 | |
1524 | 2381 | =begin original |
1525 | 2382 | |
1526 | 2383 | Details are in L<perlop/Quote and Quote-like Operators>. |
1527 | 2384 | |
1528 | 2385 | =end original |
1529 | 2386 | |
1530 | 2387 | 詳細は L<perlop/Quote and Quote-like Operators> にあります。 |
1531 | 2388 | |
1532 | 2389 | =head3 Character Classes and other Special Escapes |
1533 | 2390 | |
1534 | 2391 | (文字クラス及び他の特殊なエスケープ) |
1535 | 2392 | |
1536 | 2393 | =begin original |
1537 | 2394 | |
1538 | 2395 | In addition, Perl defines the following: |
1539 | 2396 | X<\g> X<\k> X<\K> X<backreference> |
1540 | 2397 | |
1541 | 2398 | =end original |
1542 | 2399 | |
1543 | 2400 | さらに、Perl は以下のものを定義します: |
1544 | 2401 | X<\g> X<\k> X<\K> X<backreference> |
1545 | 2402 | |
1546 | 2403 | =begin original |
1547 | 2404 | |
1548 | 2405 | Sequence Note Description |
1549 | 2406 | [...] [1] Match a character according to the rules of the |
1550 | 2407 | bracketed character class defined by the "...". |
1551 | 2408 | Example: [a-z] matches "a" or "b" or "c" ... or "z" |
1552 | 2409 | [[:...:]] [2] Match a character according to the rules of the POSIX |
1553 | 2410 | character class "..." within the outer bracketed |
1554 | 2411 | character class. Example: [[:upper:]] matches any |
1555 | 2412 | uppercase character. |
2413 | (?[...]) [8] Extended bracketed character class | |
1556 | 2414 | \w [3] Match a "word" character (alphanumeric plus "_", plus |
1557 | 2415 | other connector punctuation chars plus Unicode |
1558 | 2416 | marks) |
1559 | 2417 | \W [3] Match a non-"word" character |
1560 | 2418 | \s [3] Match a whitespace character |
1561 | 2419 | \S [3] Match a non-whitespace character |
1562 | 2420 | \d [3] Match a decimal digit character |
1563 | 2421 | \D [3] Match a non-digit character |
1564 | 2422 | \pP [3] Match P, named property. Use \p{Prop} for longer names |
1565 | 2423 | \PP [3] Match non-P |
1566 | 2424 | \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 | 2425 | \1 [5] Backreference to a specific capture group or buffer. |
1573 | 2426 | '1' may actually be any positive integer. |
1574 | 2427 | \g1 [5] Backreference to a specific or previous group, |
1575 | 2428 | \g{-1} [5] The number may be negative indicating a relative |
1576 | 2429 | previous group and may optionally be wrapped in |
1577 | 2430 | curly brackets for safer parsing. |
1578 | 2431 | \g{name} [5] Named backreference |
1579 | 2432 | \k<name> [5] Named backreference |
1580 | 2433 | \K [6] Keep the stuff left of the \K, don't include it in $& |
1581 | \N [7] Any character but \n | |
2434 | \N [7] Any character but \n. Not affected by /s modifier | |
1582 | /s modifier | |
1583 | 2435 | \v [3] Vertical whitespace |
1584 | 2436 | \V [3] Not vertical whitespace |
1585 | 2437 | \h [3] Horizontal whitespace |
1586 | 2438 | \H [3] Not horizontal whitespace |
1587 | 2439 | \R [4] Linebreak |
1588 | 2440 | |
1589 | 2441 | =end original |
1590 | 2442 | |
1591 | 2443 | Sequence Note Description |
1592 | 2444 | [...] [1] "..." で定義された大かっこ文字クラスのルールに従う文字に |
1593 | 2445 | マッチング。 |
1594 | 2446 | 例: [a-z] は "a", "b", "c", ... "z" にマッチング。 |
1595 | 2447 | [[:...:]] [2] 外側の大かっこ文字クラスの内側の POSIX 文字クラスに |
1596 | 2448 | 従う文字にマッチング。 |
1597 | 2449 | 例: [[:upper:]] は任意の大文字にマッチング。 |
2450 | (?[...]) [8] 拡張大かっこ文字クラス | |
1598 | 2451 | \w [3] "単語" 文字にマッチング (英数字及び "_" に加えて、 |
1599 | 2452 | その他の接続句読点文字と Unicode マークにマッチング) |
1600 | 2453 | \W [3] 非"単語"文字にマッチング |
1601 | 2454 | \s [3] 空白文字にマッチング |
1602 | 2455 | \S [3] 非空白文字にマッチング |
1603 | 2456 | \d [3] 10 進数字にマッチング |
1604 | 2457 | \D [3] 非数字にマッチング |
1605 | 2458 | \pP [3] 名前属性 P にマッチング. 長い名前であれば \p{Prop} |
1606 | 2459 | \PP [3] P以外にマッチング |
1607 | 2460 | \X [4] Unicode 拡張書記素クラスタ("eXtended grapheme cluster")にマッチング |
1608 | \C より大きな UTF-8 文字の一部であっても、1つの C 言語の文字 (オクテット)にマッチング | |
1609 | 従って文字をUTF-8バイト列へと変換するので、壊れた | |
1610 | UTF-8 片となるかもしれません; 後読みは対応していません | |
1611 | 2461 | \1 [5] 指定した捕捉グループやバッファへの後方参照。 |
1612 | 2462 | '1' には正の整数を指定できます。 |
1613 | 2463 | \g1 [5] 指定したまたは前のグループへの後方参照 |
1614 | 2464 | \g{-1} [5] 数値は相対的に前のグループを示す負の値にもできます、また |
1615 | 2465 | 任意で安全にパースするために波かっこで括ることもできます |
1616 | 2466 | \g{name} [5] 名前指定の後方参照 |
1617 | 2467 | \k<name> [5] 名前指定の後方参照 |
1618 | 2468 | \K [6] \K の左にある物を保持、$& に含めない |
1619 | \N [7] \n 以外の任意の文字 | |
2469 | \N [7] \n 以外の任意の文字; /s 修飾子の影響は受けない | |
1620 | 2470 | \v [3] 垂直空白 |
1621 | 2471 | \V [3] 垂直空白以外 |
1622 | 2472 | \h [3] 水平空白 |
1623 | 2473 | \H [3] 水平空白以外 |
1624 | 2474 | \R [4] 行区切り |
1625 | 2475 | |
1626 | 2476 | =over 4 |
1627 | 2477 | |
1628 | 2478 | =item [1] |
1629 | 2479 | |
1630 | 2480 | =begin original |
1631 | 2481 | |
1632 | 2482 | See L<perlrecharclass/Bracketed Character Classes> for details. |
1633 | 2483 | |
1634 | 2484 | =end original |
1635 | 2485 | |
1636 | 2486 | 詳しくは L<perlrecharclass/Bracketed Character Classes> を参照してください。 |
1637 | 2487 | |
1638 | 2488 | =item [2] |
1639 | 2489 | |
1640 | 2490 | =begin original |
1641 | 2491 | |
1642 | 2492 | See L<perlrecharclass/POSIX Character Classes> for details. |
1643 | 2493 | |
1644 | 2494 | =end original |
1645 | 2495 | |
1646 | 2496 | 詳しくは L<perlrecharclass/POSIX Character Classes> を参照してください。 |
1647 | 2497 | |
1648 | 2498 | =item [3] |
1649 | 2499 | |
1650 | 2500 | =begin original |
1651 | 2501 | |
1652 | See L<perl | |
2502 | See L<perlunicode/Unicode Character Properties> for details | |
1653 | 2503 | |
1654 | 2504 | =end original |
1655 | 2505 | |
1656 | 詳しくは L<perl | |
2506 | 詳しくは L<perlunicode/Unicode Character Properties> を参照してください。 | |
1657 | 2507 | |
1658 | 2508 | =item [4] |
1659 | 2509 | |
1660 | 2510 | =begin original |
1661 | 2511 | |
1662 | 2512 | See L<perlrebackslash/Misc> for details. |
1663 | 2513 | |
1664 | 2514 | =end original |
1665 | 2515 | |
1666 | 2516 | 詳しくは L<perlrebackslash/Misc> を参照してください。 |
1667 | 2517 | |
1668 | 2518 | =item [5] |
1669 | 2519 | |
1670 | 2520 | =begin original |
1671 | 2521 | |
1672 | 2522 | See L</Capture groups> below for details. |
1673 | 2523 | |
1674 | 2524 | =end original |
1675 | 2525 | |
1676 | 2526 | 詳しくは以下の L</Capture groups> を参照してください。 |
1677 | 2527 | |
1678 | 2528 | =item [6] |
1679 | 2529 | |
1680 | 2530 | =begin original |
1681 | 2531 | |
1682 | 2532 | See L</Extended Patterns> below for details. |
1683 | 2533 | |
1684 | 2534 | =end original |
1685 | 2535 | |
1686 | 2536 | 詳しくは以下のSee L</Extended Patterns> を参照してください。 |
1687 | 2537 | |
1688 | 2538 | =item [7] |
1689 | 2539 | |
1690 | 2540 | =begin original |
1691 | 2541 | |
1692 | Note that C<\N> has two meanings. When of the form C<\N{NAME}>, it | |
2542 | Note that C<\N> has two meanings. When of the form C<\N{I<NAME>}>, it | |
1693 | character or character sequence whose name is | |
2543 | matches the character or character sequence whose name is I<NAME>; and | |
2544 | similarly | |
1694 | 2545 | when of the form C<\N{U+I<hex>}>, it matches the character whose Unicode |
1695 | 2546 | code point is I<hex>. Otherwise it matches any character but C<\n>. |
1696 | 2547 | |
1697 | 2548 | =end original |
1698 | 2549 | |
1699 | 2550 | C<\N> には二つの意味があることに注意してください。 |
1700 | C<\N{NAME}> の形式では、これは名前が | |
2551 | C<\N{I<NAME>}> の形式では、これは名前が I<NAME> の文字または文字の並びに | |
1701 | 2552 | マッチングします; |
1702 | 2553 | 同様に、C<\N{U+I<wide hex char>}> の形式では、Unicode 符号位置が |
1703 | 2554 | I<hex> の文字にマッチングします。 |
1704 | 2555 | そうでなければ、C<\n> 以外の任意の文字にマッチングします。 |
1705 | 2556 | |
2557 | =item [8] | |
2558 | ||
2559 | =begin original | |
2560 | ||
2561 | See L<perlrecharclass/Extended Bracketed Character Classes> for details. | |
2562 | ||
2563 | =end original | |
2564 | ||
2565 | 詳しくは L<perlrecharclass/Extended Bracketed Character Classes> を | |
2566 | 参照してください。 | |
2567 | ||
1706 | 2568 | =back |
1707 | 2569 | |
1708 | 2570 | =head3 Assertions |
1709 | 2571 | |
1710 | 2572 | (言明) |
1711 | 2573 | |
1712 | 2574 | =begin original |
1713 | 2575 | |
1714 | Perl defines the following | |
2576 | Besides L<C<"^"> and C<"$">|/Metacharacters>, Perl defines the following | |
2577 | zero-width assertions: | |
1715 | 2578 | X<zero-width assertion> X<assertion> X<regex, zero-width assertion> |
1716 | 2579 | X<regexp, zero-width assertion> |
1717 | 2580 | X<regular expression, zero-width assertion> |
1718 | 2581 | X<\b> X<\B> X<\A> X<\Z> X<\z> X<\G> |
1719 | 2582 | |
1720 | 2583 | =end original |
1721 | 2584 | |
2585 | L<C<"^"> と C<"$">|/Metacharacters> の他に、 | |
1722 | 2586 | Perl は以下のゼロ幅のアサーションを定義しています: |
1723 | 2587 | X<zero-width assertion> X<assertion> X<regex, zero-width assertion> |
1724 | 2588 | X<regexp, zero-width assertion> |
1725 | 2589 | X<regular expression, zero-width assertion> |
1726 | 2590 | X<\b> X<\B> X<\A> X<\Z> X<\z> X<\G> |
1727 | 2591 | |
1728 | | |
2592 | \b{} Match at Unicode boundary of specified type | |
1729 | | |
2593 | \B{} Match where corresponding \b{} doesn't match | |
1730 | | |
2594 | \b Match a \w\W or \W\w boundary | |
1731 | | |
2595 | \B Match except at a \w\W or \W\w boundary | |
1732 | | |
2596 | \A Match only at beginning of string | |
1733 | | |
2597 | \Z Match only at end of string, or before newline at the end | |
2598 | \z Match only at end of string | |
2599 | \G Match only at pos() (e.g. at the end-of-match position | |
1734 | 2600 | of prior m//g) |
1735 | 2601 | |
1736 | 2602 | =begin original |
1737 | 2603 | |
2604 | A Unicode boundary (C<\b{}>), available starting in v5.22, is a spot | |
2605 | between two characters, or before the first character in the string, or | |
2606 | after the final character in the string where certain criteria defined | |
2607 | by Unicode are met. See L<perlrebackslash/\b{}, \b, \B{}, \B> for | |
2608 | details. | |
2609 | ||
2610 | =end original | |
2611 | ||
2612 | v5.22 から利用可能である Unicode 境界 (C<\b{}>) は、 | |
2613 | Unicode で定義されたある種の基準に一致した、 | |
2614 | 二つの文字の間か、文字列の最初の文字の前か、 | |
2615 | 文字列の最後の文字の後の地点です。 | |
2616 | 詳しくは L<perlrebackslash/\b{}, \b, \B{}, \B> を参照してください。 | |
2617 | ||
2618 | =begin original | |
2619 | ||
1738 | 2620 | A word boundary (C<\b>) is a spot between two characters |
1739 | 2621 | that has a C<\w> on one side of it and a C<\W> on the other side |
1740 | 2622 | of it (in either order), counting the imaginary characters off the |
1741 | 2623 | beginning and end of the string as matching a C<\W>. (Within |
1742 | 2624 | character classes C<\b> represents backspace rather than a word |
1743 | 2625 | 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 | |
2626 | The C<\A> and C<\Z> are just like C<"^"> and C<"$">, except that they | |
1745 | 2627 | won't match multiple times when the C</m> modifier is used, while |
1746 | "^" and "$" will match at every internal line boundary. To match | |
2628 | C<"^"> and C<"$"> will match at every internal line boundary. To match | |
1747 | 2629 | the actual end of the string and not ignore an optional trailing |
1748 | 2630 | newline, use C<\z>. |
1749 | 2631 | X<\b> X<\A> X<\Z> X<\z> X</m> |
1750 | 2632 | |
1751 | 2633 | =end original |
1752 | 2634 | |
1753 | 2635 | 単語境界(C<\b>)はC<\W> にマッチングする文字列の始まりと終わりを |
1754 | 2636 | 連想するような、片側を C<\w>、もう片側を C<\W> で挟まれている点です。 |
1755 | 2637 | (文字クラスにおいては C<\b> は単語境界ではなくバックスペースを表します, |
1756 | 2638 | ちょうどダブルクォート文字列と同じように。) |
1757 | C<\A> 及び C<\Z> は "^" 及び "$" と同様ですが、C</m> 修飾子が | |
2639 | C<\A> 及び C<\Z> は C<"^"> 及び C<"$"> と同様ですが、C</m> 修飾子が | |
1758 | 指定されているときに "^" 及び "$" は全ての内部的な行境界に | |
2640 | 指定されているときに C<"^"> 及び C<"$"> は全ての内部的な行境界に | |
1759 | 2641 | マッチングするのに対して C<\A> 及び C<\Z> は複数回のマッチングには |
1760 | 2642 | なりません。 |
1761 | 2643 | 文字列の本当の末尾にマッチングさせ、省略可能である末尾の改行を |
1762 | 2644 | 無視しないようにする C<\z> を使います。 |
1763 | 2645 | X<\b> X<\A> X<\Z> X<\z> X</m> |
1764 | 2646 | |
1765 | 2647 | =begin original |
1766 | 2648 | |
1767 | 2649 | The C<\G> assertion can be used to chain global matches (using |
1768 | 2650 | C<m//g>), as described in L<perlop/"Regexp Quote-Like Operators">. |
1769 | 2651 | It is also useful when writing C<lex>-like scanners, when you have |
1770 | 2652 | several patterns that you want to match against consequent substrings |
1771 | 2653 | of your string; see the previous reference. The actual location |
1772 | 2654 | where C<\G> will match can also be influenced by using C<pos()> as |
1773 | 2655 | an lvalue: see L<perlfunc/pos>. Note that the rule for zero-length |
1774 | 2656 | matches (see L</"Repeated Patterns Matching a Zero-length Substring">) |
1775 | 2657 | is modified somewhat, in that contents to the left of C<\G> are |
1776 | 2658 | not counted when determining the length of the match. Thus the following |
1777 | 2659 | will not match forever: |
1778 | 2660 | X<\G> |
1779 | 2661 | |
1780 | 2662 | =end original |
1781 | 2663 | |
1782 | 2664 | C<\G> アサーションはグローバルなマッチング(C<m//g>)を連結するために |
1783 | 2665 | 使います; これは L<perlop/"Regexp Quote-Like Operators"> にも説明されています。 |
1784 | 2666 | これは文字列に対していくつかのパターンを次々にマッチングさせたいといった、 |
1785 | 2667 | C<lex> 風のスキャナを書きたいときにも便利です; 以前のリファレンスを |
1786 | 2668 | 参照してください。 |
1787 | 2669 | C<\G> が実際にマッチングできる位置は C<pos()> を左辺値として |
1788 | 2670 | 使うことで変更できます: L<perlfunc/pos> を参照してください。 |
1789 | 2671 | ゼロ幅マッチング |
1790 | 2672 | (L</"Repeated Patterns Matching a Zero-length Substring"> を参照してください) |
1791 | 2673 | のルールは少し変化することに注意してください、 |
1792 | 2674 | C<\G> の左にある内容はマッチングの長さを決定するときに |
1793 | 2675 | 数えられません。 |
1794 | 2676 | 従って次のコードは永遠にマッチングしません: |
1795 | 2677 | X<\G> |
1796 | 2678 | |
1797 | 2679 | my $string = 'ABC'; |
1798 | 2680 | pos($string) = 1; |
1799 | 2681 | while ($string =~ /(.\G)/g) { |
1800 | 2682 | print $1; |
1801 | 2683 | } |
1802 | 2684 | |
1803 | 2685 | =begin original |
1804 | 2686 | |
1805 | 2687 | It will print 'A' and then terminate, as it considers the match to |
1806 | 2688 | be zero-width, and thus will not match at the same position twice in a |
1807 | 2689 | row. |
1808 | 2690 | |
1809 | 2691 | =end original |
1810 | 2692 | |
1811 | 2693 | これはゼロ幅へのマッチングと見なされ、'A' を出力し終了するので、 |
1812 | 2694 | 行の中で同じ場所に二度はマッチングしません。 |
1813 | 2695 | |
1814 | 2696 | =begin original |
1815 | 2697 | |
1816 | 2698 | It is worth noting that C<\G> improperly used can result in an infinite |
1817 | 2699 | loop. Take care when using patterns that include C<\G> in an alternation. |
1818 | 2700 | |
1819 | 2701 | =end original |
1820 | 2702 | |
1821 | 2703 | 適切に使われていない C<\G> は無限ループとなり何の価値もありません。 |
1822 | 2704 | 代替(alternation; C<|>)の中に C<\G> を含んでいるパターンを使う際には |
1823 | 2705 | 十分注意してください。 |
1824 | 2706 | |
2707 | =begin original | |
2708 | ||
2709 | Note also that C<s///> will refuse to overwrite part of a substitution | |
2710 | that has already been replaced; so for example this will stop after the | |
2711 | first iteration, rather than iterating its way backwards through the | |
2712 | string: | |
2713 | ||
2714 | =end original | |
2715 | ||
2716 | C<s///> は置換部の既に置き換えられた部分を上書きすることを拒否することにも | |
2717 | 注意してください; 従って例えばこれは文字列の後ろ向きの反復中ではなく、 | |
2718 | 最初の反復の後に停止します: | |
2719 | ||
2720 | $_ = "123456789"; | |
2721 | pos = 6; | |
2722 | s/.(?=.\G)/X/g; | |
2723 | print; # prints 1234X6789, not XXXXX6789 | |
2724 | ||
1825 | 2725 | =head3 Capture groups |
1826 | 2726 | |
1827 | 2727 | (捕捉グループ) |
1828 | 2728 | |
1829 | 2729 | =begin original |
1830 | 2730 | |
1831 | The | |
2731 | The grouping construct C<( ... )> creates capture groups (also referred to as | |
1832 | 2732 | capture buffers). To refer to the current contents of a group later on, within |
1833 | 2733 | the same pattern, use C<\g1> (or C<\g{1}>) for the first, C<\g2> (or C<\g{2}>) |
1834 | 2734 | for the second, and so on. |
1835 | 2735 | This is called a I<backreference>. |
1836 | 2736 | X<regex, capture buffer> X<regexp, capture buffer> |
1837 | 2737 | X<regex, capture group> X<regexp, capture group> |
1838 | 2738 | X<regular expression, capture buffer> X<backreference> |
1839 | 2739 | X<regular expression, capture group> X<backreference> |
1840 | 2740 | X<\g{1}> X<\g{-1}> X<\g{name}> X<relative backreference> X<named backreference> |
1841 | 2741 | X<named capture buffer> X<regular expression, named capture buffer> |
1842 | 2742 | X<named capture group> X<regular expression, named capture group> |
1843 | 2743 | X<%+> X<$+{name}> X<< \k<name> >> |
1844 | 2744 | 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 | |
2745 | Groups are numbered with the leftmost open parenthesis being number 1, I<etc>. If | |
1846 | 2746 | a group did not match, the associated backreference won't match either. (This |
1847 | 2747 | can happen if the group is optional, or in a different branch of an |
1848 | 2748 | alternation.) |
1849 | You can omit the C<"g">, and write C<"\1">, etc, but there are some issues with | |
2749 | You can omit the C<"g">, and write C<"\1">, I<etc>, but there are some issues with | |
1850 | 2750 | this form, described below. |
1851 | 2751 | |
1852 | 2752 | =end original |
1853 | 2753 | |
1854 | ||
2754 | グループ化構文 C<( ... )> は捕捉グループを作成します (そして捕捉バッファとして | |
1855 | 2755 | 参照します)。 |
1856 | 2756 | 同じパターンの中で、あるグループの現在の内容を後で参照するには、 |
1857 | 2757 | 最初のものには C<\g1> (または C<\g{1}>) を、2 番目には C<\g2> (または |
1858 | 2758 | C<\g{2}>) を、以下同様のものを使います。 |
1859 | 2759 | これを I<後方参照> (backreference) と呼びます。 |
1860 | 2760 | X<regex, capture buffer> X<regexp, capture buffer> |
1861 | 2761 | X<regex, capture group> X<regexp, capture group> |
1862 | 2762 | X<regular expression, capture buffer> X<backreference> |
1863 | 2763 | X<regular expression, capture group> X<backreference> |
1864 | 2764 | X<\g{1}> X<\g{-1}> X<\g{name}> X<relative backreference> X<named backreference> |
1865 | 2765 | X<named capture buffer> X<regular expression, named capture buffer> |
1866 | 2766 | X<named capture group> X<regular expression, named capture group> |
1867 | 2767 | X<%+> X<$+{name}> X<< \k<name> >> |
1868 | 2768 | 使う捕捉部分文字列の数に制限はありません。 |
1869 | 2769 | グループはいちばん左の開きかっこを 1 番として番号付けされます。 |
1870 | 2770 | グループがマッチングしなかった場合、対応する後方参照もマッチングしません。 |
1871 | 2771 | (これはグループがオプションか、選択の異なる枝の場合に怒ることがあります。) |
1872 | 2772 | C<"g"> を省略して C<"\1"> などと書くこともできますが、後で述べるように、 |
1873 | 2773 | この形式にはいくらかの問題があります。 |
1874 | 2774 | |
1875 | 2775 | =begin original |
1876 | 2776 | |
1877 | 2777 | You can also refer to capture groups relatively, by using a negative number, so |
1878 | 2778 | that C<\g-1> and C<\g{-1}> both refer to the immediately preceding capture |
1879 | 2779 | group, and C<\g-2> and C<\g{-2}> both refer to the group before it. For |
1880 | 2780 | example: |
1881 | 2781 | |
1882 | 2782 | =end original |
1883 | 2783 | |
1884 | 2784 | 負数を使うことで捕捉グループを相対的に参照することもできます; C<\g-1> と |
1885 | 2785 | C<\g{-1}> は両方とも直前の捕捉グループを参照し、C<\g-2> と C<\g{-2}> は |
1886 | 2786 | 両方ともその前のグループを参照します。 |
1887 | 2787 | 例えば: |
1888 | 2788 | |
1889 | 2789 | / |
1890 | 2790 | (Y) # group 1 |
1891 | 2791 | ( # group 2 |
1892 | 2792 | (X) # group 3 |
1893 | 2793 | \g{-1} # backref to group 3 |
1894 | 2794 | \g{-3} # backref to group 1 |
1895 | 2795 | ) |
1896 | 2796 | /x |
1897 | 2797 | |
1898 | 2798 | =begin original |
1899 | 2799 | |
1900 | 2800 | would match the same as C</(Y) ( (X) \g3 \g1 )/x>. This allows you to |
1901 | 2801 | interpolate regexes into larger regexes and not have to worry about the |
1902 | 2802 | capture groups being renumbered. |
1903 | 2803 | |
1904 | 2804 | =end original |
1905 | 2805 | |
1906 | 2806 | は C</(Y) ( (X) \g3 \g1 )/x> と同じマッチングとなります。 |
1907 | 2807 | これにより、正規表現をより大きな正規表現に挿入したときに、捕捉グループの |
1908 | 2808 | 番号を振り直す心配をする必要がなくなります。 |
1909 | 2809 | |
1910 | 2810 | =begin original |
1911 | 2811 | |
1912 | 2812 | You can dispense with numbers altogether and create named capture groups. |
1913 | 2813 | The notation is C<(?E<lt>I<name>E<gt>...)> to declare and C<\g{I<name>}> to |
1914 | 2814 | reference. (To be compatible with .Net regular expressions, C<\g{I<name>}> may |
1915 | 2815 | also be written as C<\k{I<name>}>, C<\kE<lt>I<name>E<gt>> or C<\k'I<name>'>.) |
1916 | 2816 | I<name> must not begin with a number, nor contain hyphens. |
1917 | 2817 | When different groups within the same pattern have the same name, any reference |
1918 | 2818 | to that name assumes the leftmost defined group. Named groups count in |
1919 | 2819 | absolute and relative numbering, and so can also be referred to by those |
1920 | 2820 | numbers. |
1921 | 2821 | (It's possible to do things with named capture groups that would otherwise |
1922 | 2822 | require C<(??{})>.) |
1923 | 2823 | |
1924 | 2824 | =end original |
1925 | 2825 | |
1926 | 2826 | 数値を全く使わずに、名前付き捕捉グループを作ることが出来ます。 |
1927 | 2827 | 記法は、宣言が C<(?E<lt>I<name>E<gt>...)>、参照が C<\g{I<name>}> です。 |
1928 | 2828 | (.Net 正規表現との互換性のために、C<\g{I<name>}> は C<\k{I<name>}>, |
1929 | 2829 | C<\kE<lt>I<name>E<gt>>, C<\k'I<name>'> とも書けます。) |
1930 | 2830 | I<name> は数字で始まってはならず、ハイフンを含んではなりません。 |
1931 | 2831 | 同じパターンの中に同じ名前の違うグループがある場合、 |
1932 | 2832 | この名前での参照は一番左で定義されたものを仮定します。 |
1933 | 2833 | 名前付きグループも絶対や相対番号付けに含まれるので、 |
1934 | 2834 | 番号で参照することも出来ます。 |
1935 | 2835 | (C<(??{})> が必要な場合でも名前付き捕捉グループを使うことが出来ます。) |
1936 | 2836 | |
1937 | 2837 | =begin original |
1938 | 2838 | |
1939 | 2839 | Capture group contents are dynamically scoped and available to you outside the |
1940 | 2840 | pattern until the end of the enclosing block or until the next successful |
1941 | 2841 | match, whichever comes first. (See L<perlsyn/"Compound Statements">.) |
1942 | 2842 | 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>}">. | |
2843 | I<etc>); or by name via the C<%+> hash, using C<"$+{I<name>}">. | |
1944 | 2844 | |
1945 | 2845 | =end original |
1946 | 2846 | |
1947 | 2847 | 捕捉グループの内容は動的スコープを持ち、パターンの外側でも現在のブロックの |
1948 | 2848 | 末尾か次のマッチングの成功のどちらか早いほうまで利用可能です。 |
1949 | 2849 | (L<perlsyn/"Compound Statements"> を参照してください。) |
1950 | 2850 | これらに対して (C<"\g1"> などの代わりに C<"$1"> を使って) 絶対値で |
1951 | 2851 | 参照するか、C<"$+{I<name>}"> を使って C<%+> 経由で名前で参照できます。 |
1952 | 2852 | |
1953 | 2853 | =begin original |
1954 | 2854 | |
1955 | 2855 | Braces are required in referring to named capture groups, but are optional for |
1956 | 2856 | absolute or relative numbered ones. Braces are safer when creating a regex by |
1957 | 2857 | concatenating smaller strings. For example if you have C<qr/$a$b/>, and C<$a> |
1958 | 2858 | contained C<"\g1">, and C<$b> contained C<"37">, you would get C</\g137/> which |
1959 | 2859 | is probably not what you intended. |
1960 | 2860 | |
1961 | 2861 | =end original |
1962 | 2862 | |
1963 | 2863 | 名前付き捕捉グループを参照するには中かっこが必要です; |
1964 | 2864 | しかし、絶対数値や相対数値の場合はオプションです。 |
1965 | 2865 | より小さい文字列を結合して正規表現を作る場合は中かっこを使う方が安全です。 |
1966 | 2866 | 例えば C<qr/$a$b/> で C<$a> に C<"\g1"> を含み、 |
1967 | 2867 | C<$b> に C<"37"> を含んでいるとき、 |
1968 | 2868 | 結果は C</\g137/> となりますが、おそらく望んでいたものではないでしょう。 |
1969 | 2869 | |
1970 | 2870 | =begin original |
1971 | 2871 | |
1972 | 2872 | The C<\g> and C<\k> notations were introduced in Perl 5.10.0. Prior to that |
1973 | 2873 | there were no named nor relative numbered capture groups. Absolute numbered |
1974 | 2874 | groups were referred to using C<\1>, |
1975 | C<\2>, etc., and this notation is still | |
2875 | C<\2>, I<etc>., and this notation is still | |
1976 | 2876 | accepted (and likely always will be). But it leads to some ambiguities if |
1977 | 2877 | there are more than 9 capture groups, as C<\10> could mean either the tenth |
1978 | 2878 | capture group, or the character whose ordinal in octal is 010 (a backspace in |
1979 | 2879 | ASCII). Perl resolves this ambiguity by interpreting C<\10> as a backreference |
1980 | 2880 | only if at least 10 left parentheses have opened before it. Likewise C<\11> is |
1981 | 2881 | a backreference only if at least 11 left parentheses have opened before it. |
1982 | 2882 | And so on. C<\1> through C<\9> are always interpreted as backreferences. |
1983 | 2883 | There are several examples below that illustrate these perils. You can avoid |
1984 | 2884 | the ambiguity by always using C<\g{}> or C<\g> if you mean capturing groups; |
1985 | 2885 | and for octal constants always using C<\o{}>, or for C<\077> and below, using 3 |
1986 | 2886 | digits padded with leading zeros, since a leading zero implies an octal |
1987 | 2887 | constant. |
1988 | 2888 | |
1989 | 2889 | =end original |
1990 | 2890 | |
1991 | 2891 | C<\g> と C<\k> の記法は Perl 5.10.0 で導入されました。 |
1992 | 2892 | それより前には名前付きや相対数値指定の捕捉グループはありませんでした。 |
1993 | 2893 | 絶対数値指定のグループは C<\1>, C<\2> などとして参照でき、この記法はまだ |
1994 | 2894 | 受け付けられます (そしておそらくいつも受け付けられます)。 |
1995 | 2895 | しかし、これは 9 を越える捕捉グループがあるとあいまいさがあります; |
1996 | 2896 | C<\10> は 10 番目の捕捉グループとも、8 進数で 010 の文字(ASCII で |
1997 | 2897 | バックスペース)とも解釈できます。 |
1998 | 2898 | Perl はこのあいまいさを以下のように解決します; |
1999 | 2899 | C<\10> の場合、これの前に少なくとも 10 の左かっこがある場合にのみ |
2000 | 2900 | これを後方参照として解釈します。 |
2001 | 2901 | 同様に、C<\11> はその前に少なくとも 11 の左かっこがある場合にのみ |
2002 | 2902 | これを後方参照として解釈します。 |
2003 | 2903 | 以下同様です。 |
2004 | 2904 | C<\1> から C<\9> は常に後方参照として解釈されます。 |
2005 | 2905 | これを図示するいくつかの例が後にあります。 |
2006 | 2906 | 捕捉グループを意味する場合は常に C<\g{}> や C<\g> を使うことで |
2007 | 2907 | あいまいさを避けられます; |
2008 | 2908 | そして 8 進定数については常に C<\o{}> を使うか、C<\077> 以下の場合は、 |
2009 | 2909 | 先頭に 0 を付けて 3 桁にします; なぜなら先頭に 0 が付くと |
2010 | 2910 | 8 進定数を仮定するからです。 |
2011 | 2911 | |
2012 | 2912 | =begin original |
2013 | 2913 | |
2014 | 2914 | The C<\I<digit>> notation also works in certain circumstances outside |
2015 | 2915 | the pattern. See L</Warning on \1 Instead of $1> below for details. |
2016 | 2916 | |
2017 | 2917 | =end original |
2018 | 2918 | |
2019 | 2919 | C<\I<digit>> 記法は、ある種の状況ではパターンの外側でも動作します。 |
2020 | 2920 | 詳しくは後述する L</Warning on \1 Instead of $1> を参照して下さい。 |
2021 | 2921 | |
2022 | 2922 | =begin original |
2023 | 2923 | |
2024 | 2924 | Examples: |
2025 | 2925 | |
2026 | 2926 | =end original |
2027 | 2927 | |
2028 | 2928 | 例: |
2029 | 2929 | |
2030 | 2930 | s/^([^ ]*) *([^ ]*)/$2 $1/; # swap first two words |
2031 | 2931 | |
2032 | 2932 | /(.)\g1/ # find first doubled char |
2033 | 2933 | and print "'$1' is the first doubled character\n"; |
2034 | 2934 | |
2035 | 2935 | /(?<char>.)\k<char>/ # ... a different way |
2036 | 2936 | and print "'$+{char}' is the first doubled character\n"; |
2037 | 2937 | |
2038 | 2938 | /(?'char'.)\g1/ # ... mix and match |
2039 | 2939 | and print "'$1' is the first doubled character\n"; |
2040 | 2940 | |
2041 | 2941 | if (/Time: (..):(..):(..)/) { # parse out values |
2042 | 2942 | $hours = $1; |
2043 | 2943 | $minutes = $2; |
2044 | 2944 | $seconds = $3; |
2045 | 2945 | } |
2046 | 2946 | |
2047 | 2947 | /(.)(.)(.)(.)(.)(.)(.)(.)(.)\g10/ # \g10 is a backreference |
2048 | 2948 | /(.)(.)(.)(.)(.)(.)(.)(.)(.)\10/ # \10 is octal |
2049 | 2949 | /((.)(.)(.)(.)(.)(.)(.)(.)(.))\10/ # \10 is a backreference |
2050 | 2950 | /((.)(.)(.)(.)(.)(.)(.)(.)(.))\010/ # \010 is octal |
2051 | 2951 | |
2052 | 2952 | $a = '(.)\1'; # Creates problems when concatenated. |
2053 | 2953 | $b = '(.)\g{1}'; # Avoids the problems. |
2054 | 2954 | "aa" =~ /${a}/; # True |
2055 | 2955 | "aa" =~ /${b}/; # True |
2056 | 2956 | "aa0" =~ /${a}0/; # False! |
2057 | 2957 | "aa0" =~ /${b}0/; # True |
2058 | 2958 | "aa\x08" =~ /${a}0/; # True! |
2059 | 2959 | "aa\x08" =~ /${b}0/; # False |
2060 | 2960 | |
2061 | 2961 | =begin original |
2062 | 2962 | |
2063 | 2963 | Several special variables also refer back to portions of the previous |
2064 | 2964 | match. C<$+> returns whatever the last bracket match matched. |
2065 | 2965 | C<$&> returns the entire matched string. (At one point C<$0> did |
2066 | 2966 | also, but now it returns the name of the program.) C<$`> returns |
2067 | 2967 | everything before the matched string. C<$'> returns everything |
2068 | 2968 | after the matched string. And C<$^N> contains whatever was matched by |
2069 | 2969 | the most-recently closed group (submatch). C<$^N> can be used in |
2070 | 2970 | extended patterns (see below), for example to assign a submatch to a |
2071 | 2971 | variable. |
2072 | 2972 | X<$+> X<$^N> X<$&> X<$`> X<$'> |
2073 | 2973 | |
2074 | 2974 | =end original |
2075 | 2975 | |
2076 | 2976 | いくつかの特殊変数もまた以前のマッチングの一部を参照しています。 |
2077 | 2977 | C<$+> は最後のマッチングしたブラケットマッチングを返します。 |
2078 | 2978 | C<$&> はマッチングした文字列全体を返します。 |
2079 | 2979 | (一頃は C<$0> もそうでしたが、現在ではこれはプログラム名を返します。) |
2080 | 2980 | C<$`> はマッチングした文字列の前の全てを返します。 |
2081 | 2981 | C<$'> はマッチングした文字列の後の全てを返します。 |
2082 | 2982 | そして C<$^N> には一番最後に閉じたグループ(サブマッチング)に |
2083 | 2983 | マッチングしたものを含んでいます。 |
2084 | 2984 | C<$^N> は例えばサブマッチングを変数に格納するため等に拡張パターンの中でも |
2085 | 2985 | 利用できます(後述)。 |
2086 | 2986 | X<$+> X<$^N> X<$&> X<$`> X<$'> |
2087 | 2987 | |
2088 | 2988 | =begin original |
2089 | 2989 | |
2090 | 2990 | These special variables, like the C<%+> hash and the numbered match variables |
2091 | (C<$1>, C<$2>, C<$3>, etc.) are dynamically scoped | |
2991 | (C<$1>, C<$2>, C<$3>, I<etc>.) are dynamically scoped | |
2092 | 2992 | until the end of the enclosing block or until the next successful |
2093 | 2993 | match, whichever comes first. (See L<perlsyn/"Compound Statements">.) |
2094 | 2994 | X<$+> X<$^N> X<$&> X<$`> X<$'> |
2095 | 2995 | X<$1> X<$2> X<$3> X<$4> X<$5> X<$6> X<$7> X<$8> X<$9> |
2096 | 2996 | |
2097 | 2997 | =end original |
2098 | 2998 | |
2099 | 2999 | C<%+> ハッシュのような特殊変数と、数値によるマッチング変数 |
2100 | 3000 | (C<$1>, C<$2>, C<$3> など)はブロックの終端または次のマッチング |
2101 | 3001 | 成功までのどちらか先に満たした方の、動的なスコープを持ちます。 |
2102 | 3002 | (L<perlsyn/"Compound Statements"> を参照してください。) |
2103 | 3003 | X<$+> X<$^N> X<$&> X<$`> X<$'> |
2104 | 3004 | X<$1> X<$2> X<$3> X<$4> X<$5> X<$6> X<$7> X<$8> X<$9> |
2105 | 3005 | |
2106 | 3006 | =begin original |
2107 | 3007 | |
2108 | 3008 | B<NOTE>: Failed matches in Perl do not reset the match variables, |
2109 | 3009 | which makes it easier to write code that tests for a series of more |
2110 | 3010 | specific cases and remembers the best match. |
2111 | 3011 | |
2112 | 3012 | =end original |
2113 | 3013 | |
2114 | 3014 | B<補足>: Perl において失敗したマッチングはマッチング変数をリセットしません; |
2115 | 3015 | これはより特殊化させる一連のテストを書くことや、 |
2116 | 3016 | 最善のマッチングを書くことを容易にします。 |
2117 | 3017 | |
2118 | 3018 | =begin original |
2119 | 3019 | |
2120 | B<WARNING>: | |
3020 | B<WARNING>: If your code is to run on Perl 5.16 or earlier, | |
3021 | beware that once Perl sees that you need one of C<$&>, C<$`>, or | |
2121 | 3022 | C<$'> anywhere in the program, it has to provide them for every |
2122 | pattern match. This may substantially slow your program. | |
3023 | pattern match. This may substantially slow your program. | |
2123 | ||
2124 | ||
3025 | =end original | |
2125 | ||
3027 | B<警告>: あなたのコードが Perl 5.16 以前で実行されるものの場合、 | |
3028 | Perl は、一旦プログラム中のどこかで C<$&>, C<$`>, C<$'> の | |
3029 | いずれかを必要としていることを見つけると、全てのパターンマッチングで | |
3030 | それらを提供しなければなあらないことに注意してください。 | |
3031 | これはあなたのプログラムを大幅に遅くさせるでしょう。 | |
3032 | ||
3033 | =begin original | |
3034 | ||
3035 | Perl uses the same mechanism to produce C<$1>, C<$2>, I<etc>, so you also | |
3036 | pay a price for each pattern that contains capturing parentheses. | |
3037 | (To avoid this cost while retaining the grouping behaviour, use the | |
2126 | 3038 | extended regular expression C<(?: ... )> instead.) But if you never |
2127 | 3039 | use C<$&>, C<$`> or C<$'>, then patterns I<without> capturing |
2128 | 3040 | parentheses will not be penalized. So avoid C<$&>, C<$'>, and C<$`> |
2129 | 3041 | if you can, but if you can't (and some algorithms really appreciate |
2130 | 3042 | them), once you've used them once, use them at will, because you've |
2131 | already paid the price. | |
3043 | already paid the price. | |
2132 | other two. | |
2133 | 3044 | X<$&> X<$`> X<$'> |
2134 | 3045 | |
2135 | 3046 | =end original |
2136 | 3047 | |
2137 | B<警告>: Perl は、一旦プログラム中のどこかで C<$&>, C<$`>, C<$'> の | |
2138 | いずれかを必要としていることを見つけると、全てのパターンマッチングで | |
2139 | それらを提供しなければなりません。 | |
2140 | これはあなたのプログラムを大幅に遅くさせるでしょう。 | |
2141 | 3048 | Perl は C<$1>, C<$2> 等の生成にも同じメカニズムを使っているので、 |
2142 | 3049 | キャプチャのかっこに含まれるそれぞれのパターンにも |
2143 | 3050 | 同じ料金を払っています。 |
2144 | 3051 | (グループ化の振る舞いを維持しつつこのコストを削減するには |
2145 | 3052 | 拡張正規表現 C<(?: ... )> を代わりに使います |
2146 | 3053 | (訳注:Perl拡張というだけで C</x> 修飾子は不要)。) |
2147 | 3054 | ですが C<$&>, C<$`> または C<$'> を一度も使わなければ、 |
2148 | 3055 | キャプチャのかっこをI<もたない>パターンではこの不利益はなくなります。 |
2149 | 3056 | この為、可能であれば C<$&>, C<$'>, 及び C<$`> を削除しましょう: |
2150 | 3057 | しかしそれができなかった(そしてそれらを |
2151 | 3058 | 本当に理解しているアルゴリズムがあるのであれば)、一旦 |
2152 | 3059 | それらを使った時点でそれ以降は自由にそれらを使うことができます; |
2153 | 3060 | なぜならあなたは(一度使った時点で)既に代価を払っているので。 |
2154 | 5.005 であれば C<$&> は他の2つほど高価ではありません。 | |
2155 | 3061 | X<$&> X<$`> X<$'> |
2156 | 3062 | |
2157 | 3063 | =begin original |
2158 | 3064 | |
2159 | ||
3065 | Perl 5.16 introduced a slightly more efficient mechanism that notes | |
3066 | separately whether each of C<$`>, C<$&>, and C<$'> have been seen, and | |
3067 | thus may only need to copy part of the string. Perl 5.20 introduced a | |
3068 | much more efficient copy-on-write mechanism which eliminates any slowdown. | |
3069 | ||
3070 | =end original | |
3071 | ||
3072 | Perl 5.16 では、C<$`>, C<$&>, C<$'> のそれぞれが現れるかどうかを | |
3073 | 個別に記録するという少し効率的な機構が導入され、 | |
3074 | 従って文字列の一部分だけコピーするようになりました。 | |
3075 | Perl 5.20 では、全く遅くならない遙かに効率的なコピーオンライト機構を | |
3076 | 導入しました。 | |
3077 | ||
3078 | =begin original | |
3079 | ||
3080 | As another workaround for this problem, Perl 5.10.0 introduced C<${^PREMATCH}>, | |
2160 | 3081 | C<${^MATCH}> and C<${^POSTMATCH}>, which are equivalent to C<$`>, C<$&> |
2161 | 3082 | and C<$'>, B<except> that they are only guaranteed to be defined after a |
2162 | 3083 | successful match that was executed with the C</p> (preserve) modifier. |
2163 | 3084 | The use of these variables incurs no global performance penalty, unlike |
2164 | their punctuation char equivalents, however at the trade-off that you | |
3085 | their punctuation character equivalents, however at the trade-off that you | |
2165 | have to tell perl when you want to use them. | |
3086 | have to tell perl when you want to use them. As of Perl 5.20, these three | |
3087 | variables are equivalent to C<$`>, C<$&> and C<$'>, and C</p> is ignored. | |
2166 | 3088 | X</p> X<p modifier> |
2167 | 3089 | |
2168 | 3090 | =end original |
2169 | 3091 | |
2170 | この問題に対する解決策として、Perl 5.10.0 からは | |
3092 | この問題に対するもう一つの解決策として、Perl 5.10.0 からは | |
3093 | C<$`>, C<$&>, C<$'> と | |
2171 | 3094 | 等価だけれども C</p> (preseve) 修飾子を伴って実行されたマッチングが |
2172 | 3095 | 成功した後でのみ定義されることが保証される C<${^PREMATCH}>、 |
2173 | 3096 | C<${^MATCH}> 及び C<${^POSTMATCH}> を導入しました。 |
2174 | 3097 | これらの変数の使用は利用したいときに perl に伝える必要がある代わりに、 |
2175 | 3098 | 等価な記号変数とは違い全体的なパフォーマンスの低下を引き起こしません。 |
3099 | Perl 5.20 からこれら三つの変数は C<$`>, C<$&>, C<$'> と等価となり、 | |
3100 | C</p> は無視されます。 | |
2176 | 3101 | X</p> X<p modifier> |
2177 | 3102 | |
2178 | 3103 | =head2 Quoting metacharacters |
2179 | 3104 | |
2180 | 3105 | (メタ文字のクォート) |
2181 | 3106 | |
2182 | 3107 | =begin original |
2183 | 3108 | |
2184 | 3109 | Backslashed metacharacters in Perl are alphanumeric, such as C<\b>, |
2185 | 3110 | C<\w>, C<\n>. Unlike some other regular expression languages, there |
2186 | 3111 | are no backslashed symbols that aren't alphanumeric. So anything |
2187 | that looks like \\, \(, \), | |
3112 | that looks like C<\\>, C<\(>, C<\)>, C<\[>, C<\]>, C<\{>, or C<\}> is | |
3113 | always | |
2188 | 3114 | interpreted as a literal character, not a metacharacter. This was |
2189 | 3115 | once used in a common idiom to disable or quote the special meanings |
2190 | 3116 | of regular expression metacharacters in a string that you want to |
2191 | 3117 | use for a pattern. Simply quote all non-"word" characters: |
2192 | 3118 | |
2193 | 3119 | =end original |
2194 | 3120 | |
2195 | Perl において | |
3121 | Perl において逆スラッシュで表現されるメタ文字は C<\b>, C<\w>, | |
2196 | 3122 | C<\n> のように英数字です。 |
2197 | 他の正規表現言語とは異なり、英数字でない | |
3123 | 他の正規表現言語とは異なり、英数字でない逆スラッシュ付きシンボルは | |
2198 | 3124 | ありません。 |
2199 | なので \\, \(, \), | |
3125 | なので C<\\>, C<\(>, C<\)>, C<\[>, C<\]>, C<\{>, or C<\}> といったものは | |
2200 | リテラル文字です。 | |
3126 | 全てメタ文字ではなくリテラル文字です。 | |
2201 | 3127 | これはパターンで使いたい文字列の中で正規表現のメタ文字としての特殊な意味を |
2202 | 3128 | 無効化またはクォートするための一般的な指標として使われてきました。 |
2203 | 3129 | 「単語」でない全ての文字は単にクォートします: |
2204 | 3130 | |
2205 | 3131 | $pattern =~ s/(\W)/\\$1/g; |
2206 | 3132 | |
2207 | 3133 | =begin original |
2208 | 3134 | |
2209 | 3135 | (If C<use locale> is set, then this depends on the current locale.) |
2210 | Today it is more common to use the quotemeta() | |
3136 | Today it is more common to use the C<L<quotemeta()|perlfunc/quotemeta>> | |
2211 | metaquoting escape sequence to disable al | |
3137 | function or the C<\Q> metaquoting escape sequence to disable all | |
2212 | meanings like this: | |
3138 | metacharacters' special meanings like this: | |
2213 | 3139 | |
2214 | 3140 | =end original |
2215 | 3141 | |
2216 | 3142 | (もし C<use locale> が有効であれば、これは現在のロケールに依存します。) |
2217 | 3143 | 今日では特殊な意味を持つメタ文字を全て無効にするためには次のように |
2218 | quotemeta() 関数か C<\Q> メタクォート | |
3144 | C<L<quotemeta()|perlfunc/quotemeta>> 関数か C<\Q> メタクォート | |
2219 | より一般的です: | |
3145 | エスケープシーケンスを使うのがより一般的です: | |
2220 | 3146 | |
2221 | 3147 | /$unquoted\Q$quoted\E$unquoted/ |
2222 | 3148 | |
2223 | 3149 | =begin original |
2224 | 3150 | |
2225 | 3151 | Beware that if you put literal backslashes (those not inside |
2226 | 3152 | interpolated variables) between C<\Q> and C<\E>, double-quotish |
2227 | 3153 | backslash interpolation may lead to confusing results. If you |
2228 | 3154 | I<need> to use literal backslashes within C<\Q...\E>, |
2229 | 3155 | consult L<perlop/"Gory details of parsing quoted constructs">. |
2230 | 3156 | |
2231 | 3157 | =end original |
2232 | 3158 | |
2233 | C<\Q> 及び C<\E> の間でリテラルとして | |
3159 | C<\Q> 及び C<\E> の間でリテラルとして逆スラッシュをおくとき | |
2234 | (埋め込んだ変数の中でではない)には、二重にクォートした | |
3160 | (埋め込んだ変数の中でではない)には、二重にクォートした逆スラッシュの | |
2235 | 3161 | 埋め込みは困惑した結果となるでしょう。 |
2236 | もし C<\Q...\E> でリテラルとしての | |
3162 | もし C<\Q...\E> でリテラルとしての逆スラッシュを使う | |
2237 | 3163 | I<必要がある> のなら、 |
2238 | 3164 | L<perlop/"Gory details of parsing quoted constructs"> を参照してください。 |
2239 | 3165 | |
2240 | 3166 | =begin original |
2241 | 3167 | |
2242 | 3168 | C<quotemeta()> and C<\Q> are fully described in L<perlfunc/quotemeta>. |
2243 | 3169 | |
2244 | 3170 | =end original |
2245 | 3171 | |
2246 | 3172 | C<quotemeta()> と C<\Q> は L<perlfunc/quotemeta> に完全に記述されています。 |
2247 | 3173 | |
2248 | 3174 | =head2 Extended Patterns |
2249 | 3175 | |
2250 | 3176 | (拡張パターン) |
2251 | 3177 | |
2252 | 3178 | =begin original |
2253 | 3179 | |
2254 | 3180 | Perl also defines a consistent extension syntax for features not |
2255 | 3181 | found in standard tools like B<awk> and |
2256 | 3182 | B<lex>. The syntax for most of these is a |
2257 | 3183 | pair of parentheses with a question mark as the first thing within |
2258 | 3184 | the parentheses. The character after the question mark indicates |
2259 | 3185 | the extension. |
2260 | 3186 | |
2261 | 3187 | =end original |
2262 | 3188 | |
2263 | 3189 | Perl は B<awk> や B<lex> といった標準的なツールでは見られない機能のための |
2264 | 3190 | 拡張構文も定義しています。 |
2265 | 3191 | これらのほとんどの構文は対のかっことかっこ内の最初に疑問符の形をとります。 |
2266 | 3192 | 疑問符の後の文字で拡張を区別します。 |
2267 | 3193 | |
2268 | 3194 | =begin original |
2269 | 3195 | |
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 | 3196 | A question mark was chosen for this and for the minimal-matching |
2287 | 3197 | construct because 1) question marks are rare in older regular |
2288 | 3198 | expressions, and 2) whenever you see one, you should stop and |
2289 | 3199 | "question" exactly what is going on. That's psychology.... |
2290 | 3200 | |
2291 | 3201 | =end original |
2292 | 3202 | |
2293 | 3203 | 疑問符は 1) それが古い正規表現で使われることは稀であること、そして |
2294 | 3204 | 2) それを見かけると何が行われるのか本当に「疑問に」思って止まることから、 |
2295 | 3205 | これのためと最小マッチング構成子のために選ばれました。 |
2296 | 3206 | これが心理学です…。 |
2297 | 3207 | |
2298 | 3208 | =over 4 |
2299 | 3209 | |
2300 | =item C<(?#text)> | |
3210 | =item C<(?<#ins>I<text>)> | |
2301 | 3211 | X<(?#)> |
2302 | 3212 | |
2303 | 3213 | =begin original |
2304 | 3214 | |
2305 | A comment. The text is ignored. | |
3215 | A comment. The I<text> is ignored. | |
2306 | ||
3216 | Note that Perl closes | |
2307 | the comment as soon as it sees a C<)>, so there is no way to put a literal | |
3217 | the comment as soon as it sees a C<")">, so there is no way to put a literal | |
2308 | C<)> in the comment. | |
3218 | C<")"> in the comment. The pattern's closing delimiter must be escaped by | |
3219 | a backslash if it appears in the comment. | |
2309 | 3220 | |
2310 | 3221 | =end original |
2311 | 3222 | |
2312 | 3223 | コメント。 |
2313 | ||
3224 | I<text> は無視されます。 | |
2314 | C< | |
3225 | Perl は C<")"> を見つけると直ぐにコメントを閉じる点に注意してください; | |
2315 | ||
3226 | この為リテラル C<")"> をコメント中におくことはできません。 | |
2316 | ||
3227 | パターンの閉じ区切り文字がコメントに見えるようなものなら、 | |
3228 | 逆スラッシュでエスケープしなければなりません。 | |
2317 | 3229 | |
2318 | = | |
3230 | =begin original | |
2319 | 3231 | |
2320 | ||
3232 | See L</E<sol>x> for another way to have comments in patterns. | |
3233 | ||
3234 | =end original | |
3235 | ||
3236 | パターンの中にコメントを入れるもう一つの方法については | |
3237 | L</E<sol>x> を参照してください。 | |
3238 | ||
3239 | =begin original | |
3240 | ||
3241 | Note that a comment can go just about anywhere, except in the middle of | |
3242 | an escape sequence. Examples: | |
3243 | ||
3244 | =end original | |
3245 | ||
3246 | コメントは、エスケープシーケンスの途中を除いて、 | |
3247 | どこにでも入れることができることに注意してください。 | |
3248 | 例: | |
3249 | ||
3250 | qr/foo(?#comment)bar/' # Matches 'foobar' | |
3251 | ||
3252 | # The pattern below matches 'abcd', 'abccd', or 'abcccd' | |
3253 | qr/abc(?#comment between literal and its quantifier){1,3}d/ | |
3254 | ||
3255 | # The pattern below generates a syntax error, because the '\p' must | |
3256 | # be followed immediately by a '{'. | |
3257 | qr/\p(?#comment between \p and its property name){Any}/ | |
3258 | ||
3259 | # The pattern below generates a syntax error, because the initial | |
3260 | # '\(' is a literal opening parenthesis, and so there is nothing | |
3261 | # for the closing ')' to match | |
3262 | qr/\(?#the backslash means this isn't a comment)p{Any}/ | |
3263 | ||
3264 | # Comments can be used to fold long patterns into multiple lines | |
3265 | qr/First part of a long regex(?# | |
3266 | )remaining part/ | |
3267 | ||
3268 | =item C<(?adlupimnsx-imnsx)> | |
3269 | ||
3270 | =item C<(?^alupimnsx)> | |
2321 | 3271 | X<(?)> X<(?^)> |
2322 | 3272 | |
2323 | 3273 | =begin original |
2324 | 3274 | |
2325 | ||
3275 | Zero or more embedded pattern-match modifiers, to be turned on (or | |
2326 | turned off | |
3276 | turned off if preceded by C<"-">) for the remainder of the pattern or | |
2327 | 3277 | the remainder of the enclosing pattern group (if any). |
2328 | 3278 | |
2329 | 3279 | =end original |
2330 | 3280 | |
2331 | ||
3281 | ゼロ以上のパターンマッチング修飾子; | |
2332 | 3282 | パターンの残りまたは(もしあれば)包含しているパターングループの残りで |
2333 | 有効にする(または C<-> が前置されていれば解除する)。 | |
3283 | 有効にする(または C<"-"> が前置されていれば解除する)。 | |
2334 | 3284 | |
2335 | 3285 | =begin original |
2336 | 3286 | |
2337 | This is particularly useful for dynamic patterns, | |
3287 | This is particularly useful for dynamically-generated patterns, | |
3288 | such as those read in from a | |
2338 | 3289 | configuration file, taken from an argument, or specified in a table |
2339 | 3290 | somewhere. Consider the case where some patterns want to be |
2340 | 3291 | case-sensitive and some do not: The case-insensitive ones merely need to |
2341 | 3292 | include C<(?i)> at the front of the pattern. For example: |
2342 | 3293 | |
2343 | 3294 | =end original |
2344 | 3295 | |
2345 | 3296 | これは設定ファイルから読む、引数から取る、どこかのテーブルで |
2346 | 指定されている箇所から | |
3297 | 指定されている箇所から動的生成されたパターンを使うときに特に便利です。 | |
2347 | 3298 | パターンの一部では大文字小文字を区別したいけれども別の箇所では |
2348 | 3299 | 区別しないといったケースを考えてみます: 区別をしない場所では |
2349 | 3300 | 単にパターンの先頭に C<(?i)> を含めるだけです。 |
2350 | 3301 | 例えば: |
2351 | 3302 | |
2352 | 3303 | $pattern = "foobar"; |
2353 | 3304 | if ( /$pattern/i ) { } |
2354 | 3305 | |
2355 | 3306 | # more flexible: |
2356 | 3307 | |
2357 | 3308 | $pattern = "(?i)foobar"; |
2358 | 3309 | if ( /$pattern/ ) { } |
2359 | 3310 | |
2360 | 3311 | =begin original |
2361 | 3312 | |
2362 | 3313 | These modifiers are restored at the end of the enclosing group. For example, |
2363 | 3314 | |
2364 | 3315 | =end original |
2365 | 3316 | |
2366 | 3317 | これらの修飾子は包含しているグループの最後で復元(restore)されます。 |
2367 | 3318 | 例えば、 |
2368 | 3319 | |
2369 | 3320 | ( (?i) blah ) \s+ \g1 |
2370 | 3321 | |
2371 | 3322 | =begin original |
2372 | 3323 | |
2373 | 3324 | will match C<blah> in any case, some spaces, and an exact (I<including the case>!) |
2374 | 3325 | repetition of the previous word, assuming the C</x> modifier, and no C</i> |
2375 | 3326 | modifier outside this group. |
2376 | 3327 | |
2377 | 3328 | =end original |
2378 | 3329 | |
2379 | 3330 | は C<blah> に大文字小文字の区別なくマッチングし、 |
2380 | 3331 | いくつかの空白、そして前の単語その物(I<大文字小文字の区別まで含めて>!)に |
2381 | 3332 | 再度マッチングします; ここではこのグループの外側で C</x> 修飾子を持ち、 |
2382 | 3333 | C</i> 修飾子を持たないものとします。 |
2383 | 3334 | |
2384 | 3335 | =begin original |
2385 | 3336 | |
2386 | 3337 | These modifiers do not carry over into named subpatterns called in the |
2387 | enclosing group. In other words, a pattern such as C<((?i)(?&NAME))> does not | |
3338 | enclosing group. In other words, a pattern such as C<((?i)(?&I<NAME>))> does not | |
2388 | change the case-sensitivity of the | |
3339 | change the case-sensitivity of the I<NAME> pattern. | |
2389 | 3340 | |
2390 | 3341 | =end original |
2391 | 3342 | |
2392 | 3343 | これらの修飾子は囲まれたグループで呼び出された名前付き部分パターンには |
2393 | 3344 | 持ち越されません。 |
2394 | 言い換えると、C<((?i)(?&NAME))> のようなパターンは、 | |
3345 | 言い換えると、C<((?i)(?&I<NAME>))> のようなパターンは、 | |
2395 | 大文字小文字を認識するのを変更しません。 | |
3346 | I<NAME> パターンが大文字小文字を認識するのを変更しません。 | |
2396 | 3347 | |
2397 | 3348 | =begin original |
2398 | 3349 | |
3350 | A modifier is overridden by later occurrences of this construct in the | |
3351 | same scope containing the same modifier, so that | |
3352 | ||
3353 | =end original | |
3354 | ||
3355 | 修飾子は、同じスコープ内で同じ修飾子を含む | |
3356 | この構文が後に出現すると上書きされるので: | |
3357 | ||
3358 | /((?im)foo(?-m)bar)/ | |
3359 | ||
3360 | =begin original | |
3361 | ||
3362 | matches all of C<foobar> case insensitively, but uses C</m> rules for | |
3363 | only the C<foo> portion. The C<"a"> flag overrides C<aa> as well; | |
3364 | likewise C<aa> overrides C<"a">. The same goes for C<"x"> and C<xx>. | |
3365 | Hence, in | |
3366 | ||
3367 | =end original | |
3368 | ||
3369 | は、C<foobar> のすべてに大文字小文字を区別せずにマッチングしますが、 | |
3370 | C<foo> 部分のみに C</m> の規則を使います。 | |
3371 | C<"a"> フラグは C<aa> も上書きします; | |
3372 | 同様に、C<aa> も C<"a">を上書きします。 | |
3373 | 同じことが C<"x"> と C<xx> についても当てはまります。 | |
3374 | したがって: | |
3375 | ||
3376 | /(?-x)foo/xx | |
3377 | ||
3378 | =begin original | |
3379 | ||
3380 | both C</x> and C</xx> are turned off during matching C<foo>. And in | |
3381 | ||
3382 | =end original | |
3383 | ||
3384 | C</x> と C</xx> は両方とも、C<foo> のマッチング中はオフになります。 | |
3385 | そして: | |
3386 | ||
3387 | /(?x)foo/x | |
3388 | ||
3389 | =begin original | |
3390 | ||
3391 | C</x> but NOT C</xx> is turned on for matching C<foo>. (One might | |
3392 | mistakenly think that since the inner C<(?x)> is already in the scope of | |
3393 | C</x>, that the result would effectively be the sum of them, yielding | |
3394 | C</xx>. It doesn't work that way.) Similarly, doing something like | |
3395 | C<(?xx-x)foo> turns off all C<"x"> behavior for matching C<foo>, it is not | |
3396 | that you subtract 1 C<"x"> from 2 to get 1 C<"x"> remaining. | |
3397 | ||
3398 | =end original | |
3399 | ||
3400 | C<foo> にマッチングする間、C</xx> ではなく C</x> がオンになります。 | |
3401 | (内部 C<(?x)> はすでに C</x> の範囲内にあるので、結果は実質的に | |
3402 | それらの和になり、C</xx> が生成されると誤って考えるかもしれません。 | |
3403 | そうはなりません。) | |
3404 | 同様に、C<(?xx-x)foo> のようなことをすると、C<foo> とマッチングする間 | |
3405 | C<"x"> の動作がすべてオフになります; | |
3406 | 二つの C<"x"> から一つ引いて一つの C<"x"> が残るということにはなりません。 | |
3407 | ||
3408 | =begin original | |
3409 | ||
2399 | 3410 | Any of these modifiers can be set to apply globally to all regular |
2400 | 3411 | expressions compiled within the scope of a C<use re>. See |
2401 | 3412 | L<re/"'/flags' mode">. |
2402 | 3413 | |
2403 | 3414 | =end original |
2404 | 3415 | |
2405 | 3416 | それらの変更のどれもセットでき、C<use re>のスコープ内でグローバルに |
2406 | 3417 | 全てのコンパイルされた正規表現に適用されます。L<re/"'/flags' mode"> |
2407 | 3418 | を見てください。 |
2408 | 3419 | |
2409 | 3420 | =begin original |
2410 | 3421 | |
2411 | 3422 | 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 | |
3423 | after the C<"?"> is a shorthand equivalent to C<d-imnsx>. Flags (except | |
2413 | 3424 | C<"d">) may follow the caret to override it. |
2414 | 3425 | But a minus sign is not legal with it. |
2415 | 3426 | |
2416 | 3427 | =end original |
2417 | 3428 | |
2418 | 3429 | Perl 5.14から、C<"^">(キャレットか曲折アクセント)がC<"?">のすぐ |
2419 | 後ろにつくと、C<d-imsx>と同じになります。フラグ(C<"d">以外の) | |
3430 | 後ろにつくと、C<d-imnsx> と同じになります。フラグ(C<"d">以外の) | |
2420 | 3431 | をキャレットに続けることで、上書きできます。 |
2421 | 3432 | ですが、マイナス記号は一緒に使えません。 |
2422 | 3433 | |
2423 | 3434 | =begin original |
2424 | 3435 | |
2425 | Note that the C<a>, C<d>, C<l>, C<p>, and C<u> modifiers are special in | |
3436 | 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 | |
3437 | 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 | |
3438 | 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 | |
3439 | others, and a maximum of one (or two C<"a">'s) may appear in the | |
2429 | 3440 | construct. Thus, for |
2430 | 3441 | example, C<(?-p)> will warn when compiled under C<use warnings>; |
2431 | 3442 | C<(?-d:...)> and C<(?dl:...)> are fatal errors. |
2432 | 3443 | |
2433 | 3444 | =end original |
2434 | 3445 | |
2435 | C<a>, C<d>, C<l>, C<p>, C<u> 修飾子は有効にできるのみで | |
3446 | C<"a">, C<"d">, C<"l">, C<"p">, C<"u"> 修飾子は有効にできるのみで、 | |
2436 | そして C<a>, C<d>, C<l>, C<u> 修飾子は | |
3447 | 無効にはできない点、 そして C<"a">, C<"d">, C<"l">, C<"u"> 修飾子は | |
3448 | 互いに排他であるという点で特別です: | |
2437 | 3449 | 一つを指定すると他のものの指定を解除し、構文中に最大で一つ (または二つの |
2438 | C<a>) だけが現れます。 | |
3450 | C<"a">) だけが現れます。 | |
2439 | 3451 | 従って 例えば C<(?-p)> は C<use warnings> の下でコンパイルされると |
2440 | 3452 | 警告を発します; C<(?-d:...)> と C<(?dl:...)> は致命的エラーです。 |
2441 | 3453 | |
2442 | 3454 | =begin original |
2443 | 3455 | |
2444 | Note also that the C<p> modifier is special in that its presence | |
3456 | Note also that the C<"p"> modifier is special in that its presence | |
2445 | 3457 | anywhere in a pattern has a global effect. |
2446 | 3458 | |
2447 | 3459 | =end original |
2448 | 3460 | |
2449 | 3461 | パターン中のどこにあってもグローバルな影響があるという意味で |
2450 | C<p> 修飾子が特別であることにも注意してください。 | |
3462 | C<"p"> 修飾子が特別であることにも注意してください。 | |
2451 | 3463 | |
2452 | = | |
3464 | =begin original | |
3465 | ||
3466 | Having zero modifiers makes this a no-op (so why did you specify it, | |
3467 | unless it's generated code), and starting in v5.30, warns under L<C<use | |
3468 | re 'strict'>|re/'strict' mode>. | |
3469 | ||
3470 | =end original | |
3471 | ||
3472 | 修飾子がない場合、これは何もせず (なので、生成されたコードでない場合、 | |
3473 | なぜこれを指定したのでしょう?)、v5.30 から、 | |
3474 | L<C<use re 'strict'>|re/'strict' mode> の警告が出ます。 | |
3475 | ||
3476 | =item C<(?:I<pattern>)> | |
2453 | 3477 | X<(?:)> |
2454 | 3478 | |
2455 | =item C<(?adluimsx-imsx:pattern)> | |
3479 | =item C<(?adluimnsx-imnsx:I<pattern>)> | |
2456 | 3480 | |
2457 | =item C<(?^aluimsx:pattern)> | |
3481 | =item C<(?^aluimnsx:I<pattern>)> | |
2458 | 3482 | X<(?^:)> |
2459 | 3483 | |
2460 | 3484 | =begin original |
2461 | 3485 | |
2462 | 3486 | This is for clustering, not capturing; it groups subexpressions like |
2463 | "()", but doesn't make backreferences as "()" does. So | |
3487 | C<"()">, but doesn't make backreferences as C<"()"> does. So | |
2464 | 3488 | |
2465 | 3489 | =end original |
2466 | 3490 | |
2467 | これはキャプチャではなくクラスタです; これは "()" のように部分式を | |
3491 | これはキャプチャではなくクラスタです; これは C<"()"> のように部分式を | |
2468 | グループ化しますが "()" が行うような後方参照は行いません。 | |
3492 | グループ化しますが C<"()"> が行うような後方参照は行いません。 | |
2469 | 3493 | つまり、 |
2470 | 3494 | |
2471 | 3495 | @fields = split(/\b(?:a|b|c)\b/) |
2472 | 3496 | |
2473 | 3497 | =begin original |
2474 | 3498 | |
2475 | ||
3499 | matches the same field delimiters as | |
2476 | 3500 | |
2477 | 3501 | =end original |
2478 | 3502 | |
2479 | ||
3503 | 次と同じフィールド区切り文字にマッチングしますが: | |
2480 | 3504 | |
2481 | 3505 | @fields = split(/\b(a|b|c)\b/) |
2482 | 3506 | |
2483 | 3507 | =begin original |
2484 | 3508 | |
2485 | but doesn't spit out | |
3509 | but doesn't spit out the delimiters themselves as extra fields (even though | |
3510 | that's the behaviour of L<perlfunc/split> when its pattern contains capturing | |
3511 | groups). It's also cheaper not to capture | |
2486 | 3512 | characters if you don't need to. |
2487 | 3513 | |
2488 | 3514 | =end original |
2489 | 3515 | |
2490 | ||
3516 | (例えそれが捕捉グループを含むときの L<perlfunc/split> の振る舞いで | |
3517 | あったとしても) 区切り文字自身を余計なフィールドとして引き出しません。 | |
2491 | 3518 | また不要であれば文字のキャプチャを行わないため低コストです。 |
2492 | 3519 | |
2493 | 3520 | =begin original |
2494 | 3521 | |
2495 | Any letters between C<?> and C<:> act as flags modifiers as with | |
3522 | Any letters between C<"?"> and C<":"> act as flags modifiers as with | |
2496 | C<(?adluimsx-imsx)>. For example, | |
3523 | C<(?adluimnsx-imnsx)>. For example, | |
2497 | 3524 | |
2498 | 3525 | =end original |
2499 | 3526 | |
2500 | C<?> 及び C<:> の間の文字は C<(?adluimsx-imsx)> のような | |
3527 | C<"?"> 及び C<":"> の間の文字は C<(?adluimnsx-imnsx)> のような | |
2501 | 動作します。 | |
3528 | フラグ修飾子として動作します。 | |
2502 | 3529 | 例えば、 |
2503 | 3530 | |
2504 | 3531 | /(?s-i:more.*than).*million/i |
2505 | 3532 | |
2506 | 3533 | =begin original |
2507 | 3534 | |
2508 | 3535 | is equivalent to the more verbose |
2509 | 3536 | |
2510 | 3537 | =end original |
2511 | 3538 | |
2512 | 3539 | はより冗長に書けば以下と等価です |
2513 | 3540 | |
2514 | 3541 | /(?:(?s-i)more.*than).*million/i |
2515 | 3542 | |
2516 | 3543 | =begin original |
2517 | 3544 | |
3545 | Note that any C<()> constructs enclosed within this one will still | |
3546 | capture unless the C</n> modifier is in effect. | |
3547 | ||
3548 | =end original | |
3549 | ||
3550 | これの中の C<()> 構文は、C</n> が有効でない限りまだ捕捉することに | |
3551 | 注意してください。 | |
3552 | ||
3553 | =begin original | |
3554 | ||
3555 | Like the L</(?adlupimnsx-imnsx)> construct, C<aa> and C<"a"> override each | |
3556 | other, as do C<xx> and C<"x">. They are not additive. So, doing | |
3557 | something like C<(?xx-x:foo)> turns off all C<"x"> behavior for matching | |
3558 | C<foo>. | |
3559 | ||
3560 | =end original | |
3561 | ||
3562 | L</(?adlupimnsx-imnsx)> 構文と同様、C<xx> と | |
3563 | C<"x">と同様に、C<aa> と C<"a"> は互いに上書きします。 | |
3564 | これらは加法的ではありません。 | |
3565 | したがって、C<(?xx-x:foo)> のようなことを行うと、 | |
3566 | C<foo> にマッチングする間、C<"x"> の動作がすべてオフになります。 | |
3567 | ||
3568 | =begin original | |
3569 | ||
2518 | 3570 | 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 | |
3571 | after the C<"?"> is a shorthand equivalent to C<d-imnsx>. Any positive | |
2520 | 3572 | flags (except C<"d">) may follow the caret, so |
2521 | 3573 | |
2522 | 3574 | =end original |
2523 | 3575 | |
2524 | Perl 5.14から、C<"^">(キャレット | |
3576 | Perl 5.14 から、C<"^">(キャレットあるいは曲折アクセント)が C<"?"> のすぐ | |
2525 | 後ろにつくと、C<d-imsx>と同じになります。 | |
3577 | 後ろにつくと、C<d-imnsx> と同じになります。 | |
2526 | どのような肯定のフラグ(C<"d">以外の)もキャレットに続けることができます; | |
3578 | どのような肯定のフラグ(C<"d"> 以外の)もキャレットに続けることができます; | |
2527 | 3579 | そのため、 |
2528 | 3580 | |
2529 | 3581 | (?^x:foo) |
2530 | 3582 | |
2531 | 3583 | =begin original |
2532 | 3584 | |
2533 | 3585 | is equivalent to |
2534 | 3586 | |
2535 | 3587 | =end original |
2536 | 3588 | |
2537 | 3589 | は、以下と同じになります。 |
2538 | 3590 | |
2539 | (?x-ims:foo) | |
3591 | (?x-imns:foo) | |
2540 | 3592 | |
2541 | 3593 | =begin original |
2542 | 3594 | |
2543 | 3595 | 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>), | |
3596 | surrounding pattern, but uses the system defaults (C<d-imnsx>), | |
2545 | 3597 | modified by any flags specified. |
2546 | 3598 | |
2547 | 3599 | =end original |
2548 | 3600 | |
2549 | 3601 | キャレットは、Perlにこのクラスターはパターンの周りのどのフラグも |
2550 | 引き継がずに、代わりに、システムのデフォルトのフラグ(C<d-imsx>) | |
3602 | 引き継がずに、代わりに、システムのデフォルトのフラグ (C<d-imnsx>) | |
2551 | 3603 | を使うことを教えます; 指定されている他のフラグによって変更されます。 |
2552 | 3604 | |
2553 | 3605 | =begin original |
2554 | 3606 | |
2555 | 3607 | The caret allows for simpler stringification of compiled regular |
2556 | 3608 | expressions. These look like |
2557 | 3609 | |
2558 | 3610 | =end original |
2559 | 3611 | |
2560 | 3612 | キャレットはより簡単なコンパイルされた正規表現の文字列化をすることが |
2561 | 3613 | できます。次のものは |
2562 | 3614 | |
2563 | 3615 | (?^:pattern) |
2564 | 3616 | |
2565 | 3617 | =begin original |
2566 | 3618 | |
2567 | 3619 | with any non-default flags appearing between the caret and the colon. |
2568 | 3620 | A test that looks at such stringification thus doesn't need to have the |
2569 | 3621 | system default flags hard-coded in it, just the caret. If new flags are |
2570 | 3622 | added to Perl, the meaning of the caret's expansion will change to include |
2571 | 3623 | the default for those flags, so the test will still work, unchanged. |
2572 | 3624 | |
2573 | 3625 | =end original |
2574 | 3626 | |
2575 | 3627 | キャレットとコロンの間には、デフォルトでないフラグがありません。 |
2576 | 3628 | このような文字列化を見るテストには、したがって、システムのデフォルトのフラグを |
2577 | 3629 | その中にハードコードする必要はなく、ただキャレットを使います。新しいフラグが |
2578 | 3630 | Perlに追加されたら、キャレットを展開した意味はそれらのフラグのためのデフォルトを |
2579 | 3631 | 含むように変更されます; そのために、このテストは、それでも何も変えずに |
2580 | 3632 | 動くでしょう。 |
2581 | 3633 | |
2582 | 3634 | =begin original |
2583 | 3635 | |
2584 | 3636 | Specifying a negative flag after the caret is an error, as the flag is |
2585 | 3637 | redundant. |
2586 | 3638 | |
2587 | 3639 | =end original |
2588 | 3640 | |
2589 | 3641 | キャレットの後ろに否定のフラグを付けると、余計なフラグであるとして、 |
2590 | 3642 | エラーになります。 |
2591 | 3643 | |
2592 | 3644 | =begin original |
2593 | 3645 | |
2594 | 3646 | Mnemonic for C<(?^...)>: A fresh beginning since the usual use of a caret is |
2595 | 3647 | to match at the beginning. |
2596 | 3648 | |
2597 | 3649 | =end original |
2598 | 3650 | |
2599 | 3651 | C<(?^...)>の覚え方: 新鮮な開始; 通常のキャレットの使い方は文字列の開始の |
2600 | 3652 | マッチなので。 |
2601 | 3653 | |
2602 | =item C<(?|pattern)> | |
3654 | =item C<(?|I<pattern>)> | |
2603 | 3655 | X<(?|)> X<Branch reset> |
2604 | 3656 | |
2605 | 3657 | =begin original |
2606 | 3658 | |
2607 | 3659 | This is the "branch reset" pattern, which has the special property |
2608 | 3660 | that the capture groups are numbered from the same starting point |
2609 | 3661 | in each alternation branch. It is available starting from perl 5.10.0. |
2610 | 3662 | |
2611 | 3663 | =end original |
2612 | 3664 | |
2613 | 3665 | これは各代替分岐において捕捉グループを同じ番号から始める特殊な |
2614 | 3666 | 属性を持っている、「ブランチリセット(branch reset)」パターンです。 |
2615 | 3667 | これは perl 5.10.0 から提供されています。 |
2616 | 3668 | |
2617 | 3669 | =begin original |
2618 | 3670 | |
2619 | 3671 | Capture groups are numbered from left to right, but inside this |
2620 | 3672 | construct the numbering is restarted for each branch. |
2621 | 3673 | |
2622 | 3674 | =end original |
2623 | 3675 | |
2624 | 3676 | 捕捉グループは左から右へと番号が振られますが、この構成子の内側では |
2625 | 3677 | 各分岐毎に番号はリセットされます。 |
2626 | 3678 | |
2627 | 3679 | =begin original |
2628 | 3680 | |
2629 | 3681 | The numbering within each branch will be as normal, and any groups |
2630 | 3682 | following this construct will be numbered as though the construct |
2631 | 3683 | contained only one branch, that being the one with the most capture |
2632 | 3684 | groups in it. |
2633 | 3685 | |
2634 | 3686 | =end original |
2635 | 3687 | |
2636 | 3688 | 各分岐内での番号付けは通常通りに行われ、この構成子の後に続くグループは |
2637 | 3689 | その中で捕捉グループが一番多かった分岐のみが |
2638 | 3690 | 格納されていたかのように番号付けされていきます。 |
2639 | 3691 | |
2640 | 3692 | =begin original |
2641 | 3693 | |
2642 | 3694 | This construct is useful when you want to capture one of a |
2643 | 3695 | number of alternative matches. |
2644 | 3696 | |
2645 | 3697 | =end original |
2646 | 3698 | |
2647 | 3699 | この構成子はいくつかの代替マッチングの1つを捕捉したいときに便利です。 |
2648 | 3700 | |
2649 | 3701 | =begin original |
2650 | 3702 | |
2651 | 3703 | Consider the following pattern. The numbers underneath show in |
2652 | 3704 | which group the captured content will be stored. |
2653 | 3705 | |
2654 | 3706 | =end original |
2655 | 3707 | |
2656 | 3708 | 以下のパターンを想像してみてください。 |
2657 | 3709 | 下側の番号は内容の格納されるグループを示します。 |
2658 | 3710 | |
2659 | # before ---------------branch-reset----------- after | |
3711 | # before ---------------branch-reset----------- after | |
2660 | 3712 | / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
2661 | # 1 2 2 3 2 3 4 | |
3713 | # 1 2 2 3 2 3 4 | |
2662 | 3714 | |
2663 | 3715 | =begin original |
2664 | 3716 | |
2665 | Be careful when using the branch reset pattern in combination with | |
3717 | Be careful when using the branch reset pattern in combination with | |
2666 | named captures. Named captures are implemented as being aliases to | |
3718 | named captures. Named captures are implemented as being aliases to | |
2667 | 3719 | numbered groups holding the captures, and that interferes with the |
2668 | 3720 | implementation of the branch reset pattern. If you are using named |
2669 | 3721 | captures in a branch reset pattern, it's best to use the same names, |
2670 | 3722 | in the same order, in each of the alternations: |
2671 | 3723 | |
2672 | 3724 | =end original |
2673 | 3725 | |
2674 | 3726 | 名前付き捕捉と枝リセットパターンを組み合わせて使うときには注意してください。 |
2675 | 3727 | 名前付き捕捉は捕捉を保持している番号付きグループへの別名として |
2676 | 3728 | 実装されていて、枝リセットパターンの実装を妨害します。 |
2677 | 3729 | 枝リセットパターンで名前付き捕捉を使うときは、それぞれの代替で |
2678 | 3730 | 同じ名前を同じ順番で使うのが最良です: |
2679 | 3731 | |
2680 | 3732 | /(?| (?<a> x ) (?<b> y ) |
2681 | 3733 | | (?<a> z ) (?<b> w )) /x |
2682 | 3734 | |
2683 | 3735 | =begin original |
2684 | 3736 | |
2685 | 3737 | Not doing so may lead to surprises: |
2686 | 3738 | |
2687 | 3739 | =end original |
2688 | 3740 | |
2689 | 3741 | そうしないと驚くことになります: |
2690 | 3742 | |
2691 | 3743 | "12" =~ /(?| (?<a> \d+ ) | (?<b> \D+))/x; |
2692 | say $+ | |
3744 | say $+{a}; # Prints '12' | |
2693 | say $+ | |
3745 | say $+{b}; # *Also* prints '12'. | |
2694 | 3746 | |
2695 | 3747 | =begin original |
2696 | 3748 | |
2697 | 3749 | The problem here is that both the group named C<< a >> and the group |
2698 | 3750 | named C<< b >> are aliases for the group belonging to C<< $1 >>. |
2699 | 3751 | |
2700 | 3752 | =end original |
2701 | 3753 | |
2702 | 3754 | ここでの問題は、C<< a >> という名前のグループと C<< b >> という名前の |
2703 | 3755 | が両方ともグループ C<< $1 >> への別名であることです。 |
2704 | 3756 | |
2705 | =item Look | |
3757 | =item Lookaround Assertions | |
2706 | 3758 | X<look-around assertion> X<lookaround assertion> X<look-around> X<lookaround> |
2707 | 3759 | |
2708 | 3760 | =begin original |
2709 | 3761 | |
2710 | Look | |
3762 | Lookaround assertions are zero-width patterns which match a specific | |
2711 | 3763 | pattern without including it in C<$&>. Positive assertions match when |
2712 | 3764 | their subpattern matches, negative assertions match when their subpattern |
2713 | fails. Look | |
3765 | fails. Lookbehind matches text up to the current match position, | |
2714 | look | |
3766 | lookahead matches text following the current match position. | |
2715 | 3767 | |
2716 | 3768 | =end original |
2717 | 3769 | |
2718 | 3770 | 先読み及び後読みの言明(assertion)は C<$&> の中に |
2719 | 3771 | 含めない特定のパターンにマッチングするゼロ幅のパターンです。 |
2720 | 3772 | 正の言明はその部分パターンがマッチングしたときにマッチングし、 |
2721 | 3773 | 負の言明はその部分パターンが失敗したときにマッチングします。 |
2722 | 3774 | 後読みのマッチングは今のマッチング位置までのテキストにマッチングし、 |
2723 | 3775 | 先読みの言明は今のマッチング位置の先にあるテキストにマッチングします。 |
2724 | 3776 | |
2725 | 3777 | =over 4 |
2726 | 3778 | |
2727 | =item C<(?=pattern)> | |
3779 | =item C<(?=I<pattern>)> | |
2728 | X<(?=)> X<look-ahead, positive> X<lookahead, positive> | |
2729 | 3780 | |
3781 | =item C<(*pla:I<pattern>)> | |
3782 | ||
3783 | =item C<(*positive_lookahead:I<pattern>)> | |
3784 | X<(?=)> | |
3785 | X<(*pla> | |
3786 | X<(*positive_lookahead> | |
3787 | X<look-ahead, positive> X<lookahead, positive> | |
3788 | ||
2730 | 3789 | =begin original |
2731 | 3790 | |
2732 | A zero-width positive look | |
3791 | A zero-width positive lookahead assertion. For example, C</\w+(?=\t)/> | |
2733 | 3792 | matches a word followed by a tab, without including the tab in C<$&>. |
2734 | 3793 | |
2735 | 3794 | =end original |
2736 | 3795 | |
2737 | 3796 | ゼロ幅の正の先読み言明。 |
2738 | 3797 | 例えば、C</\w+(?=\t)/> はタブが続く単語にマッチングしますが、タブは |
2739 | 3798 | C<$&> に含まれません。 |
2740 | 3799 | |
2741 | = | |
3800 | =begin original | |
2742 | X<(?!)> X<look-ahead, negative> X<lookahead, negative> | |
2743 | 3801 | |
3802 | The alphabetic forms are experimental; using them yields a warning in the | |
3803 | C<experimental::alpha_assertions> category. | |
3804 | ||
3805 | =end original | |
3806 | ||
3807 | 英字形式は実験的です; これらを使うと | |
3808 | C<experimental::alpha_assertions> カテゴリの警告が出ます。 | |
3809 | ||
3810 | =item C<(?!I<pattern>)> | |
3811 | ||
3812 | =item C<(*nla:I<pattern>)> | |
3813 | ||
3814 | =item C<(*negative_lookahead:I<pattern>)> | |
3815 | X<(?!)> | |
3816 | X<(*nla> | |
3817 | X<(*negative_lookahead> | |
3818 | X<look-ahead, negative> X<lookahead, negative> | |
3819 | ||
2744 | 3820 | =begin original |
2745 | 3821 | |
2746 | A zero-width negative look | |
3822 | A zero-width negative lookahead assertion. For example C</foo(?!bar)/> | |
2747 | 3823 | matches any occurrence of "foo" that isn't followed by "bar". Note |
2748 | however that look | |
3824 | however that lookahead and lookbehind are NOT the same thing. You cannot | |
2749 | use this for look | |
3825 | use this for lookbehind. | |
2750 | 3826 | |
2751 | 3827 | =end original |
2752 | 3828 | |
2753 | 3829 | ゼロ幅の負の先読み言明。 |
2754 | 3830 | 例えば C</foo(?!bar)/> は "bar" が続かない全ての "foo" にマッチングします。 |
2755 | 3831 | しかしながら先読みと後読みは同じ物では ない点に注意してください。 |
2756 | 3832 | これを後読みに使うことはできません。 |
2757 | 3833 | |
2758 | 3834 | =begin original |
2759 | 3835 | |
2760 | 3836 | If you are looking for a "bar" that isn't preceded by a "foo", C</(?!foo)bar/> |
2761 | 3837 | will not do what you want. That's because the C<(?!foo)> is just saying that |
2762 | 3838 | the next thing cannot be "foo"--and it's not, it's a "bar", so "foobar" will |
2763 | match. Use look | |
3839 | match. Use lookbehind instead (see below). | |
2764 | 3840 | |
2765 | 3841 | =end original |
2766 | 3842 | |
2767 | 3843 | もし "foo" が前にない "bar" を探しているのなら、 |
2768 | 3844 | C</(?!foo)bar/> では欲しい物にはなりません。 |
2769 | 3845 | なぜなら C<(?!foo)> は次のものが "foo" ではないとだけいっているのです -- |
2770 | 3846 | そしてそうではなく、そこには "bar" があるので、"foobar" はマッチングします。 |
2771 | 3847 | (後述の) 後方参照を使ってください。 |
2772 | 3848 | |
2773 | = | |
3849 | =begin original | |
2774 | X<(?<=)> X<look-behind, positive> X<lookbehind, positive> X<\K> | |
2775 | 3850 | |
3851 | The alphabetic forms are experimental; using them yields a warning in the | |
3852 | C<experimental::alpha_assertions> category. | |
3853 | ||
3854 | =end original | |
3855 | ||
3856 | 英字形式は実験的です; これらを使うと | |
3857 | C<experimental::alpha_assertions> カテゴリの警告が出ます。 | |
3858 | ||
3859 | =item C<(?<=I<pattern>)> | |
3860 | ||
3861 | =item C<\K> | |
3862 | ||
3863 | =item C<(*plb:I<pattern>)> | |
3864 | ||
3865 | =item C<(*positive_lookbehind:I<pattern>)> | |
3866 | X<(?<=)> | |
3867 | X<(*plb> | |
3868 | X<(*positive_lookbehind> | |
3869 | X<look-behind, positive> X<lookbehind, positive> X<\K> | |
3870 | ||
2776 | 3871 | =begin original |
2777 | 3872 | |
2778 | A zero-width positive look | |
3873 | A zero-width positive lookbehind assertion. For example, C</(?<=\t)\w+/> | |
2779 | 3874 | matches a word that follows a tab, without including the tab in C<$&>. |
2780 | Works only for fixed-width look-behind. | |
2781 | 3875 | |
2782 | 3876 | =end original |
2783 | 3877 | |
2784 | 3878 | ゼロ幅の正の後読みの言明。 |
2785 | 3879 | 例えば、C</(?<=\t)\w+/> は |
2786 | 3880 | タブに続く単語にマッチングしますが、タブは $& に 含まれません。 |
2787 | 固定幅の後読みのみが動作します。 | |
2788 | 3881 | |
2789 | 3882 | =begin original |
2790 | 3883 | |
2791 | ||
3884 | Prior to Perl 5.30, it worked only for fixed-width lookbehind, but | |
3885 | starting in that release, it can handle variable lengths from 1 to 255 | |
3886 | characters as an experimental feature. The feature is enabled | |
3887 | automatically if you use a variable length lookbehind assertion, but | |
3888 | will raise a warning at pattern compilation time, unless turned off, in | |
3889 | the C<experimental::vlb> category. This is to warn you that the exact | |
3890 | behavior is subject to change should feedback from actual use in the | |
3891 | field indicate to do so; or even complete removal if the problems found | |
3892 | are not practically surmountable. You can achieve close to pre-5.30 | |
3893 | behavior by fatalizing warnings in this category. | |
3894 | ||
3895 | =end original | |
3896 | ||
3897 | Perl 5.30 より前では、固定幅の後読みのみが動作しますが、このリリースからは、 | |
3898 | 実験的な機能として 1 から 255 文字の可変長を処理できます。 | |
3899 | 可変長の後読みの言明を使用すると、この機能は自動的に有効になりますが、 | |
3900 | C<experimental::vlb> カテゴリでオフにしない限り、パターンのコンパイル時に | |
3901 | 警告が表示されます。 | |
3902 | これは、フィールドでの実際の使用からのフィードバックが変更することを | |
3903 | 示していた場合、正確な動作が変更される可能性があることを警告するためです; | |
3904 | または、検出された問題が実際に解決できない場合は完全に | |
3905 | 削除されることもあります。 | |
3906 | このカテゴリの警告を致命的エラーにすることによって、5.30 以前に | |
3907 | 近い振る舞いを実現できます。 | |
3908 | ||
3909 | =begin original | |
3910 | ||
3911 | There is a special form of this construct, called C<\K> | |
3912 | (available since Perl 5.10.0), which causes the | |
2792 | 3913 | regex engine to "keep" everything it had matched prior to the C<\K> and |
2793 | not include it in C<$&>. This effectively provides | |
3914 | not include it in C<$&>. This effectively provides non-experimental | |
2794 | look | |
3915 | variable-length lookbehind of any length. | |
2795 | is allowed, but the behaviour is currently not well defined. | |
2796 | 3916 | |
2797 | 3917 | =end original |
2798 | 3918 | |
2799 | C<\K> というこの構成子の特殊な形式もあります | |
3919 | (Perl 5.10.0 から利用可能な) C<\K> というこの構成子の特殊な形式もあります; | |
2800 | 3920 | これは正規表現エンジンに対してそれが C<\K> までにマッチングした |
2801 | 3921 | すべてのものを"取っておいて"、C<$&> には含めないようにさせます。 |
2802 | これは事実上可変長の後読みを提供します。 | |
3922 | これは事実上実験的でない任意の長さの可変長の後読みを提供します。 | |
3923 | ||
3924 | =begin original | |
3925 | ||
3926 | And, there is a technique that can be used to handle variable length | |
3927 | lookbehinds on earlier releases, and longer than 255 characters. It is | |
3928 | described in | |
3929 | L<http://www.drregex.com/2019/02/variable-length-lookbehinds-actually.html>. | |
3930 | ||
3931 | =end original | |
3932 | ||
3933 | そして、以前のリリースや、255 文字より長い可変長後ろ読みを扱うのに使える | |
3934 | テクニックがあります。 | |
3935 | これは | |
3936 | L<http://www.drregex.com/2019/02/variable-length-lookbehinds-actually.html> に | |
3937 | 記述されています。 | |
3938 | ||
3939 | =begin original | |
3940 | ||
3941 | Note that under C</i>, a few single characters match two or three other | |
3942 | characters. This makes them variable length, and the 255 length applies | |
3943 | to the maximum number of characters in the match. For | |
3944 | example C<qr/\N{LATIN SMALL LETTER SHARP S}/i> matches the sequence | |
3945 | C<"ss">. Your lookbehind assertion could contain 127 Sharp S | |
3946 | characters under C</i>, but adding a 128th would generate a compilation | |
3947 | error, as that could match 256 C<"s"> characters in a row. | |
3948 | ||
3949 | =end original | |
3950 | ||
3951 | C</i> の下では、いくつかの単一の文字が 2 または 3 の他の文字に | |
3952 | マッチングすることに注意してください。 | |
3953 | これは後読みを可変長にし、長さ 255 はマッチングの中の文字の最大数に | |
3954 | 適用されます。 | |
3955 | 例えば、C<qr/\N{LATIN SMALL LETTER SHARP S}/i> は並び C<"ss"> に | |
3956 | マッチングします。 | |
3957 | あなたの後読み言明は C</i> の下で 127 個の Sharp S 文字を含むことは | |
3958 | できますが、128 個目を追加するとコンパイルエラーが生成されます; | |
3959 | これは 256 個連続した C<"s"> 文字にマッチングすることがあるからです。 | |
3960 | ||
3961 | =begin original | |
3962 | ||
3963 | The use of C<\K> inside of another lookaround assertion | |
3964 | is allowed, but the behaviour is currently not well defined. | |
3965 | ||
3966 | =end original | |
3967 | ||
2803 | 3968 | 他の先読み及び後読みの言明の中での利用も可能ですが、その振る舞いは |
2804 | 3969 | 今のところあまり定義されていません。 |
2805 | 3970 | |
2806 | 3971 | =begin original |
2807 | 3972 | |
2808 | 3973 | For various reasons C<\K> may be significantly more efficient than the |
2809 | 3974 | equivalent C<< (?<=...) >> construct, and it is especially useful in |
2810 | 3975 | situations where you want to efficiently remove something following |
2811 | 3976 | something else in a string. For instance |
2812 | 3977 | |
2813 | 3978 | =end original |
2814 | 3979 | |
2815 | 3980 | いくつかの理由から、C<\K> は等価な C<< (?<=...) >> |
2816 | 3981 | 構成子より非常に効率的で、文字列の中で何かに続いている何かを効率的に |
2817 | 3982 | 取り除きたいようなシチュエーションで効果的に役立ちます。 |
2818 | 3983 | 例えば |
2819 | 3984 | |
2820 | 3985 | s/(foo)bar/$1/g; |
2821 | 3986 | |
2822 | 3987 | =begin original |
2823 | 3988 | |
2824 | 3989 | can be rewritten as the much more efficient |
2825 | 3990 | |
2826 | 3991 | =end original |
2827 | 3992 | |
2828 | 3993 | 次のようにより効率的に書き直せます |
2829 | 3994 | |
2830 | 3995 | s/foo\Kbar//g; |
2831 | 3996 | |
2832 | = | |
3997 | =begin original | |
2833 | X<(?<!)> X<look-behind, negative> X<lookbehind, negative> | |
2834 | 3998 | |
3999 | Use of the non-greedy modifier C<"?"> may not give you the expected | |
4000 | results if it is within a capturing group within the construct. | |
4001 | ||
4002 | =end original | |
4003 | ||
4004 | 非貪欲修飾子 C<"?"> の使用は、この構文の中の捕捉グループの中では | |
4005 | 想定される結果にならないかもしれません。 | |
4006 | ||
2835 | 4007 | =begin original |
2836 | 4008 | |
2837 | ||
4009 | The alphabetic forms (not including C<\K> are experimental; using them | |
2838 | ||
4010 | yields a warning in the C<experimental::alpha_assertions> category. | |
2839 | only for fixed-width look-behind. | |
2840 | 4011 | |
2841 | 4012 | =end original |
2842 | 4013 | |
4014 | (C<\K> を除く) 英字形式は実験的です; これらを使うと | |
4015 | C<experimental::alpha_assertions> カテゴリの警告が出ます。 | |
4016 | ||
4017 | =item C<(?<!I<pattern>)> | |
4018 | ||
4019 | =item C<(*nlb:I<pattern>)> | |
4020 | ||
4021 | =item C<(*negative_lookbehind:I<pattern>)> | |
4022 | X<(?<!)> | |
4023 | X<(*nlb> | |
4024 | X<(*negative_lookbehind> | |
4025 | X<look-behind, negative> X<lookbehind, negative> | |
4026 | ||
4027 | =begin original | |
4028 | ||
4029 | A zero-width negative lookbehind assertion. For example C</(?<!bar)foo/> | |
4030 | matches any occurrence of "foo" that does not follow "bar". | |
4031 | ||
4032 | =end original | |
4033 | ||
2843 | 4034 | ゼロ幅の負の後読みの言明。 |
2844 | 4035 | 例えば C</(?<!bar)foo/> は "bar" に続いていない任意の "foo" に |
2845 | 4036 | マッチングします。 |
2846 | 固定幅の後読みのみが動作します。 | |
2847 | 4037 | |
4038 | =begin original | |
4039 | ||
4040 | Prior to Perl 5.30, it worked only for fixed-width lookbehind, but | |
4041 | starting in that release, it can handle variable lengths from 1 to 255 | |
4042 | characters as an experimental feature. The feature is enabled | |
4043 | automatically if you use a variable length lookbehind assertion, but | |
4044 | will raise a warning at pattern compilation time, unless turned off, in | |
4045 | the C<experimental::vlb> category. This is to warn you that the exact | |
4046 | behavior is subject to change should feedback from actual use in the | |
4047 | field indicate to do so; or even complete removal if the problems found | |
4048 | are not practically surmountable. You can achieve close to pre-5.30 | |
4049 | behavior by fatalizing warnings in this category. | |
4050 | ||
4051 | =end original | |
4052 | ||
4053 | Perl 5.30 より前では、固定幅の後読みのみが動作しますが、このリリースからは、 | |
4054 | 実験的な機能として 1 から 255 文字の可変長を処理できます。 | |
4055 | 可変長の後読みの言明を使用すると、この機能は自動的に有効になりますが、 | |
4056 | C<experimental::vlb> カテゴリでオフにしない限り、パターンのコンパイル時に | |
4057 | 警告が表示されます。 | |
4058 | これは、フィールドでの実際の使用からのフィードバックが変更することを | |
4059 | 示していた場合、正確な動作が変更される可能性があることを警告するためです; | |
4060 | または、検出された問題が実際に解決できない場合は完全に | |
4061 | 削除されることもあります。 | |
4062 | このカテゴリの警告を致命的エラーにすることによって、5.30 以前に | |
4063 | 近い振る舞いを実現できます。 | |
4064 | ||
4065 | =begin original | |
4066 | ||
4067 | There is a technique that can be used to handle variable length | |
4068 | lookbehinds on earlier releases, and longer than 255 characters. It is | |
4069 | described in | |
4070 | L<http://www.drregex.com/2019/02/variable-length-lookbehinds-actually.html>. | |
4071 | ||
4072 | =end original | |
4073 | ||
4074 | 以前のリリースや、255 文字より長い可変長後ろ読みを扱うのに使える | |
4075 | テクニックがあります。 | |
4076 | これは | |
4077 | L<http://www.drregex.com/2019/02/variable-length-lookbehinds-actually.html> に | |
4078 | 記述されています。 | |
4079 | ||
4080 | =begin original | |
4081 | ||
4082 | Note that under C</i>, a few single characters match two or three other | |
4083 | characters. This makes them variable length, and the 255 length applies | |
4084 | to the maximum number of characters in the match. For | |
4085 | example C<qr/\N{LATIN SMALL LETTER SHARP S}/i> matches the sequence | |
4086 | C<"ss">. Your lookbehind assertion could contain 127 Sharp S | |
4087 | characters under C</i>, but adding a 128th would generate a compilation | |
4088 | error, as that could match 256 C<"s"> characters in a row. | |
4089 | ||
4090 | =end original | |
4091 | ||
4092 | C</i> の下では、いくつかの単一の文字が 2 または 3 の他の文字に | |
4093 | マッチングすることに注意してください。 | |
4094 | これは後読みを可変長にし、長さ 255 はマッチングの中の文字の最大数に | |
4095 | 適用されます。 | |
4096 | 例えば、C<qr/\N{LATIN SMALL LETTER SHARP S}/i> は並び C<"ss"> に | |
4097 | マッチングします。 | |
4098 | あなたの後読み言明は C</i> の下で 127 個の Sharp S 文字を含むことは | |
4099 | できますが、128 個目を追加するとコンパイルエラーが生成されます; | |
4100 | これは 256 個連続した C<"s"> 文字にマッチングすることがあるからです。 | |
4101 | ||
4102 | =begin original | |
4103 | ||
4104 | Use of the non-greedy modifier C<"?"> may not give you the expected | |
4105 | results if it is within a capturing group within the construct. | |
4106 | ||
4107 | =end original | |
4108 | ||
4109 | 非貪欲修飾子 C<"?"> の使用は、この構文の中の捕捉グループの中では | |
4110 | 想定される結果にならないかもしれません。 | |
4111 | ||
4112 | =begin original | |
4113 | ||
4114 | The alphabetic forms are experimental; using them yields a warning in the | |
4115 | C<experimental::alpha_assertions> category. | |
4116 | ||
4117 | =end original | |
4118 | ||
4119 | 英字形式は実験的です; これらを使うと | |
4120 | C<experimental::alpha_assertions> カテゴリの警告が出ます。 | |
4121 | ||
2848 | 4122 | =back |
2849 | 4123 | |
2850 | =item C<(? | |
4124 | =item C<< (?<I<NAME>>I<pattern>) >> | |
2851 | 4125 | |
2852 | =item C< | |
4126 | =item C<(?'I<NAME>'I<pattern>)> | |
2853 | 4127 | X<< (?<NAME>) >> X<(?'NAME')> X<named capture> X<capture> |
2854 | 4128 | |
2855 | 4129 | =begin original |
2856 | 4130 | |
2857 | 4131 | A named capture group. Identical in every respect to normal capturing |
2858 | 4132 | parentheses C<()> but for the additional fact that the group |
2859 | 4133 | can be referred to by name in various regular expression |
2860 | constructs (like C<\g{NAME}>) and can be accessed by name | |
4134 | constructs (like C<\g{I<NAME>}>) and can be accessed by name | |
2861 | 4135 | after a successful match via C<%+> or C<%->. See L<perlvar> |
2862 | 4136 | for more details on the C<%+> and C<%-> hashes. |
2863 | 4137 | |
2864 | 4138 | =end original |
2865 | 4139 | |
2866 | 4140 | 名前付の捕捉グループ。 |
2867 | 4141 | 通常のキャプチャかっこ C<()> と同様ですがそれに加えて、 |
2868 | グループは(C<\g{NAME}> のように) 様々な正規表現構文で名前で参照でき、 | |
4142 | グループは(C<\g{I<NAME>}> のように) 様々な正規表現構文で名前で参照でき、 | |
2869 | 4143 | マッチングに成功したあと C<%+> を C<%-> を使って名前によって |
2870 | 4144 | アクセスできます。 |
2871 | 4145 | C<%+> 及び C<%-> ハッシュに関する詳細は L<perlvar> を |
2872 | 4146 | 参照してください。 |
2873 | 4147 | |
2874 | 4148 | =begin original |
2875 | 4149 | |
2876 | If multiple distinct capture groups have the same name then | |
4150 | If multiple distinct capture groups have the same name, then | |
2877 | $+{NAME} will refer to the leftmost defined group in the match. | |
4151 | C<$+{I<NAME>}> will refer to the leftmost defined group in the match. | |
2878 | 4152 | |
2879 | 4153 | =end original |
2880 | 4154 | |
2881 | 4155 | 複数の異なる捕捉グループが同じ名前を持っていたときには |
2882 | $+{NAME} はマッチングの中で一番左で定義されたグループを参照します。 | |
4156 | C<$+{I<NAME>}> はマッチングの中で一番左で定義されたグループを参照します。 | |
2883 | 4157 | |
2884 | 4158 | =begin original |
2885 | 4159 | |
2886 | The forms C<(?'NAME'pattern)> and C<< (?<NAME>pattern) >> | |
4160 | The forms C<(?'I<NAME>'I<pattern>)> and C<< (?<I<NAME>>I<pattern>) >> | |
4161 | are equivalent. | |
2887 | 4162 | |
2888 | 4163 | =end original |
2889 | 4164 | |
2890 | 二つの形式 C<(?'NAME'pattern)> | |
4165 | 二つの形式 C<(?'I<NAME>'I<pattern>)> と C<< (?<I<NAME>>I<pattern>) >> は | |
4166 | 等価です。 | |
2891 | 4167 | |
2892 | 4168 | =begin original |
2893 | 4169 | |
2894 | 4170 | B<NOTE:> While the notation of this construct is the same as the similar |
2895 | 4171 | function in .NET regexes, the behavior is not. In Perl the groups are |
2896 | 4172 | numbered sequentially regardless of being named or not. Thus in the |
2897 | 4173 | pattern |
2898 | 4174 | |
2899 | 4175 | =end original |
2900 | 4176 | |
2901 | 4177 | B<補足:> これを構成する記法は 類似していている .NET での正規表現と |
2902 | 4178 | 同じですが、振る舞いは異なります。 |
2903 | 4179 | Perl ではグループは名前がついているかどうかにかかわらず順番に番号が |
2904 | 4180 | 振られます。 |
2905 | 4181 | 従って次のパターンにおいて |
2906 | 4182 | |
2907 | 4183 | /(x)(?<foo>y)(z)/ |
2908 | 4184 | |
2909 | 4185 | =begin original |
2910 | 4186 | |
2911 | $+{foo} will be the same as $2, and $3 will contain 'z' instead of | |
4187 | C<$+{foo}> will be the same as C<$2>, and C<$3> will contain 'z' instead of | |
2912 | 4188 | the opposite which is what a .NET regex hacker might expect. |
2913 | 4189 | |
2914 | 4190 | =end original |
2915 | 4191 | |
2916 | $+{foo} は $2 と同じであり、$3 には .NET 正規表現に | |
4192 | C<$+{foo}> は C<$2> と同じであり、C<$3> には .NET 正規表現に | |
2917 | 4193 | 慣れた人が予測するのとは異なり 'z' が含まれます。 |
2918 | 4194 | |
2919 | 4195 | =begin original |
2920 | 4196 | |
2921 | Currently NAME is restricted to simple identifiers only. | |
4197 | Currently I<NAME> is restricted to simple identifiers only. | |
2922 | 4198 | In other words, it must match C</^[_A-Za-z][_A-Za-z0-9]*\z/> or |
2923 | 4199 | its Unicode extension (see L<utf8>), |
2924 | 4200 | though it isn't extended by the locale (see L<perllocale>). |
2925 | 4201 | |
2926 | 4202 | =end original |
2927 | 4203 | |
2928 | 現在のところ NAME はシンプルな識別子のみに制限されています。 | |
4204 | 現在のところ I<NAME> はシンプルな識別子のみに制限されています。 | |
2929 | 4205 | 言い換えると、C</^[_A-Za-z][_A-Za-z0-9]*\z/> または |
2930 | 4206 | その Unicode 拡張にマッチングしなければなりません |
2931 | 4207 | (L<utf8> も参照); しかしロケールでは拡張されません |
2932 | 4208 | (L<perllocale> 参照)。 |
2933 | 4209 | |
2934 | 4210 | =begin original |
2935 | 4211 | |
2936 | 4212 | B<NOTE:> In order to make things easier for programmers with experience |
2937 | with the Python or PCRE regex engines, the pattern C<< | |
4213 | with the Python or PCRE regex engines, the pattern C<< | |
2938 | ||
4214 | (?PE<lt>I<NAME>E<gt>I<pattern>) >> | |
4215 | may be used instead of C<< (?<I<NAME>>I<pattern>) >>; however this form does not | |
2939 | 4216 | support the use of single quotes as a delimiter for the name. |
2940 | 4217 | |
2941 | 4218 | =end original |
2942 | 4219 | |
2943 | 4220 | B<補足:> Python や PCRE 正規表現エンジンになれたプログラマが |
2944 | 楽になるように、C<< (?<NAME>pattern) >> の代わりに | |
4221 | 楽になるように、C<< (?<I<NAME>>I<pattern>) >> の代わりに | |
2945 | C<< (?PE<lt>NAMEE<gt>pattern) >> のパターンを使うことも | |
4222 | C<< (?PE<lt>I<NAME>E<gt>I<pattern>) >> のパターンを使うことも | |
2946 | 4223 | できます; しかしこの形式は名前のデリミタとして |
2947 | 4224 | シングルクォートの使用はサポートされていません。 |
2948 | 4225 | |
2949 | =item C<< \k<NAME> >> | |
4226 | =item C<< \k<I<NAME>> >> | |
2950 | 4227 | |
2951 | =item C<< \k'NAME' >> | |
4228 | =item C<< \k'I<NAME>' >> | |
2952 | 4229 | |
2953 | 4230 | =begin original |
2954 | 4231 | |
2955 | 4232 | Named backreference. Similar to numeric backreferences, except that |
2956 | 4233 | the group is designated by name and not number. If multiple groups |
2957 | 4234 | have the same name then it refers to the leftmost defined group in |
2958 | 4235 | the current match. |
2959 | 4236 | |
2960 | 4237 | =end original |
2961 | 4238 | |
2962 | 4239 | 名前による後方参照。 |
2963 | 4240 | 数値によってではなく名前によってグループを指定する点を除いて、名前による |
2964 | 4241 | 後方参照と似ています。 |
2965 | 4242 | もし同じ名前の複数のグループがあったときには現在のマッチングで |
2966 | 4243 | 一番左に定義されているグループを参照します。 |
2967 | 4244 | |
2968 | 4245 | =begin original |
2969 | 4246 | |
2970 | It is an error to refer to a name not defined by a C<< (?<NAME>) >> | |
4247 | It is an error to refer to a name not defined by a C<< (?<I<NAME>>) >> | |
2971 | 4248 | earlier in the pattern. |
2972 | 4249 | |
2973 | 4250 | =end original |
2974 | 4251 | |
2975 | パターン内で C<< (?<NAME>) >> によって定義されていない名前を | |
4252 | パターン内で C<< (?<I<NAME>>) >> によって定義されていない名前を | |
2976 | 4253 | 参照するとエラーになります。 |
2977 | 4254 | |
2978 | 4255 | =begin original |
2979 | 4256 | |
2980 | 4257 | Both forms are equivalent. |
2981 | 4258 | |
2982 | 4259 | =end original |
2983 | 4260 | |
2984 | 4261 | 両方の形式とも等価です。 |
2985 | 4262 | |
2986 | 4263 | =begin original |
2987 | 4264 | |
2988 | 4265 | B<NOTE:> In order to make things easier for programmers with experience |
2989 | with the Python or PCRE regex engines, the pattern C<< (?P=NAME) >> | |
4266 | with the Python or PCRE regex engines, the pattern C<< (?P=I<NAME>) >> | |
2990 | may be used instead of C<< \k<NAME> >>. | |
4267 | may be used instead of C<< \k<I<NAME>> >>. | |
2991 | 4268 | |
2992 | 4269 | =end original |
2993 | 4270 | |
2994 | 4271 | B<補足:> Python や PCRE 正規表現エンジンになれたプログラマが楽に |
2995 | なるように、C<< \k<NAME> >> の代わりに | |
4272 | なるように、C<< \k<I<NAME>> >> の代わりに | |
2996 | C<< (?P=NAME) >> のパターンを使うこともできます。 | |
4273 | C<< (?P=I<NAME>) >> のパターンを使うこともできます。 | |
2997 | 4274 | |
2998 | =item C<(?{ code })> | |
4275 | =item C<(?{ I<code> })> | |
2999 | 4276 | X<(?{})> X<regex, code in> X<regexp, code in> X<regular expression, code in> |
3000 | 4277 | |
3001 | 4278 | =begin original |
3002 | 4279 | |
3003 | B<WARNING>: | |
4280 | B<WARNING>: Using this feature safely requires that you understand its | |
3004 | ||
4281 | limitations. Code executed that has side effects may not perform identically | |
3005 | ||
4282 | from version to version due to the effect of future optimisations in the regex | |
3006 | ||
4283 | engine. For more information on this, see L</Embedded Code Execution | |
4284 | Frequency>. | |
3007 | 4285 | |
3008 | 4286 | =end original |
3009 | 4287 | |
3010 | B<警告>: この | |
4288 | B<警告>: この機能を安全に使うには、その制限について理解することが必要です。 | |
3011 | また通知なしに変更されるかもしれません。 | |
3012 | 4289 | 副作用を持つコードの実行は今後の正規表現エンジンの最適化の影響で |
3013 | 4290 | バージョン間で必ずしも同じになるとは限らないでしょう。 |
4291 | これに関するさらなる情報については、L</Embedded Code Execution Frequency> を | |
4292 | 参照してください。 | |
3014 | 4293 | |
3015 | 4294 | =begin original |
3016 | 4295 | |
3017 | This zero-width assertion e | |
4296 | This zero-width assertion executes any embedded Perl code. It always | |
3018 | ||
4297 | succeeds, and its return value is set as C<$^R>. | |
3019 | the rules to determine where the C<code> ends are somewhat convoluted. | |
3020 | 4298 | |
3021 | 4299 | =end original |
3022 | 4300 | |
3023 | このゼロ幅アサーションは埋め込まれた任意の Perl コードを | |
4301 | このゼロ幅アサーションは埋め込まれた任意の Perl コードを実行します。 | |
3024 | これは常に(正規表現として)成功し、 | |
4302 | これは常に(正規表現として)成功し、返り値は C<$^R> に設定されます。 | |
3025 | 今のところ、C<code> が終わる場所を認識するルールは少々複雑です。 | |
3026 | 4303 | |
3027 | 4304 | =begin original |
3028 | 4305 | |
3029 | ||
4306 | In literal patterns, the code is parsed at the same time as the | |
3030 | ||
4307 | surrounding code. While within the pattern, control is passed temporarily | |
3031 | ||
4308 | back to the perl parser, until the logically-balancing closing brace is | |
4309 | encountered. This is similar to the way that an array index expression in | |
4310 | a literal string is handled, for example | |
3032 | 4311 | |
3033 | 4312 | =end original |
3034 | 4313 | |
3035 | ||
4314 | リテラルなパターンでは、このコードは周りのコードと同時にパースされます。 | |
3036 | ||
4315 | このパターンの中では、 | |
3037 | ||
4316 | 論理的にバランスが取れる閉じかっこが現れるまで、 | |
4317 | 制御文字は一時的に perl パーサーに渡されます。 | |
4318 | これはリテラル文字列の中の配列添字表現の扱われ方と似ています; 例えば: | |
3038 | 4319 | |
3039 | | |
4320 | "abc$array[ 1 + f('[') + g()]def" | |
3040 | /the (\S+)(?{ $color = $^N }) (\S+)(?{ $animal = $^N })/i; | |
3041 | print "color = $color, animal = $animal\n"; | |
3042 | 4321 | |
3043 | 4322 | =begin original |
3044 | 4323 | |
3045 | In | |
4324 | In particular, braces do not need to be balanced: | |
4325 | ||
4326 | =end original | |
4327 | ||
4328 | 特に、中かっこはバランスが取れている必要はありません: | |
4329 | ||
4330 | s/abc(?{ f('{'); })/def/ | |
4331 | ||
4332 | =begin original | |
4333 | ||
4334 | Even in a pattern that is interpolated and compiled at run-time, literal | |
4335 | code blocks will be compiled once, at perl compile time; the following | |
4336 | prints "ABCD": | |
4337 | ||
4338 | =end original | |
4339 | ||
4340 | 実行時に展開されてコンパイルされるパターンの中であっても、 | |
4341 | リテラルなコードブロックは perl のコンパイル時に一度だけコンパイルされます; | |
4342 | 次のものは "ABCD" と表示されます: | |
4343 | ||
4344 | print "D"; | |
4345 | my $qr = qr/(?{ BEGIN { print "A" } })/; | |
4346 | my $foo = "foo"; | |
4347 | /$foo$qr(?{ BEGIN { print "B" } })/; | |
4348 | BEGIN { print "C" } | |
4349 | ||
4350 | =begin original | |
4351 | ||
4352 | In patterns where the text of the code is derived from run-time | |
4353 | information rather than appearing literally in a source code /pattern/, | |
4354 | the code is compiled at the same time that the pattern is compiled, and | |
4355 | for reasons of security, C<use re 'eval'> must be in scope. This is to | |
4356 | stop user-supplied patterns containing code snippets from being | |
4357 | executable. | |
4358 | ||
4359 | =end original | |
4360 | ||
4361 | コードのテキストがソースコードの /pattern/ としてリテラルに現れるのではなく、 | |
4362 | 実行時の情報から派生したものであるパターンでは、 | |
4363 | コードはパターンがコンパイルされるのと同時にコンパイルされ、 | |
4364 | セキュリティー上の理由により、C<use re 'eval'> が | |
4365 | スコープ内になければなりません。 | |
4366 | これは、実行可能なコード片を含む、ユーザーが提供したパターンを止めるためです。 | |
4367 | ||
4368 | =begin original | |
4369 | ||
4370 | In situations where you need to enable this with C<use re 'eval'>, you should | |
4371 | also have taint checking enabled. Better yet, use the carefully | |
4372 | constrained evaluation within a Safe compartment. See L<perlsec> for | |
4373 | details about both these mechanisms. | |
4374 | ||
4375 | =end original | |
4376 | ||
4377 | C<use re 'eval'> でこれを有効にする必要がある場合、 | |
4378 | 汚染チェックも有効にするべきです。 | |
4379 | 出来れば、Safe 区画の中で注意深く制限された評価を使ってください。 | |
4380 | これら両方の機構に関する詳細については L<perlsec> を参照してください。 | |
4381 | ||
4382 | =begin original | |
4383 | ||
4384 | From the viewpoint of parsing, lexical variable scope and closures, | |
4385 | ||
4386 | =end original | |
4387 | ||
4388 | パース、レキシカル変数スコープ、クロージャの観点から、 | |
4389 | ||
4390 | /AAA(?{ BBB })CCC/ | |
4391 | ||
4392 | =begin original | |
4393 | ||
4394 | behaves approximately like | |
4395 | ||
4396 | =end original | |
4397 | ||
4398 | はおよそ次のように振る舞います: | |
4399 | ||
4400 | /AAA/ && do { BBB } && /CCC/ | |
4401 | ||
4402 | =begin original | |
4403 | ||
4404 | Similarly, | |
4405 | ||
4406 | =end original | |
4407 | ||
4408 | 同様に、 | |
4409 | ||
4410 | qr/AAA(?{ BBB })CCC/ | |
4411 | ||
4412 | =begin original | |
4413 | ||
4414 | behaves approximately like | |
4415 | ||
4416 | =end original | |
4417 | ||
4418 | はおよそ次のように振る舞います: | |
4419 | ||
4420 | sub { /AAA/ && do { BBB } && /CCC/ } | |
4421 | ||
4422 | =begin original | |
4423 | ||
4424 | In particular: | |
4425 | ||
4426 | =end original | |
4427 | ||
4428 | 特に: | |
4429 | ||
4430 | { my $i = 1; $r = qr/(?{ print $i })/ } | |
4431 | my $i = 2; | |
4432 | /$r/; # prints "1" | |
4433 | ||
4434 | =begin original | |
4435 | ||
4436 | Inside a C<(?{...})> block, C<$_> refers to the string the regular | |
3046 | 4437 | expression is matching against. You can also use C<pos()> to know what is |
3047 | 4438 | the current position of matching within this string. |
3048 | 4439 | |
3049 | 4440 | =end original |
3050 | 4441 | |
3051 | 4442 | C<(?{...})> ブロックの中では C<$_> は正規表現をマッチングさせている文字列を |
3052 | 4443 | 参照します。 |
3053 | 4444 | C<pos()> を使ってこの文字列で現在のマッチング位置を知ることもできます。 |
3054 | 4445 | |
3055 | 4446 | =begin original |
3056 | 4447 | |
3057 | The | |
4448 | The code block introduces a new scope from the perspective of lexical | |
3058 | ||
4449 | variable declarations, but B<not> from the perspective of C<local> and | |
3059 | ||
4450 | similar localizing behaviours. So later code blocks within the same | |
4451 | pattern will still see the values which were localized in earlier blocks. | |
4452 | These accumulated localizations are undone either at the end of a | |
4453 | successful match, or if the assertion is backtracked (compare | |
4454 | L</"Backtracking">). For example, | |
3060 | 4455 | |
3061 | 4456 | =end original |
3062 | 4457 | |
3063 | C< | |
4458 | コードブロックは、C<local> や同様のローカル化の振る舞いの観点 B<ではなく> | |
3064 | ||
4459 | レキシカル変数宣言の観点での新しいスコープを導入します。 | |
3065 | ||
4460 | 従って、同じパターンで後に出てくるコードブロックは | |
4461 | 前に出てきたローカル化された値が見えるままです。 | |
4462 | これらの蓄積されたローカル化は、マッチングが成功するか、 | |
4463 | アサートがバックトラックした時点で巻き戻されます | |
4464 | (L</"Backtracking"> と比較してください)。 | |
4465 | 例えば: | |
3066 | 4466 | |
3067 | 4467 | $_ = 'a' x 8; |
3068 | 4468 | m< |
3069 | 4469 | (?{ $cnt = 0 }) # Initialize $cnt. |
3070 | 4470 | ( |
3071 | 4471 | a |
3072 | 4472 | (?{ |
3073 | 4473 | local $cnt = $cnt + 1; # Update $cnt, |
3074 | 4474 | # backtracking-safe. |
3075 | 4475 | }) |
3076 | 4476 | )* |
3077 | 4477 | aaaa |
3078 | 4478 | (?{ $res = $cnt }) # On success copy to |
3079 | 4479 | # non-localized location. |
3080 | 4480 | >x; |
3081 | 4481 | |
3082 | 4482 | =begin original |
3083 | 4483 | |
3084 | will | |
4484 | will initially increment C<$cnt> up to 8; then during backtracking, its | |
3085 | ||
4485 | value will be unwound back to 4, which is the value assigned to C<$res>. | |
3086 | ||
4486 | At the end of the regex execution, C<$cnt> will be wound back to its initial | |
4487 | value of 0. | |
3087 | 4488 | |
3088 | 4489 | =end original |
3089 | 4490 | |
3090 | は C<$ | |
4491 | これはまず C<$cnt> は 8 までインクリメントされます; | |
3091 | ||
4492 | それからバックトラックの間に、この値は 4 まで巻き戻され、 | |
3092 | C< | |
4493 | その値が C<$res> に代入されます。 | |
4494 | 正規表現実行の最後では、$cnt は初期値である 0 に巻き戻ります。 | |
3093 | 4495 | |
3094 | 4496 | =begin original |
3095 | 4497 | |
3096 | This assertion may be used as | |
4498 | 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 | 4499 | |
3102 | 4500 | =end original |
3103 | 4501 | |
3104 | このアサーションは | |
4502 | このアサーションは条件として: | |
3105 | ||
3106 | ||
4504 | (?(condition)yes-pattern|no-pattern) | |
4505 | ||
4506 | =begin original | |
4507 | ||
4508 | switch. If I<not> used in this way, the result of evaluation of I<code> | |
4509 | is put into the special variable C<$^R>. This happens immediately, so | |
4510 | C<$^R> can be used from other C<(?{ I<code> })> assertions inside the same | |
4511 | regular expression. | |
4512 | ||
4513 | =end original | |
4514 | ||
4515 | スイッチとして使われるかもしれません。 | |
4516 | この方法で使われI<なかった>のなら、I<code> の評価結果は特殊変数 C<$^R> に | |
3107 | 4517 | おかれます。 |
3108 | これはすぐに行われるので C<$^R> は同じ正規表現内の他の C<?{ code })> | |
4518 | これはすぐに行われるので C<$^R> は同じ正規表現内の他の C<?{ I<code> })> | |
3109 | 4519 | アサーションで使うことができます。 |
3110 | 4520 | |
3111 | 4521 | =begin original |
3112 | 4522 | |
3113 | 4523 | The assignment to C<$^R> above is properly localized, so the old |
3114 | 4524 | value of C<$^R> is restored if the assertion is backtracked; compare |
3115 | L<"Backtracking">. | |
4525 | L</"Backtracking">. | |
3116 | 4526 | |
3117 | 4527 | =end original |
3118 | 4528 | |
3119 | 4529 | この C<$^R> への設定は適切にlocal化されるため、C<$^R> の古い値は |
3120 | バックトラックしたときには復元されます; L<"Backtracking"> を | |
4530 | バックトラックしたときには復元されます; L</"Backtracking"> を | |
3121 | 4531 | 見てください。 |
3122 | 4532 | |
3123 | 4533 | =begin original |
3124 | 4534 | |
3125 | ||
4535 | Note that the special variable C<$^N> is particularly useful with code | |
3126 | ||
4536 | blocks to capture the results of submatches in variables without having to | |
3127 | ||
4537 | 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 | 4538 | |
3131 | 4539 | =end original |
3132 | 4540 | |
3133 | ||
4541 | 特殊変数 C<$^N> は、一緒にネストしたかっこの数を数えずに一つ前の | |
3134 | ||
4542 | マッチング結果を捕捉するコードブロックで特に有用です。 | |
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 | 4543 | 例えば: |
3148 | 4544 | |
3149 | | |
4545 | $_ = "The brown fox jumps over the lazy dog"; | |
3150 | c | |
4546 | /the (\S+)(?{ $color = $^N }) (\S+)(?{ $animal = $^N })/i; | |
3151 | $ | |
4547 | print "color = $color, animal = $animal\n"; | |
3152 | 4548 | |
4549 | =item C<(??{ I<code> })> | |
4550 | X<(??{})> | |
4551 | X<regex, postponed> X<regexp, postponed> X<regular expression, postponed> | |
4552 | ||
3153 | 4553 | =begin original |
3154 | 4554 | |
3155 | B | |
4555 | B<WARNING>: Using this feature safely requires that you understand its | |
3156 | ||
4556 | limitations. Code executed that has side effects may not perform | |
3157 | ||
4557 | identically from version to version due to the effect of future | |
3158 | ||
4558 | optimisations in the regex engine. For more information on this, see | |
3159 | ||
4559 | 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 | 4560 | |
3163 | 4561 | =end original |
3164 | 4562 | |
3165 | ||
4563 | B<警告>: この機能を安全に使うには、その制限について理解することが必要です。 | |
3166 | ||
4564 | 副作用を持つコードの実行は今後の正規表現エンジンの最適化の影響で | |
3167 | ||
4565 | バージョン間で必ずしも同じになるとは限らないでしょう。 | |
3168 | ||
4566 | これに関するさらなる情報については、L</Embedded Code Execution Frequency> を | |
3169 | ||
4567 | 参照してください。 | |
3170 | 使うべきでしょう。 | |
3171 | この双方のメカニズムについての詳細は L<perlsec> を参照してください。 | |
3172 | 4568 | |
3173 | 4569 | =begin original |
3174 | 4570 | |
3175 | ||
4571 | This is a "postponed" regular subexpression. It behaves in I<exactly> the | |
3176 | ||
4572 | same way as a C<(?{ I<code> })> code block as described above, except that | |
3177 | ||
4573 | its return value, rather than being assigned to C<$^R>, is treated as a | |
4574 | pattern, compiled if it's a string (or used as-is if its a qr// object), | |
4575 | then matched as if it were inserted instead of this construct. | |
3178 | 4576 | |
3179 | 4577 | =end original |
3180 | 4578 | |
3181 | ||
4579 | これは「先送りされた」正規部分表現です。 | |
3182 | ||
4580 | これは上述の C<(?{ I<code> })> コードブロックと I<正確に> 同じように | |
3183 | ||
4581 | 振る舞いますが、その返り値は、C<$^R> に代入されるのではなく、 | |
4582 | パターンとして扱われ、 | |
4583 | それが文字列の場合はコンパイルされ(あるいは qr// オブジェクトの場合は | |
4584 | そのまま使われ)、それからこの構文の代わりに挿入されていたかのように | |
4585 | マッチングします。 | |
3184 | 4586 | |
3185 | 4587 | =begin original |
3186 | 4588 | |
3187 | ||
4589 | During the matching of this sub-pattern, it has its own set of | |
3188 | ||
4590 | captures which are valid during the sub-match, but are discarded once | |
3189 | ||
4591 | control returns to the main pattern. For example, the following matches, | |
3190 | ||
4592 | with the inner pattern capturing "B" and matching "BB", while the outer | |
3191 | ||
4593 | pattern captures "A"; | |
3192 | unstable. | |
3193 | 4594 | |
3194 | 4595 | =end original |
3195 | 4596 | |
3196 | ||
4597 | 副パターンのマッチングの間、副マッチングの間有効な独自の捕捉グループを | |
3197 | ||
4598 | 持ちますが、一旦制御が主パターンに戻ると捨てられます。 | |
3198 | ||
4599 | 例えば、次のマッチングは、内側のパターンで "B" と "BB" にマッチングし、 | |
3199 | ||
4600 | 一方外側のパターンは "A" を捕捉します; | |
3200 | 4601 | |
3201 | ||
4602 | my $inner = '(.)\1'; | |
3202 | ||
4603 | "ABBA" =~ /^(.)(??{ $inner })\1/; | |
3203 | ||
4604 | print $1; # prints "A"; | |
3204 | 4605 | |
3205 | 4606 | =begin original |
3206 | 4607 | |
3207 | ||
4608 | Note that this means that there is no way for the inner pattern to refer | |
3208 | ||
4609 | to a capture group defined outside. (The code block itself can use C<$1>, | |
3209 | ||
4610 | 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 | 4611 | |
3212 | 4612 | =end original |
3213 | 4613 | |
3214 | ||
4614 | 内側のパターンが外側で定義された捕捉グループを参照する方法は | |
3215 | ||
4615 | ないことに注意してください。 | |
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 | 4616 | (コードブロック自体は、内側のパターンの捕捉グループを参照するために |
3242 | 4617 | C<$1> などを使えます。) |
3243 | 4618 | 従って: |
3244 | 4619 | |
3245 | 4620 | ('a' x 100)=~/(??{'(.)' x 100})/ |
3246 | 4621 | |
3247 | 4622 | =begin original |
3248 | 4623 | |
3249 | ||
4624 | I<will> match, it will I<not> set C<$1> on exit. | |
3250 | 4625 | |
3251 | 4626 | =end original |
3252 | 4627 | |
3253 | これはマッチング | |
4628 | これはマッチング I<します> が、終了時に C<$1> は設定 I<されません>。 | |
3254 | 4629 | |
3255 | 4630 | =begin original |
3256 | 4631 | |
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 | 4632 | The following pattern matches a parenthesized group: |
3268 | 4633 | |
3269 | 4634 | =end original |
3270 | 4635 | |
3271 | 4636 | 次のパターンはかっこで囲まれたグループにマッチングします: |
3272 | 4637 | |
3273 | 4638 | $re = qr{ |
3274 | 4639 | \( |
3275 | 4640 | (?: |
3276 | 4641 | (?> [^()]+ ) # Non-parens without backtracking |
3277 | 4642 | | |
3278 | 4643 | (??{ $re }) # Group with matching parens |
3279 | 4644 | )* |
3280 | 4645 | \) |
3281 | 4646 | }x; |
3282 | 4647 | |
3283 | 4648 | =begin original |
3284 | 4649 | |
3285 | See also | |
4650 | See also | |
4651 | L<C<(?I<PARNO>)>|/(?I<PARNO>) (?-I<PARNO>) (?+I<PARNO>) (?R) (?0)> | |
4652 | for a different, more efficient way to accomplish | |
3286 | 4653 | the same task. |
3287 | 4654 | |
3288 | 4655 | =end original |
3289 | 4656 | |
3290 | 同じタスクを行う別の、より効率的な方法として | |
4657 | 同じタスクを行う別の、より効率的な方法として | |
4658 | L<C<(?I<PARNO>)>|/(?I<PARNO>) (?-I<PARNO>) (?+I<PARNO>) (?R) (?0)> も | |
3291 | 4659 | 参照してください。 |
3292 | 4660 | |
3293 | 4661 | =begin original |
3294 | 4662 | |
3295 | ||
4663 | Executing a postponed regular expression too many times without | |
3296 | ||
4664 | consuming any input string will also result in a fatal error. The depth | |
3297 | ||
4665 | at which that happens is compiled into perl, so it can be changed with a | |
3298 | ||
4666 | custom build. | |
3299 | L<perlop/"qrE<sol>STRINGE<sol>msixpodual">). | |
3300 | 4667 | |
3301 | 4668 | =end original |
3302 | 4669 | |
3303 | ||
4670 | 入力を消費しない多すぎる先送りされた正規表現を実行するのも | |
3304 | ||
4671 | 致命的なエラーとなります。 | |
3305 | ||
4672 | これが起きる深度は perl にコンパイルされているので、カスタムビルドで | |
3306 | ||
4673 | これを変更できます。 | |
3307 | 4674 | |
3308 | = | |
4675 | =item C<(?I<PARNO>)> C<(?-I<PARNO>)> C<(?+I<PARNO>)> C<(?R)> C<(?0)> | |
4676 | X<(?PARNO)> X<(?1)> X<(?R)> X<(?0)> X<(?-1)> X<(?+1)> X<(?-PARNO)> X<(?+PARNO)> | |
4677 | X<regex, recursive> X<regexp, recursive> X<regular expression, recursive> | |
4678 | X<regex, relative recursion> X<GOSUB> X<GOSTART> | |
3309 | 4679 | |
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 | 4680 | =begin original |
3321 | 4681 | |
3322 | Recursi | |
4682 | Recursive subpattern. Treat the contents of a given capture buffer in the | |
3323 | re | |
4683 | current pattern as an independent subpattern and attempt to match it at | |
3324 | ||
4684 | the current position in the string. Information about capture state from | |
4685 | the caller for things like backreferences is available to the subpattern, | |
4686 | but capture buffers set by the subpattern are not visible to the caller. | |
3325 | 4687 | |
3326 | 4688 | =end original |
3327 | 4689 | |
3328 | ||
4690 | 再帰部分パターン。 | |
3329 | ||
4691 | 現在のパターンの与えられた捕捉バッファの内容を独立した | |
3330 | ||
4692 | 部分パターンとして扱って、 | |
4693 | 文字列の現在の位置でマッチングしようとします。 | |
4694 | 後方参照のような呼び出し元からの捕捉状態に関する情報は | |
4695 | 部分パターンで利用可能ですが、 | |
4696 | 部分パターンで設定された捕捉バッファは呼び出し元には見えません。 | |
3331 | 4697 | |
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 | 4698 | =begin original |
3338 | 4699 | |
3339 | Similar to C<(??{ code })> except it does not involve c | |
4700 | Similar to C<(??{ I<code> })> except that it does not involve executing any | |
3340 | ||
4701 | code or potentially compiling a returned pattern string; instead it treats | |
3341 | ||
4702 | the part of the current pattern contained within a specified capture group | |
3342 | ||
4703 | as an independent pattern that must match at the current position. Also | |
3343 | ||
4704 | different is the treatment of capture buffers, unlike C<(??{ I<code> })> | |
4705 | recursive patterns have access to their caller's match state, so one can | |
4706 | use backreferences safely. | |
3344 | 4707 | |
3345 | 4708 | =end original |
3346 | 4709 | |
3347 | ||
4710 | C<(??{ I<code> })> と似ていますが、 | |
3348 | ||
4711 | コードの実行を伴なわず、返されたパターン文字列をコンパイルもしません; | |
3349 | ||
4712 | その代わりに、指定された捕捉グループに含まれる現在のパターンの一部を、 | |
3350 | ||
4713 | 現在の位置でマッチングすべき独立したパターンとして扱います。 | |
4714 | また、捕捉バッファの扱いも異なります; | |
4715 | C<(??{ I<code> })> と異なり、再帰パターンはその呼び出し元のマッチング | |
4716 | 状態にアクセスすることが出来るので、安全に後方参照を使えます。 | |
3351 | 4717 | |
3352 | 4718 | =begin original |
3353 | 4719 | |
3354 | PARNO is a sequence of digits (not starting with 0) whose value reflects | |
4720 | I<PARNO> is a sequence of digits (not starting with 0) whose value reflects | |
3355 | 4721 | the paren-number of the capture group to recurse to. C<(?R)> recurses to |
3356 | 4722 | 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 | |
4723 | C<(?R)>. If I<PARNO> is preceded by a plus or minus sign then it is assumed | |
3358 | 4724 | to be relative, with negative numbers indicating preceding capture groups |
3359 | 4725 | and positive ones following. Thus C<(?-1)> refers to the most recently |
3360 | 4726 | declared group, and C<(?+1)> indicates the next group to be declared. |
3361 | 4727 | Note that the counting for relative recursion differs from that of |
3362 | 4728 | relative backreferences, in that with recursion unclosed groups B<are> |
3363 | 4729 | included. |
3364 | 4730 | |
3365 | 4731 | =end original |
3366 | 4732 | |
3367 | PARNO はその値が再帰させる捕捉グループのかっこ番号を反映する一連の | |
4733 | I<PARNO> はその値が再帰させる捕捉グループのかっこ番号を反映する一連の | |
3368 | 4734 | 数字からなります(そして 0 からは始まりません)。 |
3369 | 4735 | C<(?R)> はパターン全体の最初から再帰します。 |
3370 | 4736 | C<(?0)> は C<(?R)> の別の構文です。 |
3371 | PARNO の前に正符号または負符号がついていた場合には相対的な位置として | |
4737 | I<PARNO> の前に正符号または負符号がついていた場合には相対的な位置として | |
3372 | 4738 | 使われます; 負数であれば前の捕捉グループを、正数であれば続く |
3373 | 4739 | 捕捉グループを示します。 |
3374 | 4740 | 従って C<(?-1)> は一番最近宣言されたグループを参照し、C<(?+1)> は次に |
3375 | 4741 | 宣言されるグループを参照します。 |
3376 | 4742 | 相対再帰の数え方は相対後方参照とは違って、グループに閉じていない再帰は |
3377 | 4743 | 含まB<れる>ことに注意してください, |
3378 | 4744 | |
3379 | 4745 | =begin original |
3380 | 4746 | |
3381 | The following pattern matches a function foo() which may contain | |
4747 | The following pattern matches a function C<foo()> which may contain | |
3382 | 4748 | balanced parentheses as the argument. |
3383 | 4749 | |
3384 | 4750 | =end original |
3385 | 4751 | |
3386 | 4752 | 以下のパターンは引数にバランスのとれたかっこを含んでいるかもしれない関数 |
3387 | foo() にマッチングします。 | |
4753 | C<foo()> にマッチングします。 | |
3388 | 4754 | |
3389 | 4755 | $re = qr{ ( # paren group 1 (full function) |
3390 | 4756 | foo |
3391 | 4757 | ( # paren group 2 (parens) |
3392 | 4758 | \( |
3393 | 4759 | ( # paren group 3 (contents of parens) |
3394 | 4760 | (?: |
3395 | 4761 | (?> [^()]+ ) # Non-parens without backtracking |
3396 | 4762 | | |
3397 | 4763 | (?2) # Recurse to start of paren group 2 |
3398 | 4764 | )* |
3399 | 4765 | ) |
3400 | 4766 | \) |
3401 | 4767 | ) |
3402 | 4768 | ) |
3403 | 4769 | }x; |
3404 | 4770 | |
3405 | 4771 | =begin original |
3406 | 4772 | |
3407 | 4773 | If the pattern was used as follows |
3408 | 4774 | |
3409 | 4775 | =end original |
3410 | 4776 | |
3411 | 4777 | このパターンを以下のように使うと, |
3412 | 4778 | |
3413 | 4779 | 'foo(bar(baz)+baz(bop))'=~/$re/ |
3414 | 4780 | and print "\$1 = $1\n", |
3415 | 4781 | "\$2 = $2\n", |
3416 | 4782 | "\$3 = $3\n"; |
3417 | 4783 | |
3418 | 4784 | =begin original |
3419 | 4785 | |
3420 | 4786 | the output produced should be the following: |
3421 | 4787 | |
3422 | 4788 | =end original |
3423 | 4789 | |
3424 | 4790 | 次のように出力されます: |
3425 | 4791 | |
3426 | 4792 | $1 = foo(bar(baz)+baz(bop)) |
3427 | 4793 | $2 = (bar(baz)+baz(bop)) |
3428 | 4794 | $3 = bar(baz)+baz(bop) |
3429 | 4795 | |
3430 | 4796 | =begin original |
3431 | 4797 | |
3432 | 4798 | If there is no corresponding capture group defined, then it is a |
3433 | fatal error. Recursing deep | |
4799 | fatal error. Recursing deeply without consuming any input string will | |
3434 | ||
4800 | also result in a fatal error. The depth at which that happens is | |
3435 | into perl, so chang | |
4801 | compiled into perl, so it can be changed with a custom build. | |
3436 | 4802 | |
3437 | 4803 | =end original |
3438 | 4804 | |
3439 | 4805 | もし対応する捕捉グループが定義されていなかったときには致命的な |
3440 | 4806 | エラーとなります。 |
3441 | 入力を消費しない | |
4807 | 入力を消費しない深い再帰も致命的なエラーとなります。 | |
3442 | ||
4808 | これが起きる深度は perl にコンパイルされているので、カスタムビルドで | |
3443 | ||
4809 | これを変更できます。 | |
3444 | 4810 | |
3445 | 4811 | =begin original |
3446 | 4812 | |
3447 | 4813 | The following shows how using negative indexing can make it |
3448 | 4814 | easier to embed recursive patterns inside of a C<qr//> construct |
3449 | 4815 | for later use: |
3450 | 4816 | |
3451 | 4817 | =end original |
3452 | 4818 | |
3453 | 4819 | 以下に後で使うパターンのために、C<qr//> 構成子内で再帰を埋め込むのに |
3454 | 4820 | 負数の参照を使うとどのように容易になるかを示します: |
3455 | 4821 | |
3456 | 4822 | my $parens = qr/(\((?:[^()]++|(?-1))*+\))/; |
3457 | if (/foo $parens \s+ + \s+ bar $parens/x) { | |
4823 | if (/foo $parens \s+ \+ \s+ bar $parens/x) { | |
3458 | 4824 | # do something here... |
3459 | 4825 | } |
3460 | 4826 | |
3461 | 4827 | =begin original |
3462 | 4828 | |
3463 | 4829 | B<Note> that this pattern does not behave the same way as the equivalent |
3464 | 4830 | PCRE or Python construct of the same form. In Perl you can backtrack into |
3465 | 4831 | a recursed group, in PCRE and Python the recursed into group is treated |
3466 | 4832 | 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 | |
4833 | like C<(?i:(?1))> or C<(?:(?i)(?1))> do not affect how the sub-pattern will | |
3468 | 4834 | be processed. |
3469 | 4835 | |
3470 | 4836 | =end original |
3471 | 4837 | |
3472 | 4838 | B<補足> このパターンは PCRE や Python での等価な形式の構成子と同じように |
3473 | 4839 | 振る舞うわけではありません。 |
3474 | 4840 | Perl においては再帰グループの中にバックトラックできますが、PCRE や |
3475 | 4841 | Python ではグループへの再帰はアトミックに扱われます。 |
3476 | また、修飾子はコンパイル時に解決されるので、(?i:(?1)) や | |
4842 | また、修飾子はコンパイル時に解決されるので、C<(?i:(?1))> や | |
3477 | (?:(?i)(?1)) といった構成子はサブパターンがどのように処理されたかに | |
4843 | C<(?:(?i)(?1))> といった構成子はサブパターンがどのように処理されたかに | |
3478 | 4844 | 影響されません。 |
3479 | 4845 | |
3480 | =item C<(?&NAME)> | |
4846 | =item C<(?&I<NAME>)> | |
3481 | 4847 | X<(?&NAME)> |
3482 | 4848 | |
3483 | 4849 | =begin original |
3484 | 4850 | |
3485 | Recurse to a named subpattern. Identical to C<(?PARNO)> except that the | |
4851 | Recurse to a named subpattern. Identical to C<(?I<PARNO>)> except that the | |
3486 | 4852 | parenthesis to recurse to is determined by name. If multiple parentheses have |
3487 | 4853 | the same name, then it recurses to the leftmost. |
3488 | 4854 | |
3489 | 4855 | =end original |
3490 | 4856 | |
3491 | 4857 | 名前付きサブパターンへの再帰。 |
3492 | 再帰するかっこが名前によって決定される点以外は C<(?PARNO)> と等価です。 | |
4858 | 再帰するかっこが名前によって決定される点以外は C<(?I<PARNO>)> と等価です。 | |
3493 | 4859 | もし複数のかっこで同じ名前を持っていた場合には一番左のものに再帰します。 |
3494 | 4860 | |
3495 | 4861 | =begin original |
3496 | 4862 | |
3497 | 4863 | It is an error to refer to a name that is not declared somewhere in the |
3498 | 4864 | pattern. |
3499 | 4865 | |
3500 | 4866 | =end original |
3501 | 4867 | |
3502 | 4868 | パターンのどこでも宣言されていない名前の参照はエラーになります。 |
3503 | 4869 | |
3504 | 4870 | =begin original |
3505 | 4871 | |
3506 | 4872 | B<NOTE:> In order to make things easier for programmers with experience |
3507 | with the Python or PCRE regex engines the pattern C<< (?P>NAME) >> | |
4873 | with the Python or PCRE regex engines the pattern C<< (?P>I<NAME>) >> | |
3508 | may be used instead of C<< (?&NAME) >>. | |
4874 | may be used instead of C<< (?&I<NAME>) >>. | |
3509 | 4875 | |
3510 | 4876 | =end original |
3511 | 4877 | |
3512 | 4878 | B<補足:> Python または PCRE 正規表現エンジンに慣れているプログラマが |
3513 | 簡単になるように C<< (?&NAME) >> の代わりに C<< (?P>NA | |
4879 | 簡単になるように C<< (?&I<NAME>) >> の代わりに C<< (?P>I<NAME>) >> を | |
3514 | できます。 | |
4880 | 使うこともできます。 | |
3515 | 4881 | |
3516 | =item C<(?(condition)yes-pattern|no-pattern)> | |
4882 | =item C<(?(I<condition>)I<yes-pattern>|I<no-pattern>)> | |
3517 | 4883 | X<(?()> |
3518 | 4884 | |
3519 | =item C<(?(condition)yes-pattern)> | |
4885 | =item C<(?(I<condition>)I<yes-pattern>)> | |
3520 | 4886 | |
3521 | 4887 | =begin original |
3522 | 4888 | |
3523 | Conditional expression. Matches | |
4889 | Conditional expression. Matches I<yes-pattern> if I<condition> yields | |
3524 | a true value, matches | |
4890 | a true value, matches I<no-pattern> otherwise. A missing pattern always | |
3525 | 4891 | matches. |
3526 | 4892 | |
3527 | 4893 | =end original |
3528 | 4894 | |
3529 | 4895 | 条件付き式。 |
3530 | ||
4896 | I<condition> が真なら I<yes-pattern> にマッチングし、さもなければ | |
3531 | ||
4897 | I<no-pattern> にマッチングします。 | |
3532 | 4898 | パターンがなければ常にマッチングします。 |
3533 | 4899 | |
3534 | 4900 | =begin original |
3535 | 4901 | |
3536 | C<(condition)> should be | |
4902 | C<(I<condition>)> should be one of: | |
3537 | ||
3538 | ||
4904 | =end original | |
3539 | ||
3540 | ||
4906 | C<(I<condition>)> は次のいずれかです: | |
3541 | ||
4908 | =over 4 | |
4909 | ||
4910 | =item an integer in parentheses | |
4911 | ||
4912 | (かっこでくるまれた数値) | |
4913 | ||
4914 | =begin original | |
4915 | ||
4916 | (which is valid if the corresponding pair of parentheses | |
4917 | matched); | |
4918 | ||
4919 | =end original | |
4920 | ||
4921 | (対応するかっこ対がマッチングしたときに有効); | |
4922 | ||
4923 | =item a lookahead/lookbehind/evaluate zero-width assertion; | |
4924 | ||
4925 | (先読み/後読み/ゼロ幅で評価される言明) | |
4926 | ||
4927 | =item a name in angle brackets or single quotes | |
4928 | ||
4929 | (角かっこもしくはシングルクォートでくるまれた名前) | |
4930 | ||
4931 | =begin original | |
4932 | ||
4933 | (which is valid if a group with the given name matched); | |
4934 | ||
4935 | =end original | |
4936 | ||
4937 | (その名前のグループがマッチングしたときに有効); | |
4938 | ||
4939 | =item the special symbol C<(R)> | |
4940 | ||
4941 | (特殊なシンボル C<(R)>) | |
4942 | ||
4943 | =begin original | |
4944 | ||
4945 | (true when evaluated inside of recursion or eval). Additionally the | |
4946 | C<"R"> may be | |
3542 | 4947 | followed by a number, (which will be true when evaluated when recursing |
3543 | inside of the appropriate group), or by C<&NAME>, in which case it will | |
4948 | inside of the appropriate group), or by C<&I<NAME>>, in which case it will | |
3544 | 4949 | be true only when evaluated during recursion in the named group. |
3545 | 4950 | |
3546 | 4951 | =end original |
3547 | 4952 | |
3548 | ||
4953 | (再帰または eval 内で評価されているときに真)。 | |
3549 | ||
4954 | 加えて C<"R"> には数字(対応するグループ内で再帰しているときに真)、もしくは | |
3550 | ||
4955 | C<&I<NAME>>、こちらの時はその名前のグループで再帰している時にのみ真、を | |
3551 | マッチングしたときに有効)、特殊なシンボル (R) (再帰または eval 内で | |
3552 | 評価されているときに真)のいずれかです。 | |
3553 | 加えて R には数字(対応するグループ内で再帰しているときに真)、もしくは | |
3554 | C<&NAME>、こちらの時はその名前のグループで再帰している時にのみ真、を | |
3555 | 4956 | 続けることもできます。 |
3556 | 4957 | |
4958 | =back | |
4959 | ||
3557 | 4960 | =begin original |
3558 | 4961 | |
3559 | 4962 | Here's a summary of the possible predicates: |
3560 | 4963 | |
3561 | 4964 | =end original |
3562 | 4965 | |
3563 | 4966 | 可能な述語の要約を次に示します: |
3564 | 4967 | |
3565 | 4968 | =over 4 |
3566 | 4969 | |
3567 | =item (1) (2) ... | |
4970 | =item C<(1)> C<(2)> ... | |
3568 | 4971 | |
3569 | 4972 | =begin original |
3570 | 4973 | |
3571 | 4974 | Checks if the numbered capturing group has matched something. |
4975 | Full syntax: C<< (?(1)then|else) >> | |
3572 | 4976 | |
3573 | 4977 | =end original |
3574 | 4978 | |
3575 | 4979 | その番号の捕捉グループが何かにマッチングしたかどうかを調べます。 |
4980 | 完全な文法: C<< (?(1)then|else) >> | |
3576 | 4981 | |
3577 | =item (<NAME>) ('NAME') | |
4982 | =item C<(E<lt>I<NAME>E<gt>)> C<('I<NAME>')> | |
3578 | 4983 | |
3579 | 4984 | =begin original |
3580 | 4985 | |
3581 | 4986 | Checks if a group with the given name has matched something. |
4987 | Full syntax: C<< (?(<name>)then|else) >> | |
3582 | 4988 | |
3583 | 4989 | =end original |
3584 | 4990 | |
3585 | 4991 | その名前のグループが何かにマッチングしたかどうかを調べます。 |
4992 | 完全な文法: C<< (?(<name>)then|else) >> | |
3586 | 4993 | |
3587 | =item (?=...) (?!...) (?<=...) (?<!...) | |
4994 | =item C<(?=...)> C<(?!...)> C<(?<=...)> C<(?<!...)> | |
3588 | 4995 | |
3589 | 4996 | =begin original |
3590 | 4997 | |
3591 | Checks whether the pattern matches (or does not match, for the | |
4998 | Checks whether the pattern matches (or does not match, for the C<"!"> | |
3592 | 4999 | variants). |
5000 | Full syntax: C<< (?(?=I<lookahead>)I<then>|I<else>) >> | |
3593 | 5001 | |
3594 | 5002 | =end original |
3595 | 5003 | |
3596 | パターンがマッチングするか (あるいは | |
5004 | パターンがマッチングするか (あるいは C<"!"> 版はマッチングしないか) を | |
3597 | 5005 | チェックします。 |
5006 | 完全な文法: C<< (?(?=lookahead)then|else) >> | |
3598 | 5007 | |
3599 | =item (?{ CODE }) | |
5008 | =item C<(?{ I<CODE> })> | |
3600 | 5009 | |
3601 | 5010 | =begin original |
3602 | 5011 | |
3603 | 5012 | Treats the return value of the code block as the condition. |
5013 | Full syntax: C<< (?(?{ I<code> })I<then>|I<else>) >> | |
3604 | 5014 | |
3605 | 5015 | =end original |
3606 | 5016 | |
3607 | 5017 | コードブロックの返り値を条件として扱います。 |
5018 | 完全な文法: C<< (?(?{ I<code> })I<then>|I<else>) >> | |
3608 | 5019 | |
3609 | =item (R) | |
5020 | =item C<(R)> | |
3610 | 5021 | |
3611 | 5022 | =begin original |
3612 | 5023 | |
3613 | 5024 | Checks if the expression has been evaluated inside of recursion. |
5025 | Full syntax: C<< (?(R)I<then>|I<else>) >> | |
3614 | 5026 | |
3615 | 5027 | =end original |
3616 | 5028 | |
3617 | 5029 | 式が再帰の中で評価されているかどうかを調べます。 |
5030 | 完全な文法: C<< (?(R)I<then>|I<else>) >> | |
3618 | 5031 | |
3619 | =item (R1) (R2) ... | |
5032 | =item C<(R1)> C<(R2)> ... | |
3620 | 5033 | |
3621 | 5034 | =begin original |
3622 | 5035 | |
3623 | 5036 | Checks if the expression has been evaluated while executing directly |
3624 | 5037 | inside of the n-th capture group. This check is the regex equivalent of |
3625 | 5038 | |
3626 | 5039 | =end original |
3627 | 5040 | |
3628 | 5041 | 式がその n 番目の捕捉グループのすぐ内側で実行されているかどうかを調べます。 |
3629 | 5042 | これは次のものと等価な正規表現です |
3630 | 5043 | |
3631 | 5044 | if ((caller(0))[3] eq 'subname') { ... } |
3632 | 5045 | |
3633 | 5046 | =begin original |
3634 | 5047 | |
3635 | 5048 | In other words, it does not check the full recursion stack. |
3636 | 5049 | |
3637 | 5050 | =end original |
3638 | 5051 | |
3639 | 5052 | 言い換えると、これは完全な再帰スタックを調べるわけではありません。 |
3640 | 5053 | |
3641 | = | |
5054 | =begin original | |
3642 | 5055 | |
5056 | Full syntax: C<< (?(R1)I<then>|I<else>) >> | |
5057 | ||
5058 | =end original | |
5059 | ||
5060 | 完全な文法: C<< (?(R1)I<then>|I<else>) >> | |
5061 | ||
5062 | =item C<(R&I<NAME>)> | |
5063 | ||
3643 | 5064 | =begin original |
3644 | 5065 | |
3645 | 5066 | Similar to C<(R1)>, this predicate checks to see if we're executing |
3646 | 5067 | 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 | |
5068 | logic used by C<(?&I<NAME>)> to disambiguate). It does not check the full | |
3648 | 5069 | stack, but only the name of the innermost active recursion. |
5070 | Full syntax: C<< (?(R&I<name>)I<then>|I<else>) >> | |
3649 | 5071 | |
3650 | 5072 | =end original |
3651 | 5073 | |
3652 | 5074 | C<(R1)> と似ていて、この述語はその名前のつけられている一番左のグループの |
3653 | すぐ内側で実行されているかどうかをしらべます(一番左は C<(?NAME)> と | |
5075 | すぐ内側で実行されているかどうかをしらべます(一番左は C<(?&I<NAME>)> と | |
3654 | 5076 | 同じロジックです)。 |
3655 | 5077 | これは完全なスタックを調べずに、一番内部のアクティブな再帰の名前だけを |
3656 | 5078 | 調べます。 |
5079 | 完全な文法: C<< (?(R&I<name>)I<then>|I<else>) >> | |
3657 | 5080 | |
3658 | =item (DEFINE) | |
5081 | =item C<(DEFINE)> | |
3659 | 5082 | |
3660 | 5083 | =begin original |
3661 | 5084 | |
3662 | 5085 | In this case, the yes-pattern is never directly executed, and no |
3663 | 5086 | no-pattern is allowed. Similar in spirit to C<(?{0})> but more efficient. |
3664 | 5087 | See below for details. |
5088 | Full syntax: C<< (?(DEFINE)I<definitions>...) >> | |
3665 | 5089 | |
3666 | 5090 | =end original |
3667 | 5091 | |
3668 | 5092 | この場合において、yes-pattern は直接は実行されず、no-pattern は |
3669 | 5093 | 許可されていません。 |
3670 | 5094 | C<(?{0})> と似ていますがより効率的です。 |
3671 | 5095 | 詳細は次のようになります。 |
5096 | 完全な文法: C<< (?(DEFINE)I<definitions>...) >> | |
3672 | 5097 | |
3673 | 5098 | =back |
3674 | 5099 | |
3675 | 5100 | =begin original |
3676 | 5101 | |
3677 | 5102 | For example: |
3678 | 5103 | |
3679 | 5104 | =end original |
3680 | 5105 | |
3681 | 5106 | 例: |
3682 | 5107 | |
3683 | 5108 | m{ ( \( )? |
3684 | 5109 | [^()]+ |
3685 | 5110 | (?(1) \) ) |
3686 | 5111 | }x |
3687 | 5112 | |
3688 | 5113 | =begin original |
3689 | 5114 | |
3690 | 5115 | matches a chunk of non-parentheses, possibly included in parentheses |
3691 | 5116 | themselves. |
3692 | 5117 | |
3693 | 5118 | =end original |
3694 | 5119 | |
3695 | 5120 | これはかっこ以外からなる固まりかかっこの中にあるそれらにマッチングします。 |
3696 | 5121 | |
3697 | 5122 | =begin original |
3698 | 5123 | |
3699 | 5124 | A special form is the C<(DEFINE)> predicate, which never executes its |
3700 | 5125 | yes-pattern directly, and does not allow a no-pattern. This allows one to |
3701 | 5126 | define subpatterns which will be executed only by the recursion mechanism. |
3702 | 5127 | This way, you can define a set of regular expression rules that can be |
3703 | 5128 | bundled into any pattern you choose. |
3704 | 5129 | |
3705 | 5130 | =end original |
3706 | 5131 | |
3707 | 5132 | C<(DEFINE)> は特殊な形式で、これはその yes-pattern を直接は実行せず、 |
3708 | 5133 | no-pattern も許可していません。 |
3709 | 5134 | これは再帰メカニズムの中で利用することでのみ実行されるサブパターンの |
3710 | 5135 | 定義を許可します。 |
3711 | 5136 | これによって、選んだパターンと一緒に正規表現ルールを定義できます。 |
3712 | 5137 | |
3713 | 5138 | =begin original |
3714 | 5139 | |
3715 | 5140 | It is recommended that for this usage you put the DEFINE block at the |
3716 | 5141 | end of the pattern, and that you name any subpatterns defined within it. |
3717 | 5142 | |
3718 | 5143 | =end original |
3719 | 5144 | |
3720 | 5145 | この使い方において、DEFINE ブロックはパターンの最後におくこと、 |
3721 | 5146 | そしてそこで定義する全てのサブパターンに名前をつけることが |
3722 | 5147 | 推奨されています。 |
3723 | 5148 | |
3724 | 5149 | =begin original |
3725 | 5150 | |
3726 | 5151 | Also, it's worth noting that patterns defined this way probably will |
3727 | not be as efficient, as the optimi | |
5152 | not be as efficient, as the optimizer is not very clever about | |
3728 | 5153 | handling them. |
3729 | 5154 | |
3730 | 5155 | =end original |
3731 | 5156 | |
3732 | 5157 | また、この方法によって定義されるパターンはその処理に関してそんなに |
3733 | 5158 | 賢い訳ではないので効率的でないことに価値は何もないでしょう。 |
3734 | 5159 | |
3735 | 5160 | =begin original |
3736 | 5161 | |
3737 | 5162 | An example of how this might be used is as follows: |
3738 | 5163 | |
3739 | 5164 | =end original |
3740 | 5165 | |
3741 | 5166 | これをどのように使うかの例を次に示します: |
3742 | 5167 | |
3743 | 5168 | /(?<NAME>(?&NAME_PAT))(?<ADDR>(?&ADDRESS_PAT)) |
3744 | 5169 | (?(DEFINE) |
3745 | 5170 | (?<NAME_PAT>....) |
3746 | (?<ADRESS_PAT>....) | |
5171 | (?<ADDRESS_PAT>....) | |
3747 | 5172 | )/x |
3748 | 5173 | |
3749 | 5174 | =begin original |
3750 | 5175 | |
3751 | 5176 | Note that capture groups matched inside of recursion are not accessible |
3752 | 5177 | after the recursion returns, so the extra layer of capturing groups is |
3753 | 5178 | necessary. Thus C<$+{NAME_PAT}> would not be defined even though |
3754 | 5179 | C<$+{NAME}> would be. |
3755 | 5180 | |
3756 | 5181 | =end original |
3757 | 5182 | |
3758 | 5183 | 再帰の内側でマッチングした捕捉グループは再帰から戻った後には |
3759 | 5184 | アクセスできないため、余分な捕捉グループの |
3760 | 5185 | レイヤは必要な点に注意してください。 |
3761 | 5186 | 従って C<$+{NAME}> が定義されていても C<$+{NAME_PAT}> は定義されません。 |
3762 | 5187 | |
3763 | 5188 | =begin original |
3764 | 5189 | |
3765 | 5190 | Finally, keep in mind that subpatterns created inside a DEFINE block |
3766 | 5191 | count towards the absolute and relative number of captures, so this: |
3767 | 5192 | |
3768 | 5193 | =end original |
3769 | 5194 | |
3770 | 5195 | 最後に、DEFINE ブロックの内側で作られた副パターンは捕捉の絶対及び |
3771 | 5196 | 相対番号で数えることに注意してください; 従ってこうすると: |
3772 | 5197 | |
3773 | 5198 | my @captures = "a" =~ /(.) # First capture |
3774 | 5199 | (?(DEFINE) |
3775 | 5200 | (?<EXAMPLE> 1 ) # Second capture |
3776 | 5201 | )/x; |
3777 | 5202 | say scalar @captures; |
3778 | 5203 | |
3779 | 5204 | =begin original |
3780 | 5205 | |
3781 | 5206 | Will output 2, not 1. This is particularly important if you intend to |
3782 | 5207 | compile the definitions with the C<qr//> operator, and later |
3783 | 5208 | interpolate them in another pattern. |
3784 | 5209 | |
3785 | 5210 | =end original |
3786 | 5211 | |
3787 | 5212 | 1 ではなく 2 を出力します。 |
3788 | 5213 | これは、C<qr//> 演算子で定義をコンパイルして、 |
3789 | 5214 | 後で他のパターンの中で展開することを意図している場合に特に重要です。 |
3790 | 5215 | |
3791 | =item C<< (?>pattern) >> | |
5216 | =item C<< (?>I<pattern>) >> | |
5217 | ||
5218 | =item C<< (*atomic:I<pattern>) >> | |
5219 | X<(?E<gt>pattern)> | |
5220 | X<(*atomic> | |
3792 | 5221 | X<backtrack> X<backtracking> X<atomic> X<possessive> |
3793 | 5222 | |
3794 | 5223 | =begin original |
3795 | 5224 | |
3796 | 5225 | An "independent" subexpression, one which matches the substring |
3797 | that a | |
5226 | that a standalone I<pattern> would match if anchored at the given | |
3798 | 5227 | position, and it matches I<nothing other than this substring>. This |
3799 | 5228 | construct is useful for optimizations of what would otherwise be |
3800 | "eternal" matches, because it will not backtrack (see L<"Backtracking">). | |
5229 | "eternal" matches, because it will not backtrack (see L</"Backtracking">). | |
3801 | 5230 | It may also be useful in places where the "grab all you can, and do not |
3802 | 5231 | give anything back" semantic is desirable. |
3803 | 5232 | |
3804 | 5233 | =end original |
3805 | 5234 | |
3806 | 「独立した」部分式、 | |
5235 | 「独立した」部分式、スタンドアロンの I<pattern> がその場所に | |
3807 | 5236 | 固定されてマッチングする部分文字列にマッチングし、 |
3808 | 5237 | I<その文字列以外にはなにも>マッチングしません。 |
3809 | 5238 | この構成子は他の"外部"マッチングになる最適化に便利です; |
3810 | なぜならこれはバックトラックしないためです(L<"Backtracking"> 参照)。 | |
5239 | なぜならこれはバックトラックしないためです(L</"Backtracking"> 参照)。 | |
3811 | 5240 | これは "できる限りを取り込んで、後は戻らない"セマンティクスが |
3812 | 5241 | 必要な場所でも便利です。 |
3813 | 5242 | |
3814 | 5243 | =begin original |
3815 | 5244 | |
3816 | 5245 | For example: C<< ^(?>a*)ab >> will never match, since C<< (?>a*) >> |
3817 | 5246 | (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 | |
5247 | characters C<"a"> at the beginning of string, leaving no C<"a"> for | |
3819 | 5248 | C<ab> to match. In contrast, C<a*ab> will match the same as C<a+b>, |
3820 | 5249 | 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 | |
5250 | group C<ab> (see L</"Backtracking">). In particular, C<a*> inside | |
3822 | 5251 | C<a*ab> will match fewer characters than a standalone C<a*>, since |
3823 | 5252 | this makes the tail match. |
3824 | 5253 | |
3825 | 5254 | =end original |
3826 | 5255 | |
3827 | 5256 | 例: C<< ^(?>a*)ab >> は何もマッチングしません、 |
3828 | 5257 | なぜなら C<< (?>a*) >> (前述のように、文字列の開始で固定されます)は |
3829 | 文字列のはじめにある全ての文字 C<a> にマッチングし、 | |
5258 | 文字列のはじめにある全ての文字 C<"a"> にマッチングし、 | |
3830 | C<ab> のマッチングのための C<a> を残さないためです。 | |
5259 | C<ab> のマッチングのための C<"a"> を残さないためです。 | |
3831 | 5260 | 対照的に、C<a*ab> は C<a+b> と同じようにマッチングします、 |
3832 | 5261 | これはサブグループ C<a*> のマッチングは次のグループ C<ab> の影響を |
3833 | 受けるためです (L<"Backtracking"> 参照)。 | |
5262 | 受けるためです (L</"Backtracking"> 参照)。 | |
3834 | 5263 | 特に、C<a*ab> の中の C<a*> は単独の C<a*> より短い文字にマッチングします; |
3835 | 5264 | これによって最後のマッチングが行えるようになります。 |
3836 | 5265 | |
3837 | 5266 | =begin original |
3838 | 5267 | |
3839 | C<< (?>pattern) >> does not disable backtracking altogether once it has | |
5268 | C<< (?>I<pattern>) >> does not disable backtracking altogether once it has | |
3840 | 5269 | matched. It is still possible to backtrack past the construct, but not |
3841 | 5270 | into it. So C<< ((?>a*)|(?>b*))ar >> will still match "bar". |
3842 | 5271 | |
3843 | 5272 | =end original |
3844 | 5273 | |
3845 | C<< (?>pattern) >> は、一旦マッチングしたら、全くバックトラックを | |
5274 | C<< (?>I<pattern>) >> は、一旦マッチングしたら、全くバックトラックを | |
3846 | 5275 | 無効にしません。 |
3847 | 5276 | 未だこの構文の前までバックトラックする可能性はありますが、構文の中に |
3848 | 5277 | バックトラックすることはありません。 |
3849 | 5278 | 従って C<< ((?>a*)|(?>b*))ar >> は "bar" にマッチングするままです。 |
3850 | 5279 | |
3851 | 5280 | =begin original |
3852 | 5281 | |
3853 | An effect similar to C<< (?>pattern) >> may be achieved by writing | |
5282 | An effect similar to C<< (?>I<pattern>) >> may be achieved by writing | |
3854 | C<(?=(pattern))\g{-1}>. This matches the same substring as a standalone | |
5283 | C<(?=(I<pattern>))\g{-1}>. This matches the same substring as a standalone | |
3855 | 5284 | C<a+>, and the following C<\g{-1}> eats the matched string; it therefore |
3856 | 5285 | makes a zero-length assertion into an analogue of C<< (?>...) >>. |
3857 | 5286 | (The difference between these two constructs is that the second one |
3858 | 5287 | uses a capturing group, thus shifting ordinals of backreferences |
3859 | 5288 | in the rest of a regular expression.) |
3860 | 5289 | |
3861 | 5290 | =end original |
3862 | 5291 | |
3863 | C<< (?>pattern) >> と似た効果は C<(?=(pattern))\g{-1}> でも達成できます。 | |
5292 | C<< (?>I<pattern>) >> と似た効果は C<(?=(I<pattern>))\g{-1}> でも達成できます。 | |
3864 | 5293 | これは単独の C<a+> と同じ部分文字列にマッチングし、それに続く C<\g{-1}> が |
3865 | 5294 | マッチングした文字列を消費します; |
3866 | 5295 | これはゼロ幅の言明が C<< (?>...) >> の類似を作るためです。 |
3867 | 5296 | (この2つの構成子は後者はグループをキャプチャするため、 |
3868 | 5297 | それに続く正規表現の残りで後方参照の順序をずらす点で違いがあります。) |
3869 | 5298 | |
3870 | 5299 | =begin original |
3871 | 5300 | |
3872 | 5301 | Consider this pattern: |
3873 | 5302 | |
3874 | 5303 | =end original |
3875 | 5304 | |
3876 | 5305 | 次のパターンを考えてみてください: |
3877 | 5306 | |
3878 | 5307 | m{ \( |
3879 | 5308 | ( |
3880 | 5309 | [^()]+ # x+ |
3881 | 5310 | | |
3882 | 5311 | \( [^()]* \) |
3883 | 5312 | )+ |
3884 | 5313 | \) |
3885 | 5314 | }x |
3886 | 5315 | |
3887 | 5316 | =begin original |
3888 | 5317 | |
3889 | 5318 | That will efficiently match a nonempty group with matching parentheses |
3890 | 5319 | two levels deep or less. However, if there is no such group, it |
3891 | 5320 | will take virtually forever on a long string. That's because there |
3892 | 5321 | are so many different ways to split a long string into several |
3893 | 5322 | substrings. This is what C<(.+)+> is doing, and C<(.+)+> is similar |
3894 | 5323 | to a subpattern of the above pattern. Consider how the pattern |
3895 | 5324 | above detects no-match on C<((()aaaaaaaaaaaaaaaaaa> in several |
3896 | 5325 | seconds, but that each extra letter doubles this time. This |
3897 | 5326 | exponential performance will make it appear that your program has |
3898 | 5327 | hung. However, a tiny change to this pattern |
3899 | 5328 | |
3900 | 5329 | =end original |
3901 | 5330 | |
3902 | 5331 | これは 2 段階までのかっこでくるまれた空でないグループに効率的に |
3903 | 5332 | マッチングします。 |
3904 | 5333 | しかしながら、これはマッチングするグループがなかったときに長い |
3905 | 5334 | 文字列においてはほとんど永遠に戻りません。 |
3906 | 5335 | これは長い文字列をいくつかの部分文字列に分解する方法がいくつもあるためです。 |
3907 | 5336 | これは C<(.+)+> が行うことでもあり、C<(.+)+> は このパターンの |
3908 | 5337 | 部分パターンと似ています。 |
3909 | 5338 | このパターンが C<((()aaaaaaaaaaaaaaaaaa> にはマッチングしないことを |
3910 | 5339 | どうやって検出するかを少し考えてみましょう、 |
3911 | 5340 | しかしここでは余計な文字を2倍にしてみます。 |
3912 | 5341 | この指数的なパフォーマンスはプログラムのハングアップとして表面化します。 |
3913 | 5342 | しかしながら、このパターンに小さな変更をいれてみます, |
3914 | 5343 | |
3915 | 5344 | m{ \( |
3916 | 5345 | ( |
3917 | 5346 | (?> [^()]+ ) # change x+ above to (?> x+ ) |
3918 | 5347 | | |
3919 | 5348 | \( [^()]* \) |
3920 | 5349 | )+ |
3921 | 5350 | \) |
3922 | 5351 | }x |
3923 | 5352 | |
3924 | 5353 | =begin original |
3925 | 5354 | |
3926 | 5355 | which uses C<< (?>...) >> matches exactly when the one above does (verifying |
3927 | 5356 | 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, | |
5357 | the time when used on a similar string with 1000000 C<"a">s. Be aware, | |
3929 | 5358 | however, that, when this construct is followed by a |
3930 | 5359 | quantifier, it currently triggers a warning message under |
3931 | 5360 | the C<use warnings> pragma or B<-w> switch saying it |
3932 | 5361 | C<"matches null string many times in regex">. |
3933 | 5362 | |
3934 | 5363 | =end original |
3935 | 5364 | |
3936 | 5365 | これは上で行っているように C<< (?>...) >> マッチングを |
3937 | 5366 | 使っています(これは自身で確認してみるとよいでしょう)が、 |
3938 | しかし 1000000 個の C<a> からなる似た文字列を使ってみると、4 分の 1 の | |
5367 | しかし 1000000 個の C<"a"> からなる似た文字列を使ってみると、4 分の 1 の | |
3939 | 5368 | 時間で完了します。 |
3940 | 5369 | しかしながら、この構文は量指定子が引き続くと現在のところ |
3941 | 5370 | C<use warnings> プラグマまたは B<-w> スイッチの影響下では |
3942 | 5371 | C<"matches null string many times in regex"> |
3943 | 5372 | (正規表現において空文字列に何回もマッチングしました) という警告を |
3944 | 5373 | 発するでしょう。 |
3945 | 5374 | |
3946 | 5375 | =begin original |
3947 | 5376 | |
3948 | 5377 | On simple groups, such as the pattern C<< (?> [^()]+ ) >>, a comparable |
3949 | effect may be achieved by negative look | |
5378 | 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. | |
5379 | This was only 4 times slower on a string with 1000000 C<"a">s. | |
3951 | 5380 | |
3952 | 5381 | =end original |
3953 | 5382 | |
3954 | 5383 | パターン C<< (?> [^()]+ ) >> のような簡単なグループでは、 |
3955 | 5384 | 比較できる影響は C<[^()]+ (?! [^()] )> のように負の先読みの |
3956 | 5385 | 言明で達することができます。 |
3957 | これは 1000000 個の C<a> からなる文字列において 4 倍だけ遅くなります。 | |
5386 | これは 1000000 個の C<"a"> からなる文字列において 4 倍だけ遅くなります。 | |
3958 | 5387 | |
3959 | 5388 | =begin original |
3960 | 5389 | |
3961 | 5390 | The "grab all you can, and do not give anything back" semantic is desirable |
3962 | 5391 | in many situations where on the first sight a simple C<()*> looks like |
3963 | 5392 | the correct solution. Suppose we parse text with comments being delimited |
3964 | by C<#> followed by some optional (horizontal) whitespace. Contrary to | |
5393 | by C<"<#ins>"> followed by some optional (horizontal) whitespace. Contrary to | |
3965 | 5394 | its appearance, C<#[ \t]*> I<is not> the correct subexpression to match |
3966 | 5395 | the comment delimiter, because it may "give up" some whitespace if |
3967 | 5396 | the remainder of the pattern can be made to match that way. The correct |
3968 | 5397 | answer is either one of these: |
3969 | 5398 | |
3970 | 5399 | =end original |
3971 | 5400 | |
3972 | 5401 | 最初の C<()*> のような正しい解法となる多くの状況において |
3973 | 5402 | 「できる限りを取り込んで、後は戻らない」セマンティクスが望まれるものです。 |
3974 | 任意で(水平)空白の続く C<#> によって区切られるコメントのついたテキストの | |
5403 | 任意で(水平)空白の続く C<"<#ins>"> によって区切られるコメントのついたテキストの | |
3975 | 5404 | パースを考えてみます。 |
3976 | 5405 | その出現と対比して、C<#[ \t]*> はコメント区切りにマッチングする |
3977 | 5406 | 正しい部分式ではありません; なぜならパターンの残りがそれのマッチングを |
3978 | 5407 | 作ることができるのならそれはいくつかの空白を「あきらめてしまう」ためです。 |
3979 | 5408 | 正しい回答は以下のいずれかです: |
3980 | 5409 | |
3981 | 5410 | (?>#[ \t]*) |
3982 | 5411 | #[ \t]*(?![ \t]) |
3983 | 5412 | |
3984 | 5413 | =begin original |
3985 | 5414 | |
3986 | For example, to grab non-empty comments into $1, one should use either | |
5415 | For example, to grab non-empty comments into C<$1>, one should use either | |
3987 | 5416 | one of these: |
3988 | 5417 | |
3989 | 5418 | =end original |
3990 | 5419 | |
3991 | 例えば空でないコメントを $1 に取り込むためには次のいずれかを使います: | |
5420 | 例えば空でないコメントを C<$1> に取り込むためには次のいずれかを使います: | |
3992 | 5421 | |
3993 | 5422 | / (?> \# [ \t]* ) ( .+ ) /x; |
3994 | 5423 | / \# [ \t]* ( [^ \t] .* ) /x; |
3995 | 5424 | |
3996 | 5425 | =begin original |
3997 | 5426 | |
3998 | 5427 | Which one you pick depends on which of these expressions better reflects |
3999 | 5428 | the above specification of comments. |
4000 | 5429 | |
4001 | 5430 | =end original |
4002 | 5431 | |
4003 | 5432 | 選んだ方はコメントの仕様をより適切に反映した式に依存します。 |
4004 | 5433 | |
4005 | 5434 | =begin original |
4006 | 5435 | |
4007 | 5436 | In some literature this construct is called "atomic matching" or |
4008 | 5437 | "possessive matching". |
4009 | 5438 | |
4010 | 5439 | =end original |
4011 | 5440 | |
4012 | 5441 | いくつかの書籍においてこの構成子は「アトミックなマッチング」 |
4013 | 5442 | または「絶対最大量マッチング(possessive matching)」と呼ばれます。 |
4014 | 5443 | |
4015 | 5444 | =begin original |
4016 | 5445 | |
4017 | 5446 | Possessive quantifiers are equivalent to putting the item they are applied |
4018 | 5447 | to inside of one of these constructs. The following equivalences apply: |
4019 | 5448 | |
4020 | 5449 | =end original |
4021 | 5450 | |
4022 | 5451 | 絶対最大量指定子はそれが適用されている項目をこれらの構成子の中に置くことと |
4023 | 5452 | 等価です。 |
4024 | 5453 | 以下の等式が適用されます: |
4025 | 5454 | |
4026 | 5455 | Quantifier Form Bracketing Form |
4027 | 5456 | --------------- --------------- |
4028 | 5457 | PAT*+ (?>PAT*) |
4029 | 5458 | PAT++ (?>PAT+) |
4030 | 5459 | PAT?+ (?>PAT?) |
4031 | 5460 | PAT{min,max}+ (?>PAT{min,max}) |
4032 | 5461 | |
4033 | =back | |
4034 | ||
4035 | =head2 Special Backtracking Control Verbs | |
4036 | ||
4037 | (特殊なバックトラック制御記号) | |
4038 | ||
4039 | 5462 | =begin original |
4040 | 5463 | |
4041 | ||
5464 | Nested C<(?E<gt>...)> constructs are not no-ops, even if at first glance | |
4042 | ||
5465 | they might seem to be. This is because the nested C<(?E<gt>...)> can | |
4043 | ||
5466 | restrict internal backtracking that otherwise might occur. For example, | |
4044 | 5467 | |
4045 | 5468 | =end original |
4046 | 5469 | |
4047 | ||
5470 | ネストした C<(?E<gt>...)> 構文は、 | |
4048 | ||
5471 | たとえ一見何もしないように見えても、何もしないものではありません。 | |
4049 | ||
5472 | これは、ネストした C<(?E<gt>...)> は、なければ起きるかもしれない | |
4050 | ||
5473 | 内部バックトラックを制限するからです。 | |
5474 | 例えば: | |
4051 | 5475 | |
4052 | ||
5476 | "abc" =~ /(?>a[bc]*c)/ | |
4053 | 5477 | |
4054 | These special patterns are generally of the form C<(*VERB:ARG)>. Unless | |
4055 | otherwise stated the ARG argument is optional; in some cases, it is | |
4056 | forbidden. | |
4057 | ||
4058 | =end original | |
4059 | ||
4060 | これらの特殊なパターンは C<(*VERB:ARG)> という一般形式を持っています。 | |
4061 | ARG が任意であると規定されていいないいくつかのケース以外では、それは | |
4062 | 拒否されます。 | |
4063 | ||
4064 | 5478 | =begin original |
4065 | 5479 | |
4066 | ||
5480 | matches, but | |
4067 | has the special behaviour that when executed it sets the current package's | |
4068 | C<$REGERROR> and C<$REGMARK> variables. When doing so the following | |
4069 | rules apply: | |
4070 | 5481 | |
4071 | 5482 | =end original |
4072 | 5483 | |
4073 | ||
5484 | これはマッチングしますが: | |
4074 | それが実行されると現在のパッケージの C<$REGERROR> 及び C<$REGMARK> 変数を | |
4075 | 設定する特殊な振る舞いを持っています。 | |
4076 | これが行われる時以下の手順が適用されます。 | |
4077 | 5485 | |
4078 | ||
5486 | "abc" =~ /(?>a(?>[bc]*)c)/ | |
4079 | 5487 | |
4080 | On failure, the C<$REGERROR> variable will be set to the ARG value of the | |
4081 | 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 | |
4083 | name of the last C<(*MARK:NAME)> pattern executed, or to TRUE if there was | |
4084 | none. Also, the C<$REGMARK> variable will be set to FALSE. | |
4085 | ||
4086 | =end original | |
4087 | ||
4088 | 失敗時には C<$REGERROR> 変数には、記号がマッチングの失敗の中で | |
4089 | 使われていたのならその記号パターンの ARG の値がセットされます。 | |
4090 | もしパターンの ARG 部分が省略されていたときには、C<$REGERROR> には | |
4091 | 最後に実行された C<(*MARK:NAME)> パターンの名前、またはそれもなければ | |
4092 | 真に設定されます。 | |
4093 | また、C<$REGMARK> 変数は偽に設定されます。 | |
4094 | ||
4095 | 5488 | =begin original |
4096 | 5489 | |
4097 | ||
5490 | does not. | |
4098 | the C<$REGMARK> variable will be set to the name of the last | |
4099 | C<(*MARK:NAME)> pattern executed. See the explanation for the | |
4100 | C<(*MARK:NAME)> verb below for more details. | |
4101 | 5491 | |
4102 | 5492 | =end original |
4103 | 5493 | |
4104 | マッチング | |
5494 | これはマッチングしません。 | |
4105 | 最後に実行された C<(*MARK:NAME)> パターンの名前が設定されます。 | |
4106 | 詳細は C<(*MARK:NAME)> 記号の説明を参照してください。 | |
4107 | 5495 | |
4108 | 5496 | =begin original |
4109 | 5497 | |
4110 | ||
5498 | The alphabetic form (C<(*atomic:...)>) is experimental; using it | |
4111 | an | |
5499 | yields a warning in the C<experimental::alpha_assertions> category. | |
4112 | readonly, but instead are volatile package variables similar to C<$AUTOLOAD>. | |
4113 | Use C<local> to localize changes to them to a specific scope if necessary. | |
4114 | 5500 | |
4115 | 5501 | =end original |
4116 | 5502 | |
4117 | ||
5503 | 英字形式 (C<(*atomic:...)>) は実験的です; これを使うと | |
4118 | ||
5504 | C<experimental::alpha_assertions> カテゴリの警告が出ます。 | |
4119 | それらはスコープ内にローカルにならず、読み込み専用でもありませんが、 | |
4120 | C<$AUTOLOAD> と似た揮発するパッケージ変数です。 | |
4121 | 必要時に特定のスコープ内に変更を留めたいときには C<local> を使ってください。 | |
4122 | 5505 | |
4123 | = | |
5506 | =item C<(?[ ])> | |
4124 | 5507 | |
4125 | If a pattern does not contain a special backtracking verb that allows an | |
4126 | argument, then C<$REGERROR> and C<$REGMARK> are not touched at all. | |
4127 | ||
4128 | =end original | |
4129 | ||
4130 | もしパターンが引数を許可する特殊バックトラック記号を含んでなかった場合には、 | |
4131 | C<$REGERROR> 及び C<$REGMARK> は全く触られません。 | |
4132 | ||
4133 | =over 3 | |
4134 | ||
4135 | =item Verbs that take an argument | |
4136 | ||
4137 | (引数を取る動詞) | |
4138 | ||
4139 | =over 4 | |
4140 | ||
4141 | =item C<(*PRUNE)> C<(*PRUNE:NAME)> | |
4142 | X<(*PRUNE)> X<(*PRUNE:NAME)> | |
4143 | ||
4144 | 5508 | =begin original |
4145 | 5509 | |
4146 | ||
5510 | See L<perlrecharclass/Extended Bracketed Character Classes>. | |
4147 | when backtracked into on failure. Consider the pattern C<A (*PRUNE) B>, | |
4148 | where A and B are complex patterns. Until the C<(*PRUNE)> verb is reached, | |
4149 | 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 | |
4151 | not match, then no further backtracking will take place, and the pattern | |
4152 | will fail outright at the current starting position. | |
4153 | 5511 | |
4154 | 5512 | =end original |
4155 | 5513 | |
4156 | ||
5514 | L<perlrecharclass/Extended Bracketed Character Classes> を参照してください。 | |
4157 | バックトラックツリーを刈り取ります。 | |
4158 | C<A (*PRUNE) B> というパターンで A も B も複雑なパターンである時を | |
4159 | 考えてみます。 | |
4160 | C<(*PRUNE)> に達するまでは、A はマッチングに必要であれば | |
4161 | バックトラックしていきます。 | |
4162 | しかし一旦そこに達して B に続くと、そこでも必要に応じてバックトラックします; | |
4163 | しかしながら、B がマッチングしなかったときにはそれ以上のバックトラックは | |
4164 | 行われず、現在の開始位置でのマッチングはすぐに失敗します。 | |
4165 | 5515 | |
4166 | 5516 | =begin original |
4167 | 5517 | |
4168 | ||
5518 | Note that this feature is currently L<experimental|perlpolicy/experimental>; | |
4169 | ||
5519 | using it yields a warning in the C<experimental::regex_sets> category. | |
4170 | 5520 | |
4171 | 5521 | =end original |
4172 | 5522 | |
4173 | ||
5523 | この機能は現在 L<実験的|perlpolicy/experimental> です; | |
4174 | ||
5524 | これを使うと C<experimental::regex_sets> カテゴリの警告が発生します。 | |
4175 | 5525 | |
4176 | 'aaab' =~ /a+b?(?{print "$&\n"; $count++})(*FAIL)/; | |
4177 | print "Count=$count\n"; | |
4178 | ||
4179 | =begin original | |
4180 | ||
4181 | which produces: | |
4182 | ||
4183 | =end original | |
4184 | ||
4185 | この出力: | |
4186 | ||
4187 | aaab | |
4188 | aaa | |
4189 | aa | |
4190 | a | |
4191 | aab | |
4192 | aa | |
4193 | a | |
4194 | ab | |
4195 | a | |
4196 | Count=9 | |
4197 | ||
4198 | =begin original | |
4199 | ||
4200 | If we add a C<(*PRUNE)> before the count like the following | |
4201 | ||
4202 | =end original | |
4203 | ||
4204 | 次のように数える前に C<(*PRUNE)> を加えると | |
4205 | ||
4206 | 'aaab' =~ /a+b?(*PRUNE)(?{print "$&\n"; $count++})(*FAIL)/; | |
4207 | print "Count=$count\n"; | |
4208 | ||
4209 | =begin original | |
4210 | ||
4211 | we prevent backtracking and find the count of the longest matching string | |
4212 | at each matching starting point like so: | |
4213 | ||
4214 | =end original | |
4215 | ||
4216 | バックトラックを妨げ次のように各開始位置での一番長いマッチング文字列を | |
4217 | 数えるようになります: | |
4218 | ||
4219 | aaab | |
4220 | aab | |
4221 | ab | |
4222 | Count=3 | |
4223 | ||
4224 | =begin original | |
4225 | ||
4226 | Any number of C<(*PRUNE)> assertions may be used in a pattern. | |
4227 | ||
4228 | =end original | |
4229 | ||
4230 | 1つのパターン内で C<(*PRUNE)> 言明はいくつでも使えます。 | |
4231 | ||
4232 | =begin original | |
4233 | ||
4234 | See also C<< (?>pattern) >> and possessive quantifiers for other ways to | |
4235 | control backtracking. In some cases, the use of C<(*PRUNE)> can be | |
4236 | replaced with a C<< (?>pattern) >> with no functional difference; however, | |
4237 | C<(*PRUNE)> can be used to handle cases that cannot be expressed using a | |
4238 | C<< (?>pattern) >> alone. | |
4239 | ||
4240 | =end original | |
4241 | ||
4242 | バックトラックを制御する他の方法として C<< (?>pattern) >> | |
4243 | 及び絶対最大量指定子も参照してください。 | |
4244 | 幾つかのケースにおいては C<(*PRUNE)> の利用は機能的な違いなしに | |
4245 | C<< (?>pattern) >> で置き換えることができます; しかしながら C<(*PRUNE)> は | |
4246 | C<< (?>pattern) >> 単独では表現できないケースを扱うために使えます。 | |
4247 | ||
4248 | =item C<(*SKIP)> C<(*SKIP:NAME)> | |
4249 | X<(*SKIP)> | |
4250 | ||
4251 | =begin original | |
4252 | ||
4253 | This zero-width pattern is similar to C<(*PRUNE)>, except that on | |
4254 | failure it also signifies that whatever text that was matched leading up | |
4255 | to the C<(*SKIP)> pattern being executed cannot be part of I<any> match | |
4256 | of this pattern. This effectively means that the regex engine "skips" forward | |
4257 | to this position on failure and tries to match again, (assuming that | |
4258 | there is sufficient room to match). | |
4259 | ||
4260 | =end original | |
4261 | ||
4262 | このゼロ幅のパターンは C<*PRUNE> と似ていますが、実行されている | |
4263 | C<(*SKIP)> パターンまでにマッチングしたテキストはこのパターンの | |
4264 | I<どの>マッチングの一部にもならないことを示します。 | |
4265 | これは正規表現エンジンがこの位置まで失敗として「スキップ」して(マッチングに | |
4266 | 十分な空間があれば)再びマッチングを試みることを効率的に意味します。 | |
4267 | ||
4268 | =begin original | |
4269 | ||
4270 | The name of the C<(*SKIP:NAME)> pattern has special significance. If a | |
4271 | C<(*MARK:NAME)> was encountered while matching, then it is that position | |
4272 | which is used as the "skip point". If no C<(*MARK)> of that name was | |
4273 | encountered, then the C<(*SKIP)> operator has no effect. When used | |
4274 | without a name the "skip point" is where the match point was when | |
4275 | executing the (*SKIP) pattern. | |
4276 | ||
4277 | =end original | |
4278 | ||
4279 | C<(*SKIP:NAME)> パターンの名前部分には特別な意味があります。 | |
4280 | もしマッチングにおいて C<(*MARK:NAME)> に遭遇すると、それは「スキップ | |
4281 | 位置」として使われる位置になります。 | |
4282 | その名前の C<(*MARK)> と東宮していなければ、C<(*SKIP)> 操作は効果を | |
4283 | 持ちません。 | |
4284 | 名前がなければ「スキップ位置」は(*SKIP)パターンの実行されたときに | |
4285 | マッチングポイントが使われます。 | |
4286 | ||
4287 | =begin original | |
4288 | ||
4289 | Compare the following to the examples in C<(*PRUNE)>; note the string | |
4290 | is twice as long: | |
4291 | ||
4292 | =end original | |
4293 | ||
4294 | 以下の例を C<(*PRUNE)> と比べてみてください; | |
4295 | 文字列が2倍になってることに注意してください: | |
4296 | ||
4297 | 'aaabaaab' =~ /a+b?(*SKIP)(?{print "$&\n"; $count++})(*FAIL)/; | |
4298 | print "Count=$count\n"; | |
4299 | ||
4300 | =begin original | |
4301 | ||
4302 | outputs | |
4303 | ||
4304 | =end original | |
4305 | ||
4306 | これの出力は | |
4307 | ||
4308 | aaab | |
4309 | aaab | |
4310 | Count=2 | |
4311 | ||
4312 | =begin original | |
4313 | ||
4314 | Once the 'aaab' at the start of the string has matched, and the C<(*SKIP)> | |
4315 | executed, the next starting point will be where the cursor was when the | |
4316 | C<(*SKIP)> was executed. | |
4317 | ||
4318 | =end original | |
4319 | ||
4320 | いったん文字列の最初の 'aaab' がマッチングして、C<(*SKIP)> が実行されると、 | |
4321 | 次の開始位置は C<(*SKIP)> が実行されたときのカーソルがいた位置になります。 | |
4322 | ||
4323 | =item C<(*MARK:NAME)> C<(*:NAME)> | |
4324 | X<(*MARK)> X<(*MARK:NAME)> X<(*:NAME)> | |
4325 | ||
4326 | =begin original | |
4327 | ||
4328 | This zero-width pattern can be used to mark the point reached in a string | |
4329 | when a certain part of the pattern has been successfully matched. This | |
4330 | mark may be given a name. A later C<(*SKIP)> pattern will then skip | |
4331 | 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. | |
4333 | ||
4334 | =end original | |
4335 | ||
4336 | このゼロ幅のマッチングはパターン内の特定の箇所がマッチングに成功したときに、 | |
4337 | 文字列の中で達した位置を記録するために使われます。 | |
4338 | このマークには名前をつけることもできます。 | |
4339 | 後者の C<(*SKIP)> パターンは失敗時でバックトラックしたときにその箇所まで | |
4340 | スキップします。 | |
4341 | C<(*MARK)> パターンはいくつでも使うことができて、NAME 部分は | |
4342 | 重複することもあります。 | |
4343 | ||
4344 | =begin original | |
4345 | ||
4346 | In addition to interacting with the C<(*SKIP)> pattern, C<(*MARK:NAME)> | |
4347 | can be used to "label" a pattern branch, so that after matching, the | |
4348 | program can determine which branches of the pattern were involved in the | |
4349 | match. | |
4350 | ||
4351 | =end original | |
4352 | ||
4353 | C<(*SKIP)> パターンとの相互動作に加えて、C<(*MARK:NAME)> はパターン分岐の | |
4354 | 「ラベル」としても使うことができます; このためマッチングの後で、プログラムは | |
4355 | そのマッチングにおいてパターンのどの分岐が使われたのかを知ることができます。 | |
4356 | ||
4357 | =begin original | |
4358 | ||
4359 | When a match is successful, the C<$REGMARK> variable will be set to the | |
4360 | name of the most recently executed C<(*MARK:NAME)> that was involved | |
4361 | in the match. | |
4362 | ||
4363 | =end original | |
4364 | ||
4365 | マッチングの成功時に、C<$REGMARK> 変数はマッチングの中で一番最近に | |
4366 | 実行された C<(*MARK:NAME)> の名前を設定します。 | |
4367 | ||
4368 | =begin original | |
4369 | ||
4370 | This can be used to determine which branch of a pattern was matched | |
4371 | without using a separate capture group for each branch, which in turn | |
4372 | can result in a performance improvement, as perl cannot optimize | |
4373 | C</(?:(x)|(y)|(z))/> as efficiently as something like | |
4374 | C</(?:x(*MARK:x)|y(*MARK:y)|z(*MARK:z))/>. | |
4375 | ||
4376 | =end original | |
4377 | ||
4378 | これは書く分岐で別々の捕捉グループを使うことなしにパターンのどの分岐が | |
4379 | マッチングしたのかを知るために使うことができます; これは perl は | |
4380 | C</(?:(x)|(y)|(z))/> を C</(?:x(*MARK:x)|y(*MARK:y)|z(*MARK:z))/> 程度に | |
4381 | 効率的には最適化できないためパフォーマンスの向上をもたらします。 | |
4382 | ||
4383 | =begin original | |
4384 | ||
4385 | When a match has failed, and unless another verb has been involved in | |
4386 | failing the match and has provided its own name to use, the C<$REGERROR> | |
4387 | variable will be set to the name of the most recently executed | |
4388 | C<(*MARK:NAME)>. | |
4389 | ||
4390 | =end original | |
4391 | ||
4392 | マッチングが失敗して、そして他の記号がマッチングの失敗で行われずかつ名前を | |
4393 | 持っているというのでなければ、C<$REGERROR> 変数には一番最近に実行された | |
4394 | 名前が設定されます。 | |
4395 | ||
4396 | =begin original | |
4397 | ||
4398 | See L</(*SKIP)> for more details. | |
4399 | ||
4400 | =end original | |
4401 | ||
4402 | 詳細は L</(*SKIP)> を参照してください。 | |
4403 | ||
4404 | =begin original | |
4405 | ||
4406 | As a shortcut C<(*MARK:NAME)> can be written C<(*:NAME)>. | |
4407 | ||
4408 | =end original | |
4409 | ||
4410 | C<(*MARK:NAME)> の短縮形として C<(*:NAME)> とも記述できます。 | |
4411 | ||
4412 | =item C<(*THEN)> C<(*THEN:NAME)> | |
4413 | ||
4414 | =begin original | |
4415 | ||
4416 | This is similar to the "cut group" operator C<::> from Perl 6. Like | |
4417 | C<(*PRUNE)>, this verb always matches, and when backtracked into on | |
4418 | failure, it causes the regex engine to try the next alternation in the | |
4419 | innermost enclosing group (capturing or otherwise) that has alternations. | |
4420 | The two branches of a C<(?(condition)yes-pattern|no-pattern)> do not | |
4421 | count as an alternation, as far as C<(*THEN)> is concerned. | |
4422 | ||
4423 | =end original | |
4424 | ||
4425 | これは Perl 6 の "cut group" 演算子 C<::> と似ています。 | |
4426 | C<(*PRUNE)> のように、この記号は常にマッチングし、そして失敗で | |
4427 | バックトラックした時に正規表現エンジンに、代替のある一番内側で閉じている | |
4428 | グループ(キャプチャでもそうでなくとも)で次の代替を試みるようにさせます。 | |
4429 | C<(*THEN)> が有効である限り、 | |
4430 | C<(?(condition)yes-pattern|no-pattern)> の二つの枝は代替とは扱われません。 | |
4431 | ||
4432 | =begin original | |
4433 | ||
4434 | 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 | |
4436 | pattern-based if/then/else block: | |
4437 | ||
4438 | =end original | |
4439 | ||
4440 | この名前は代替演算子(C<|>) と連結されたこの演算子で本質的にパターンベースの | |
4441 | if/then/else ブロックとなるものを作るために使うことが | |
4442 | できることからきています: | |
4443 | ||
4444 | ( COND (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) | |
4445 | ||
4446 | =begin original | |
4447 | ||
4448 | Note that if this operator is used and NOT inside of an alternation then | |
4449 | it acts exactly like the C<(*PRUNE)> operator. | |
4450 | ||
4451 | =end original | |
4452 | ||
4453 | この演算子が使われていてそしてそれが代替の内側ではなければ | |
4454 | これはちょうど C<(*PRUNE)> 演算子のように動作します。 | |
4455 | ||
4456 | / A (*PRUNE) B / | |
4457 | ||
4458 | =begin original | |
4459 | ||
4460 | is the same as | |
4461 | ||
4462 | =end original | |
4463 | ||
4464 | は次と同じです | |
4465 | ||
4466 | / A (*THEN) B / | |
4467 | ||
4468 | =begin original | |
4469 | ||
4470 | but | |
4471 | ||
4472 | =end original | |
4473 | ||
4474 | しかし | |
4475 | ||
4476 | / ( A (*THEN) B | C (*THEN) D ) / | |
4477 | ||
4478 | =begin original | |
4479 | ||
4480 | is not the same as | |
4481 | ||
4482 | =end original | |
4483 | ||
4484 | は次と同じではありません | |
4485 | ||
4486 | / ( A (*PRUNE) B | C (*PRUNE) D ) / | |
4487 | ||
4488 | =begin original | |
4489 | ||
4490 | as after matching the A but failing on the B the C<(*THEN)> verb will | |
4491 | backtrack and try C; but the C<(*PRUNE)> verb will simply fail. | |
4492 | ||
4493 | =end original | |
4494 | ||
4495 | A にマッチングしたけれど B に失敗した後 C<(*THEN)> 記号はバックトラックして | |
4496 | C を試みます; しかし C<(*PRUNE)> 記号であれば単純に失敗します。 | |
4497 | ||
4498 | 5526 | =back |
4499 | 5527 | |
4500 | =item Verbs without an argument | |
4501 | ||
4502 | =over 4 | |
4503 | ||
4504 | =item C<(*COMMIT)> | |
4505 | X<(*COMMIT)> | |
4506 | ||
4507 | =begin original | |
4508 | ||
4509 | This is the Perl 6 "commit pattern" C<< <commit> >> or C<:::>. It's a | |
4510 | zero-width pattern similar to C<(*SKIP)>, except that when backtracked | |
4511 | into on failure it causes the match to fail outright. No further attempts | |
4512 | to find a valid match by advancing the start pointer will occur again. | |
4513 | For example, | |
4514 | ||
4515 | =end original | |
4516 | ||
4517 | これは Perl 6 の"コミットパターン" C<< <commit> >> または C<:::> です。 | |
4518 | これは C<(*SKIP)> と似たゼロ幅のパターンですが、失敗でバックトラックした | |
4519 | 際にマッチングがすぐに失敗する点で異なります。 | |
4520 | それ以降で開始位置を進めて有効なマッチングを探す試行は行われません。 | |
4521 | 例えば、 | |
4522 | ||
4523 | 'aaabaaab' =~ /a+b?(*COMMIT)(?{print "$&\n"; $count++})(*FAIL)/; | |
4524 | print "Count=$count\n"; | |
4525 | ||
4526 | =begin original | |
4527 | ||
4528 | outputs | |
4529 | ||
4530 | =end original | |
4531 | ||
4532 | これの出力は | |
4533 | ||
4534 | aaab | |
4535 | Count=1 | |
4536 | ||
4537 | =begin original | |
4538 | ||
4539 | In other words, once the C<(*COMMIT)> has been entered, and if the pattern | |
4540 | does not match, the regex engine will not try any further matching on the | |
4541 | rest of the string. | |
4542 | ||
4543 | =end original | |
4544 | ||
4545 | 言い換えると、いったん C<(*COMMIT)> に入った後に、そのパターンが | |
4546 | マッチングしなかったのなら、正規表現エンジンは文字列の残りに対して | |
4547 | それ以上のマッチングを試みません。 | |
4548 | ||
4549 | =item C<(*FAIL)> C<(*F)> | |
4550 | X<(*FAIL)> X<(*F)> | |
4551 | ||
4552 | =begin original | |
4553 | ||
4554 | This pattern matches nothing and always fails. It can be used to force the | |
4555 | engine to backtrack. It is equivalent to C<(?!)>, but easier to read. In | |
4556 | fact, C<(?!)> gets optimised into C<(*FAIL)> internally. | |
4557 | ||
4558 | =end original | |
4559 | ||
4560 | このパターンは何にもマッチングせず常に失敗します。 | |
4561 | これはエンジンを強制的にバックトラックさせるために使えます。 | |
4562 | これは C<(?!)> と等価ですが、より読みやすくなっています。 | |
4563 | 実際、C<(?!)> は内部的には C<(*FAIL)> に最適化されます。 | |
4564 | ||
4565 | =begin original | |
4566 | ||
4567 | It is probably useful only when combined with C<(?{})> or C<(??{})>. | |
4568 | ||
4569 | =end original | |
4570 | ||
4571 | これはおそらく C<(?{})> または C<(??{})> と組み合わせた時にだけ | |
4572 | 役に立つでしょう。 | |
4573 | ||
4574 | =item C<(*ACCEPT)> | |
4575 | X<(*ACCEPT)> | |
4576 | ||
4577 | =begin original | |
4578 | ||
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 | This pattern matches nothing and causes the end of successful matching at | |
4590 | the point at which the C<(*ACCEPT)> pattern was encountered, regardless of | |
4591 | whether there is actually more to match in the string. When inside of a | |
4592 | nested pattern, such as recursion, or in a subpattern dynamically generated | |
4593 | via C<(??{})>, only the innermost pattern is ended immediately. | |
4594 | ||
4595 | =end original | |
4596 | ||
4597 | このパターンマッチングは何もせず C<(*ACCEPT)> パターンと遭遇した場所で | |
4598 | 文字列の中で実際にもっとマッチングするものがあるかどうかにかかわらず | |
4599 | 成功のマッチングを終了させます。 | |
4600 | 再帰、または C<(??{})> といったネストしたパターンの内側では、一番内側の | |
4601 | パターンのみがすぐに終了します。 | |
4602 | ||
4603 | =begin original | |
4604 | ||
4605 | If the C<(*ACCEPT)> is inside of capturing groups then the groups are | |
4606 | marked as ended at the point at which the C<(*ACCEPT)> was encountered. | |
4607 | For instance: | |
4608 | ||
4609 | =end original | |
4610 | ||
4611 | C<(*ACCEPT)> が捕捉グループの内側で使われた場合捕捉グループは | |
4612 | C<(*ACCEPT)> と遭遇した位置で終了とマークされます。 | |
4613 | 例えば: | |
4614 | ||
4615 | 'AB' =~ /(A (A|B(*ACCEPT)|C) D)(E)/x; | |
4616 | ||
4617 | =begin original | |
4618 | ||
4619 | will match, and C<$1> will be C<AB> and C<$2> will be C<B>, C<$3> will not | |
4620 | 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. | |
4622 | ||
4623 | =end original | |
4624 | ||
4625 | はマッチングし、C<$1> は C<AB> になり、C<$2> は C<B> に、そして | |
4626 | C<$3> は設定されません。 | |
4627 | 'ACDE' のようにかっこの内側で他の分岐がマッチングしたのなら、C<D> 及び | |
4628 | C<E> もマッチングします。 | |
4629 | ||
4630 | =back | |
4631 | ||
4632 | =back | |
4633 | ||
4634 | 5528 | =head2 Backtracking |
4635 | 5529 | X<backtrack> X<backtracking> |
4636 | 5530 | |
4637 | 5531 | (バックトラック) |
4638 | 5532 | |
4639 | 5533 | =begin original |
4640 | 5534 | |
4641 | 5535 | NOTE: This section presents an abstract approximation of regular |
4642 | 5536 | expression behavior. For a more rigorous (and complicated) view of |
4643 | 5537 | the rules involved in selecting a match among possible alternatives, |
4644 | see L<Combining RE Pieces>. | |
5538 | see L</Combining RE Pieces>. | |
4645 | 5539 | |
4646 | 5540 | =end original |
4647 | 5541 | |
4648 | 5542 | 補足: このセクションでは正規表現の振る舞いに関する抽象的な概要を |
4649 | 5543 | 説明します。 |
4650 | 5544 | 可能な代替におけるマッチングの選択におけるルールの厳密な(そして複雑な) |
4651 | 説明は L<Combining RE Pieces> を参照してください。 | |
5545 | 説明は L</Combining RE Pieces> を参照してください。 | |
4652 | 5546 | |
4653 | 5547 | =begin original |
4654 | 5548 | |
4655 | 5549 | A fundamental feature of regular expression matching involves the |
4656 | 5550 | notion called I<backtracking>, which is currently used (when needed) |
4657 | by all regular non-possessive expression quantifiers, namely C<*>, C<*?>, C<+>, | |
5551 | by all regular non-possessive expression quantifiers, namely C<"*">, C<*?>, C<"+">, | |
4658 | 5552 | C<+?>, C<{n,m}>, and C<{n,m}?>. Backtracking is often optimized |
4659 | 5553 | internally, but the general principle outlined here is valid. |
4660 | 5554 | |
4661 | 5555 | =end original |
4662 | 5556 | |
4663 | 5557 | 正規表現マッチングの基本的な機能には最近(必要であれば)すべての強欲でない |
4664 | 正規表現量指定子、つまり、 | |
5558 | 正規表現量指定子、つまり、 | |
5559 | C<"*">, C<*?>, C<"+">, C<+?>, C<{n,m}>, C<{n,m}?> で | |
4665 | 5560 | 使われる I<バックトラッキング> と呼ばれる概念が含まれています。 |
4666 | 5561 | バックトラックはしばしば内部で最適化されますが、ここで概説する一般的な |
4667 | 5562 | 原則は妥当です。 |
4668 | 5563 | |
4669 | 5564 | =begin original |
4670 | 5565 | |
4671 | 5566 | For a regular expression to match, the I<entire> regular expression must |
4672 | 5567 | match, not just part of it. So if the beginning of a pattern containing a |
4673 | 5568 | quantifier succeeds in a way that causes later parts in the pattern to |
4674 | 5569 | fail, the matching engine backs up and recalculates the beginning |
4675 | 5570 | part--that's why it's called backtracking. |
4676 | 5571 | |
4677 | 5572 | =end original |
4678 | 5573 | |
4679 | 5574 | 正規表現がマッチングする時、その正規表現の一部ではなく、 |
4680 | 5575 | I<全体> がマッチングしなければなりません。 |
4681 | 5576 | そのためもしパターンの前半にパターンの後半部分を失敗させてしまう |
4682 | 5577 | 量指定子が含まれているのなら、マッチングングエンジンはいったん戻って |
4683 | 5578 | 開始位置を再計算します -- これがバックトラッキングと呼ばれる所以です。 |
4684 | 5579 | |
4685 | 5580 | =begin original |
4686 | 5581 | |
4687 | 5582 | Here is an example of backtracking: Let's say you want to find the |
4688 | 5583 | word following "foo" in the string "Food is on the foo table.": |
4689 | 5584 | |
4690 | 5585 | =end original |
4691 | 5586 | |
4692 | 5587 | バックトラッキングの例をあげてみます: "Food is on the foo table." という |
4693 | 5588 | 文字列の中で "foo" に続く単語を取り出してください: |
4694 | 5589 | |
4695 | 5590 | $_ = "Food is on the foo table."; |
4696 | 5591 | if ( /\b(foo)\s+(\w+)/i ) { |
4697 | 5592 | print "$2 follows $1.\n"; |
4698 | 5593 | } |
4699 | 5594 | |
4700 | 5595 | =begin original |
4701 | 5596 | |
4702 | 5597 | When the match runs, the first part of the regular expression (C<\b(foo)>) |
4703 | 5598 | 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 | |
5599 | C<$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 | |
5600 | no whitespace following the "Foo" that it had saved in C<$1>, it realizes its | |
4706 | 5601 | mistake and starts over again one character after where it had the |
4707 | 5602 | tentative match. This time it goes all the way until the next occurrence |
4708 | 5603 | of "foo". The complete regular expression matches this time, and you get |
4709 | 5604 | the expected output of "table follows foo." |
4710 | 5605 | |
4711 | 5606 | =end original |
4712 | 5607 | |
4713 | 5608 | マッチングが実行される時、正規表現の最初の部分 (C<\b(foo)>) は開始文字列の |
4714 | 右側で可能なマッチングを探します; そして $1 に "Foo" をロードします。 | |
5609 | 右側で可能なマッチングを探します; そして C<$1> に "Foo" をロードします。 | |
4715 | しかし、すぐにマッチングエンジンは $1 に保存した "Foo" の後に空白が | |
5610 | しかし、すぐにマッチングエンジンは C<$1> に保存した "Foo" の後に空白が | |
4716 | 5611 | 無いことを見つけ、それが失敗だったことを検出して仮にマッチングさせた |
4717 | 場所の1文字後から開始します。 | |
5612 | 場所の 1 文字後から開始します。 | |
4718 | 5613 | この時次の "foo" の出現まで進みます。 |
4719 | 5614 | この時に正規表現は完全にマッチングし、予測した出力 "table follows foo." を |
4720 | 5615 | 得ます。 |
4721 | 5616 | |
4722 | 5617 | =begin original |
4723 | 5618 | |
4724 | 5619 | Sometimes minimal matching can help a lot. Imagine you'd like to match |
4725 | 5620 | everything between "foo" and "bar". Initially, you write something |
4726 | 5621 | like this: |
4727 | 5622 | |
4728 | 5623 | =end original |
4729 | 5624 | |
4730 | 5625 | 最小マッチングが役立つこともあります。 |
4731 | 5626 | "foo" と "bar" の間の全てにマッチングしたいと考えてください。 |
4732 | 5627 | 最初に、次のように書くかもしれません: |
4733 | 5628 | |
4734 | 5629 | $_ = "The food is under the bar in the barn."; |
4735 | 5630 | if ( /foo(.*)bar/ ) { |
4736 | 5631 | print "got <$1>\n"; |
4737 | 5632 | } |
4738 | 5633 | |
4739 | 5634 | =begin original |
4740 | 5635 | |
4741 | 5636 | Which perhaps unexpectedly yields: |
4742 | 5637 | |
4743 | 5638 | =end original |
4744 | 5639 | |
4745 | 5640 | しかしこれは考えたのと違う結果となるでしょう: |
4746 | 5641 | |
4747 | 5642 | got <d is under the bar in the > |
4748 | 5643 | |
4749 | 5644 | =begin original |
4750 | 5645 | |
4751 | 5646 | That's because C<.*> was greedy, so you get everything between the |
4752 | 5647 | I<first> "foo" and the I<last> "bar". Here it's more effective |
4753 | 5648 | to use minimal matching to make sure you get the text between a "foo" |
4754 | 5649 | and the first "bar" thereafter. |
4755 | 5650 | |
4756 | 5651 | =end original |
4757 | 5652 | |
4758 | 5653 | これは C<.*> が貪欲であり、そのために I<最初の> "foo" と I<最後の> |
4759 | 5654 | "bar" の間にある全てを取り出してしまいます。 |
4760 | 5655 | 次に "foo" とその後の最初の "bar" の間にあるテキストを取り出す |
4761 | 5656 | 最小マッチングを使ったもっと効率的な方法を示します: |
4762 | 5657 | |
4763 | 5658 | if ( /foo(.*?)bar/ ) { print "got <$1>\n" } |
4764 | 5659 | got <d is under the > |
4765 | 5660 | |
4766 | 5661 | =begin original |
4767 | 5662 | |
4768 | 5663 | Here's another example. Let's say you'd like to match a number at the end |
4769 | 5664 | of a string, and you also want to keep the preceding part of the match. |
4770 | 5665 | So you write this: |
4771 | 5666 | |
4772 | 5667 | =end original |
4773 | 5668 | |
4774 | 5669 | 別の例も出してみます。 |
4775 | 5670 | 文字列の最後にある数字にマッチングさせて、そのマッチングの前の部分も |
4776 | 5671 | 保持させてみましょう。 |
4777 | 5672 | そしてあなたは次のように書くかもしれません。 |
4778 | 5673 | |
4779 | 5674 | $_ = "I have 2 numbers: 53147"; |
4780 | 5675 | if ( /(.*)(\d*)/ ) { # Wrong! |
4781 | 5676 | print "Beginning is <$1>, number is <$2>.\n"; |
4782 | 5677 | } |
4783 | 5678 | |
4784 | 5679 | =begin original |
4785 | 5680 | |
4786 | 5681 | That won't work at all, because C<.*> was greedy and gobbled up the |
4787 | 5682 | whole string. As C<\d*> can match on an empty string the complete |
4788 | 5683 | regular expression matched successfully. |
4789 | 5684 | |
4790 | 5685 | =end original |
4791 | 5686 | |
4792 | 5687 | これは全く動作しません、なぜなら C<.*> は貪欲であり文字列全体を |
4793 | 5688 | 飲み込んでしまいます。 |
4794 | 5689 | C<\d*> は空の文字列にマッチングできるので正規表現は完全に正常に |
4795 | 5690 | マッチングします。 |
4796 | 5691 | |
4797 | 5692 | Beginning is <I have 2 numbers: 53147>, number is <>. |
4798 | 5693 | |
4799 | 5694 | =begin original |
4800 | 5695 | |
4801 | 5696 | Here are some variants, most of which don't work: |
4802 | 5697 | |
4803 | 5698 | =end original |
4804 | 5699 | |
4805 | 5700 | 動作しない主なバリエーションをあげておきます: |
4806 | 5701 | |
4807 | 5702 | $_ = "I have 2 numbers: 53147"; |
4808 | 5703 | @pats = qw{ |
4809 | 5704 | (.*)(\d*) |
4810 | 5705 | (.*)(\d+) |
4811 | 5706 | (.*?)(\d*) |
4812 | 5707 | (.*?)(\d+) |
4813 | 5708 | (.*)(\d+)$ |
4814 | 5709 | (.*?)(\d+)$ |
4815 | 5710 | (.*)\b(\d+)$ |
4816 | 5711 | (.*\D)(\d+)$ |
4817 | 5712 | }; |
4818 | 5713 | |
4819 | 5714 | for $pat (@pats) { |
4820 | 5715 | printf "%-12s ", $pat; |
4821 | 5716 | if ( /$pat/ ) { |
4822 | 5717 | print "<$1> <$2>\n"; |
4823 | 5718 | } else { |
4824 | 5719 | print "FAIL\n"; |
4825 | 5720 | } |
4826 | 5721 | } |
4827 | 5722 | |
4828 | 5723 | =begin original |
4829 | 5724 | |
4830 | 5725 | That will print out: |
4831 | 5726 | |
4832 | 5727 | =end original |
4833 | 5728 | |
4834 | 5729 | これらの結果は次のようになります: |
4835 | 5730 | |
4836 | 5731 | (.*)(\d*) <I have 2 numbers: 53147> <> |
4837 | 5732 | (.*)(\d+) <I have 2 numbers: 5314> <7> |
4838 | 5733 | (.*?)(\d*) <> <> |
4839 | 5734 | (.*?)(\d+) <I have > <2> |
4840 | 5735 | (.*)(\d+)$ <I have 2 numbers: 5314> <7> |
4841 | 5736 | (.*?)(\d+)$ <I have 2 numbers: > <53147> |
4842 | 5737 | (.*)\b(\d+)$ <I have 2 numbers: > <53147> |
4843 | 5738 | (.*\D)(\d+)$ <I have 2 numbers: > <53147> |
4844 | 5739 | |
4845 | 5740 | =begin original |
4846 | 5741 | |
4847 | 5742 | As you see, this can be a bit tricky. It's important to realize that a |
4848 | 5743 | regular expression is merely a set of assertions that gives a definition |
4849 | 5744 | of success. There may be 0, 1, or several different ways that the |
4850 | 5745 | definition might succeed against a particular string. And if there are |
4851 | 5746 | multiple ways it might succeed, you need to understand backtracking to |
4852 | 5747 | know which variety of success you will achieve. |
4853 | 5748 | |
4854 | 5749 | =end original |
4855 | 5750 | |
4856 | 5751 | このように、これは幾分トリッキーです。 |
4857 | 5752 | 重要なのは正規表現は成功の定義を定める主張の集合にすぎないことを |
4858 | 5753 | 認識することです。 |
4859 | 5754 | 特定の文字列で成功となる定義には 0, 1 または複数の違ったやり方が存在します。 |
4860 | 5755 | そしてもし成功する複数の方法が存在するのなら成功したうちのどれが目的と |
4861 | 5756 | するものなのかを知るためにバックトラッキングを理解しておく必要があります。 |
4862 | 5757 | |
4863 | 5758 | =begin original |
4864 | 5759 | |
4865 | When using look | |
5760 | When using lookahead assertions and negations, this can all get even | |
4866 | 5761 | trickier. Imagine you'd like to find a sequence of non-digits not |
4867 | 5762 | followed by "123". You might try to write that as |
4868 | 5763 | |
4869 | 5764 | =end original |
4870 | 5765 | |
4871 | 5766 | 前読みの言明及び否定を使っている時にはこれはますますトリッキーになります。 |
4872 | 5767 | "123" が後ろに続かない数字以外の列を探したいと考えてみてください。 |
4873 | 5768 | あなたは次のように書くかもしれません。 |
4874 | 5769 | |
4875 | 5770 | $_ = "ABC123"; |
4876 | 5771 | if ( /^\D*(?!123)/ ) { # Wrong! |
4877 | 5772 | print "Yup, no 123 in $_\n"; |
4878 | 5773 | } |
4879 | 5774 | |
4880 | 5775 | =begin original |
4881 | 5776 | |
4882 | 5777 | But that isn't going to match; at least, not the way you're hoping. It |
4883 | 5778 | claims that there is no 123 in the string. Here's a clearer picture of |
4884 | 5779 | why that pattern matches, contrary to popular expectations: |
4885 | 5780 | |
4886 | 5781 | =end original |
4887 | 5782 | |
4888 | 5783 | ですがこれはマッチングしません; 少なくともなってほしかったようには。 |
4889 | 5784 | これは文字列の中に 123 がないことを要求します。 |
4890 | 5785 | よくある予想と比較してなぜパターンがマッチングするのかのわかりやすい |
4891 | 5786 | 説明を次に示します: |
4892 | 5787 | |
4893 | 5788 | $x = 'ABC123'; |
4894 | 5789 | $y = 'ABC445'; |
4895 | 5790 | |
4896 | 5791 | print "1: got $1\n" if $x =~ /^(ABC)(?!123)/; |
4897 | 5792 | print "2: got $1\n" if $y =~ /^(ABC)(?!123)/; |
4898 | 5793 | |
4899 | 5794 | print "3: got $1\n" if $x =~ /^(\D*)(?!123)/; |
4900 | 5795 | print "4: got $1\n" if $y =~ /^(\D*)(?!123)/; |
4901 | 5796 | |
4902 | 5797 | =begin original |
4903 | 5798 | |
4904 | 5799 | This prints |
4905 | 5800 | |
4906 | 5801 | =end original |
4907 | 5802 | |
4908 | 5803 | これは次の出力となります |
4909 | 5804 | |
4910 | 5805 | 2: got ABC |
4911 | 5806 | 3: got AB |
4912 | 5807 | 4: got ABC |
4913 | 5808 | |
4914 | 5809 | =begin original |
4915 | 5810 | |
4916 | 5811 | You might have expected test 3 to fail because it seems to a more |
4917 | 5812 | general purpose version of test 1. The important difference between |
4918 | 5813 | them is that test 3 contains a quantifier (C<\D*>) and so can use |
4919 | 5814 | 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 | |
5815 | that you've asked "Is it true that at the start of C<$x>, following 0 or more | |
4921 | 5816 | non-digits, you have something that's not 123?" If the pattern matcher had |
4922 | 5817 | let C<\D*> expand to "ABC", this would have caused the whole pattern to |
4923 | 5818 | fail. |
4924 | 5819 | |
4925 | 5820 | =end original |
4926 | 5821 | |
4927 | 5822 | テスト 3 はテスト 1 のより一般的なバージョンなのでそれが失敗すると |
4928 | 5823 | 考えたかもしれません。 |
4929 | 5824 | この 2 つの重要な違いは、テスト 3 には量指定子(C<\D*>)が含まれているので |
4930 | 5825 | テスト1ではできなかったバックトラッキングを行うことが |
4931 | 5826 | できるところにあります。 |
4932 | ここであなたは「$x のはじめで 0 個以上の非数字があるから 123 ではない | |
5827 | ここであなたは「C<$x> のはじめで 0 個以上の非数字があるから 123 ではない | |
4933 | 5828 | 何かを得られるんじゃないの?」と聞くでしょう。 |
4934 | 5829 | このパターンマッチングが C<\D*> を "ABC" に展開させると |
4935 | 5830 | これはパターン全体を失敗させることになります。 |
4936 | 5831 | |
4937 | 5832 | =begin original |
4938 | 5833 | |
4939 | 5834 | The search engine will initially match C<\D*> with "ABC". Then it will |
4940 | 5835 | try to match C<(?!123)> with "123", which fails. But because |
4941 | 5836 | a quantifier (C<\D*>) has been used in the regular expression, the |
4942 | 5837 | search engine can backtrack and retry the match differently |
4943 | 5838 | in the hope of matching the complete regular expression. |
4944 | 5839 | |
4945 | 5840 | =end original |
4946 | 5841 | |
4947 | 5842 | 探索エンジンは最初に C<\D*> を "ABC" にマッチングさせます。 |
4948 | 5843 | そして C<(?!123)> を "123" にマッチングさせ、これは失敗します。 |
4949 | 5844 | けれども量指定子 (C<\D*>) が正規表現の中で使われているので、探索エンジンは |
4950 | 5845 | バックトラックしてこの正規表現全体をマッチングさせるように異なるマッチングを |
4951 | 5846 | 行うことができます。 |
4952 | 5847 | |
4953 | 5848 | =begin original |
4954 | 5849 | |
4955 | 5850 | The pattern really, I<really> wants to succeed, so it uses the |
4956 | 5851 | standard pattern back-off-and-retry and lets C<\D*> expand to just "AB" this |
4957 | 5852 | time. Now there's indeed something following "AB" that is not |
4958 | 5853 | "123". It's "C123", which suffices. |
4959 | 5854 | |
4960 | 5855 | =end original |
4961 | 5856 | |
4962 | 5857 | このパターンは本当に、I<本当に> 成功したいので、これは標準的なパターンの |
4963 | 5858 | 後退再試行を行い、この時に C<\D*> を "AB" のみに展開させます。 |
4964 | 5859 | そして確かに "AB" の後ろは "123" ではありません。 |
4965 | 5860 | "C123" は十分満たしています。 |
4966 | 5861 | |
4967 | 5862 | =begin original |
4968 | 5863 | |
4969 | 5864 | 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 | |
5865 | We'll say that the first part in C<$1> must be followed both by a digit | |
4971 | and by something that's not "123". Remember that the look | |
5866 | and by something that's not "123". Remember that the lookaheads | |
4972 | 5867 | are zero-width expressions--they only look, but don't consume any |
4973 | 5868 | of the string in their match. So rewriting this way produces what |
4974 | 5869 | you'd expect; that is, case 5 will fail, but case 6 succeeds: |
4975 | 5870 | |
4976 | 5871 | =end original |
4977 | 5872 | |
4978 | 5873 | これは言明と否定の両方を使うことで処理することができます。 |
4979 | $1 の最初の部分は数字が続きかつそれは "123" ではないことを宣言します。 | |
5874 | C<$1> の最初の部分は数字が続きかつそれは "123" ではないことを宣言します。 | |
4980 | 5875 | 先読みはゼロ幅の式なのでそれがマッチングした文字列を全く消費しないことを |
4981 | 5876 | 思い出してください。 |
4982 | 5877 | そしてこれを必要なものを生成するように書き換えます; |
4983 | 5878 | つまり、5 のケースでは失敗し、6 のケースは成功します: |
4984 | 5879 | |
4985 | 5880 | print "5: got $1\n" if $x =~ /^(\D*)(?=\d)(?!123)/; |
4986 | 5881 | print "6: got $1\n" if $y =~ /^(\D*)(?=\d)(?!123)/; |
4987 | 5882 | |
4988 | 5883 | 6: got ABC |
4989 | 5884 | |
4990 | 5885 | =begin original |
4991 | 5886 | |
4992 | 5887 | In other words, the two zero-width assertions next to each other work as though |
4993 | 5888 | they're ANDed together, just as you'd use any built-in assertions: C</^$/> |
4994 | 5889 | matches only if you're at the beginning of the line AND the end of the |
4995 | 5890 | line simultaneously. The deeper underlying truth is that juxtaposition in |
4996 | 5891 | regular expressions always means AND, except when you write an explicit OR |
4997 | 5892 | using the vertical bar. C</ab/> means match "a" AND (then) match "b", |
4998 | 5893 | although the attempted matches are made at different positions because "a" |
4999 | 5894 | is not a zero-width assertion, but a one-width assertion. |
5000 | 5895 | |
5001 | 5896 | =end original |
5002 | 5897 | |
5003 | 5898 | 言い換えると、このそれぞれの次にある2つのゼロ幅の言明はちょうど何か組み込みの |
5004 | 5899 | 言明を使ったかのようにそれらがともに AND されているかのように動作します: |
5005 | 5900 | C</^$/> は行の始まりで且つ同時に行の終了でる時にのみマッチングします。 |
5006 | 5901 | もっと深部での真実は、併記された正規表現は垂直線を使って明示的に OR を |
5007 | 5902 | 書いたとき以外は常に AND を意味します。 |
5008 | 5903 | C</ab/> は、"a" がゼロ幅の言明ではなく 1 文字幅の言明なので異なる場所で |
5009 | 5904 | マッチングが行われはしますが、 "a" にマッチング且つ(そして) "b" に |
5010 | 5905 | マッチングということを意味します。 |
5011 | 5906 | |
5012 | 5907 | =begin original |
5013 | 5908 | |
5014 | 5909 | B<WARNING>: Particularly complicated regular expressions can take |
5015 | 5910 | exponential time to solve because of the immense number of possible |
5016 | 5911 | ways they can use backtracking to try for a match. For example, without |
5017 | 5912 | internal optimizations done by the regular expression engine, this will |
5018 | 5913 | take a painfully long time to run: |
5019 | 5914 | |
5020 | 5915 | =end original |
5021 | 5916 | |
5022 | 5917 | B<警告>: 特にコンパイルされた正規表現はマッチングのために |
5023 | 5918 | できる限りのバックトラックを非常に多くの回数行うので |
5024 | 5919 | 解くために指数的な時間を必要とすることがあります。 |
5025 | 5920 | 例えば、正規表現エンジンの内部で行われる最適化がなかったときには、次の評価は |
5026 | 5921 | 尋常じゃないくらい長時間かかります: |
5027 | 5922 | |
5028 | 5923 | 'aaaaaaaaaaaa' =~ /((a{0,5}){0,5})*[c]/ |
5029 | 5924 | |
5030 | 5925 | =begin original |
5031 | 5926 | |
5032 | And if you used C<*>'s in the internal groups instead of limiting them | |
5927 | And if you used C<"*">'s in the internal groups instead of limiting them | |
5033 | 5928 | to 0 through 5 matches, then it would take forever--or until you ran |
5034 | 5929 | out of stack space. Moreover, these internal optimizations are not |
5035 | always applicable. For example, if you put C<{0,5}> instead of C<*> | |
5930 | always applicable. For example, if you put C<{0,5}> instead of C<"*"> | |
5036 | 5931 | on the external group, no current optimization is applicable, and the |
5037 | 5932 | match takes a long time to finish. |
5038 | 5933 | |
5039 | 5934 | =end original |
5040 | 5935 | |
5041 | 5936 | そしてもし内側のグループで 0 から 5 回にマッチングを制限する代わりに |
5042 | C<*> を使うと、永久に、またはスタックを使い果たすまで | |
5937 | C<"*"> を使うと、永久に、またはスタックを使い果たすまで | |
5043 | 5938 | 実行し続けることになります。 |
5044 | 5939 | その上、これらの最適化は常にできるわけではありません。 |
5045 | 例えば、外側のグループで C<*> の代わりに C<{0,5}> を使ったときに、現在の | |
5940 | 例えば、外側のグループで C<"*"> の代わりに C<{0,5}> を使ったときに、現在の | |
5046 | 5941 | 最適化は適用されません; そしてマッチングが終わるまでの長い時間が |
5047 | 5942 | 必要になります。 |
5048 | 5943 | |
5049 | 5944 | =begin original |
5050 | 5945 | |
5051 | 5946 | A powerful tool for optimizing such beasts is what is known as an |
5052 | 5947 | "independent group", |
5053 | 5948 | which does not backtrack (see L</C<< (?>pattern) >>>). Note also that |
5054 | zero-length look | |
5949 | zero-length lookahead/lookbehind assertions will not backtrack to make | |
5055 | 5950 | the tail match, since they are in "logical" context: only |
5056 | 5951 | whether they match is considered relevant. For an example |
5057 | where side-effects of look | |
5952 | where side-effects of lookahead I<might> have influenced the | |
5058 | 5953 | following match, see L</C<< (?>pattern) >>>. |
5059 | 5954 | |
5060 | 5955 | =end original |
5061 | 5956 | |
5062 | 5957 | そのような野獣のような最適化のためのパワフルなツールとして |
5063 | 5958 | 知られているものに、「独立グループ」があります; これはバックトラックを |
5064 | 5959 | 行いません (L</C<< (?>pattern) >>> を参照)。 |
5065 | 5960 | ゼロ幅の先読み/後読みの言明も「論理的な」文脈なので末尾のマッチングを |
5066 | 5961 | バックトラックしません: マッチングが関連して考慮されるかどうかだけです。 |
5067 | 5962 | 先読みの言明の副作用がそれに続くマッチングに影響する I<かもしれない> 例は、 |
5068 | 5963 | L</C<< (?>pattern) >>> を参照してください。 |
5069 | 5964 | |
5070 | =head2 | |
5965 | =head2 Script Runs | |
5071 | X< | |
5966 | X<(*script_run:...)> X<(sr:...)> | |
5967 | X<(*atomic_script_run:...)> X<(asr:...)> | |
5072 | 5968 | |
5073 | ( | |
5969 | (用字並び) | |
5074 | 5970 | |
5075 | 5971 | =begin original |
5076 | 5972 | |
5077 | ||
5973 | A script run is basically a sequence of characters, all from the same | |
5078 | ||
5974 | Unicode script (see L<perlunicode/Scripts>), such as Latin or Greek. In | |
5975 | most places a single word would never be written in multiple scripts, | |
5976 | unless it is a spoofing attack. An infamous example, is | |
5079 | 5977 | |
5080 | 5978 | =end original |
5081 | 5979 | |
5082 | ||
5980 | 用字連続は基本的には、ラテン文字やギリシャ文字のような、 | |
5083 | ||
5981 | 全て同じ Unicode 用字 (L<perlunicode/Scripts> 参照) からの文字の並びです。 | |
5982 | ほとんどの場所では、なりすまし攻撃でない限り、一つの単語は決して複数の用字で | |
5983 | 書かれることはありません。 | |
5984 | 悪名高い例は次のものです: | |
5084 | 5985 | |
5986 | paypal.com | |
5987 | ||
5085 | 5988 | =begin original |
5086 | 5989 | |
5087 | ||
5990 | Those letters could all be Latin (as in the example just above), or they | |
5088 | ||
5991 | could be all Cyrillic (except for the dot), or they could be a mixture | |
5089 | ||
5992 | of the two. In the case of an internet address the C<.com> would be in | |
5090 | ||
5993 | Latin, And any Cyrillic ones would cause it to be a mixture, not a | |
5091 | c | |
5994 | script run. Someone clicking on such a link would not be directed to | |
5092 | ||
5995 | the real Paypal website, but an attacker would craft a look-alike one to | |
5996 | attempt to gather sensitive information from the person. | |
5093 | 5997 | |
5094 | 5998 | =end original |
5095 | 5999 | |
5096 | ||
6000 | これらの文字は全て (直前のように) ラテン文字かもしれませんし、 | |
5097 | ||
6001 | (ドットを除いて) 全てキリル文字かもしれませんし、二つの混合かも | |
5098 | ||
6002 | しれません。 | |
5099 | ||
6003 | インターネットアドレスの場合、C<.com> はラテン文字で、 | |
5100 | ||
6004 | キリル文字は混合となり用字連続ではありません。 | |
5101 | この | |
6005 | 誰かがこのようなリンクをクリックすると、本当の Paypal ウェブサイトに | |
6006 | 移動せず、攻撃者がその人から機微情報を集めようとするために | |
6007 | 見た目が似たものを細工するかもしれません。 | |
5102 | 6008 | |
5103 | 6009 | =begin original |
5104 | 6010 | |
5105 | ||
6011 | Starting in Perl 5.28, it is now easy to detect strings that aren't | |
5106 | s | |
6012 | script runs. Simply enclose just about any pattern like either of | |
5107 | ||
6013 | these: | |
5108 | 6014 | |
5109 | 6015 | =end original |
5110 | 6016 | |
5111 | ||
6017 | Perl 5.28 から、用字連続でない文字列を簡単に検出できるようになりました。 | |
5112 | パターン | |
6018 | 単にパターンを次のどちらかのような形で囲みます: | |
5113 | 6019 | |
6020 | (*script_run:pattern) | |
6021 | (*sr:pattern) | |
6022 | ||
5114 | 6023 | =begin original |
5115 | 6024 | |
5116 | ||
6025 | What happens is that after I<pattern> succeeds in matching, it is | |
5117 | ||
6026 | subjected to the additional criterion that every character in it must be | |
5118 | f | |
6027 | from the same script (see exceptions below). If this isn't true, | |
5119 | in | |
6028 | backtracking occurs until something all in the same script is found that | |
5120 | ||
6029 | matches, or all possibilities are exhausted. This can cause a lot of | |
5121 | ||
6030 | backtracking, but generally, only malicious input will result in this, | |
5122 | ||
6031 | though the slow down could cause a denial of service attack. If your | |
5123 | es | |
6032 | needs permit, it is best to make the pattern atomic to cut down on the | |
5124 | at | |
6033 | amount of backtracking. This is so likely to be what you want, that | |
5125 | ||
6034 | instead of writing this: | |
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 | 6035 | |
5132 | 6036 | =end original |
5133 | 6037 | |
5134 | ||
6038 | I<pattern> がマッチングした後に次のことが起きます; | |
5135 | ||
6039 | その中の全ての文字が同じ用字であるという追加の条件が課されます | |
5136 | ||
6040 | (後述する例外参照)。 | |
5137 | ||
6041 | これが真でない場合、全て同じ用字でマッチングする何かが見つかるか、 | |
5138 | ||
6042 | 全ての可能性がなくなるまで、バックトラッキングが発生します。 | |
5139 | ||
6043 | これは多くのバックトラッキングを引き起こしますが、一般的に、 | |
5140 | ||
6044 | 悪意のある入力だけがこれを引き起こします; | |
5141 | ||
6045 | しかし、この速度低下はサービス不能攻撃を引き起こすかもしれません。 | |
5142 | ||
6046 | 事情が許すなら、バックトラッキングの量を減らすためにパターンを | |
6047 | アトミックに書くのが最善です。 | |
6048 | これはおそらくあなたが求めているものなので、次のように書く代わりに: | |
6049 | ||
6050 | (*script_run:(?>pattern)) | |
6051 | ||
6052 | =begin original | |
6053 | ||
6054 | you can write either of these: | |
6055 | ||
6056 | =end original | |
6057 | ||
6058 | 次のどちらかのように書けます: | |
6059 | ||
6060 | (*atomic_script_run:pattern) | |
6061 | (*asr:pattern) | |
6062 | ||
6063 | =begin original | |
6064 | ||
6065 | (See L</C<(?E<gt>I<pattern>)>>.) | |
6066 | ||
6067 | =end original | |
6068 | ||
6069 | (L</C<(?E<gt>I<pattern>)>> を参照してください。) | |
6070 | ||
6071 | =begin original | |
6072 | ||
6073 | In Taiwan, Japan, and Korea, it is common for text to have a mixture of | |
6074 | characters from their native scripts and base Chinese. Perl follows | |
6075 | Unicode's UTS 39 (L<http://unicode.org/reports/tr39/>) Unicode Security | |
6076 | Mechanisms in allowing such mixtures. For example, the Japanese scripts | |
6077 | Katakana and Hiragana are commonly mixed together in practice, along | |
6078 | with some Chinese characters, and hence are treated as being in a single | |
6079 | script run by Perl. | |
6080 | ||
6081 | =end original | |
6082 | ||
6083 | 台湾、日本、韓国では、独自の用字からの文字と中国語を基にした文字が | |
6084 | 混合している文章は一般的です。 | |
6085 | Perl はこのような混合を許すために Unicode の UTS 39 | |
6086 | (L<http://unicode.org/reports/tr39/>) Unicode Security Mechanisms に | |
6087 | 従います。 | |
6088 | 例えば、日本語用字のカタカナとひらがなは実際には一部の中国語文字と共に | |
6089 | 混合しているのが一般的なので、Perl によって単一の用字連続として | |
5143 | 6090 | 扱われます。 |
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 | 6091 | |
5151 | 6092 | =begin original |
5152 | 6093 | |
5153 | ||
6094 | The rules used for matching decimal digits are slightly stricter. Many | |
5154 | cha | |
6095 | scripts have their own sets of digits equivalent to the Western C<0> | |
5155 | ||
6096 | through C<9> ones. A few, such as Arabic, have more than one set. For | |
5156 | ||
6097 | a string to be considered a script run, all digits in it must come from | |
5157 | ||
6098 | the same set of ten, as determined by the first digit encountered. | |
5158 | s | |
6099 | As an example, | |
5159 | 6100 | |
5160 | 6101 | =end original |
5161 | 6102 | |
5162 | ||
6103 | 10 進数字とマッチングするために使われる規則は少し厳密になります。 | |
5163 | ||
6104 | 多くの用字は、西洋の C<0> から C<9> と等価な独自の数字の集合を持ちます。 | |
5164 | ||
6105 | アラビア文字のように、複数の集合を持つものもあります。 | |
5165 | ||
6106 | 用字連続が考慮される文字列については、全ての数字は、遭遇した | |
5166 | ||
6107 | 最初の数字で決定されるものと同じ集合でなければなりません。 | |
5167 | ||
6108 | 例えば: | |
5168 | 6109 | |
6110 | qr/(*script_run: \d+ \b )/x | |
6111 | ||
5169 | 6112 | =begin original |
5170 | 6113 | |
5171 | ||
6114 | guarantees that the digits matched will all be from the same set of 10. | |
5172 | u | |
6115 | You won't get a look-alike digit from a different script that has a | |
5173 | ||
6116 | different value than what it appears to be. | |
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 | 6117 | |
5180 | 6118 | =end original |
5181 | 6119 | |
5182 | ||
6120 | これはマッチングした数字が全て同じ集合の 10 文字からであることを保証します。 | |
5183 | ||
6121 | 見た目と異なる値を持つ、異なる用字からの数字に見えるものを得ることは | |
5184 | ||
6122 | ありません。 | |
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 | 6123 | |
5193 | 6124 | =begin original |
5194 | 6125 | |
5195 | ||
6126 | Unicode has three pseudo scripts that are handled specially. | |
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 | 6127 | |
5205 | 6128 | =end original |
5206 | 6129 | |
5207 | ||
6130 | Unicode には特別に扱われる三つの疑似用字があります。 | |
5208 | C<fee|fie|foe> は対象の文字列の "fee"、"fie"、または "foe" のいずれかに | |
5209 | (C<f(e|i|o)e> のように)マッチングします。 | |
5210 | 最初の代替には最後のパターン区切り ("(", "(?:" など、またはパターンの始まり)から | |
5211 | 最初の "|" までのすべてが含まれ、最後の代替には最後の "|" から | |
5212 | 次の閉じパターン区切りまでが含まれます。 | |
5213 | 通常代替をかっこの中に入れるのは、その開始位置と終了位置が少しはわかりやすく | |
5214 | なるようにです。 | |
5215 | 6131 | |
5216 | 6132 | =begin original |
5217 | 6133 | |
5218 | ||
6134 | "Unknown" is applied to code points whose meaning has yet to be | |
5219 | ||
6135 | determined. Perl currently will match as a script run, any single | |
5220 | ||
6136 | character string consisting of one of these code points. But any string | |
5221 | ||
6137 | longer than one code point containing one of these will not be | |
5222 | ||
6138 | considered a script run. | |
5223 | matches the target string. (This might not seem important, but it is | |
5224 | important when you are capturing matched text using parentheses.) | |
5225 | 6139 | |
5226 | 6140 | =end original |
5227 | 6141 | |
5228 | ||
6142 | "Unknown" は、意味がまだ決定されていない符号位置に適用されます。 | |
5229 | ||
6143 | Perl は現在の所それらのうちの一つの符号位置からなる 1 文字文字列を | |
5230 | ||
6144 | 用字連続としてマッチングします。 | |
5231 | ||
6145 | しかし、それらの一つを含む 2 符号位置以上の文字列は用字連続として | |
5232 | ||
6146 | 扱われません。 | |
5233 | マッチングします。 | |
5234 | (これは重要ではないでしょうが、かっこを使ってマッチングしたテキストを | |
5235 | 捕捉しているときには重要でしょう。) | |
5236 | 6147 | |
5237 | 6148 | =begin original |
5238 | 6149 | |
5239 | ||
6150 | "Inherited" is applied to characters that modify another, such as an | |
5240 | ||
6151 | accent of some type. These are considered to be in the script of the | |
6152 | master character, and so never cause a script run to not match. | |
5241 | 6153 | |
5242 | 6154 | =end original |
5243 | 6155 | |
5244 | ||
6156 | "Inherited" は、一部のアクセントのように、他のものを変更する文字に | |
5245 | ||
6157 | 適用されます。 | |
6158 | これらは元の文字の用字として扱われるので、 | |
6159 | マッチングしない用字連続になることはありません。 | |
5246 | 6160 | |
5247 | 6161 | =begin original |
5248 | 6162 | |
5249 | ||
6163 | The other one is "Common". This consists of mostly punctuation, emoji, | |
5250 | ||
6164 | and characters used in mathematics and music, the ASCII digits C<0> | |
5251 | ||
6165 | through C<9>, and full-width forms of these digits. These characters | |
5252 | ||
6166 | can appear intermixed in text in many of the world's scripts. These | |
5253 | o | |
6167 | also don't cause a script run to not match. But like other scripts, all | |
5254 | ||
6168 | digits in a run must come from the same set of 10. | |
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 | 6169 | |
5260 | 6170 | =end original |
5261 | 6171 | |
5262 | ||
6172 | もう一つは "Common" です。 | |
5263 | ||
6173 | これはほとんど句読点、絵文字、数学と音楽で使われる文字、 | |
5264 | ||
6174 | ASCII の数字 C<0> から C<9>、およびそれらの全角版 で構成されます。 | |
5265 | ||
6175 | これらの文字は世界中の多くの用字の文章で混ぜられて現れます。 | |
5266 | ||
6176 | これらもまた、マッチングしない用字連続にはなりません。 | |
5267 | ||
6177 | しかし他の幼児と同様、連続の中の全ての数字は 10 の同じ集合からの | |
5268 | ||
6178 | ものでなければなりません。 | |
5269 | "0x1234 01234" にはマッチングしません; なぜなら、C<0|0x> は二つめ数字の | |
5270 | 先頭にある 0 にマッチングすることができるのですが、 | |
5271 | 部分パターン 1 は "0x" にマッチングするためです。 | |
5272 | 6179 | |
5273 | = | |
6180 | =begin original | |
5274 | 6181 | |
5275 | ||
6182 | This construct is non-capturing. You can add parentheses to I<pattern> | |
6183 | to capture, if desired. You will have to do this if you plan to use | |
6184 | L</(*ACCEPT) (*ACCEPT:arg)> and not have it bypass the script run | |
6185 | checking. | |
5276 | 6186 | |
6187 | =end original | |
6188 | ||
6189 | この構文は捕捉しません。 | |
6190 | 捕捉したい場合は、その I<pattern> にかっこを追加できます。 | |
6191 | L</(*ACCEPT) (*ACCEPT:arg)> を使う予定で、用字連続チェックを | |
6192 | 迂回しない場合は、こうすることが必要でしょう。 | |
6193 | ||
5277 | 6194 | =begin original |
5278 | 6195 | |
6196 | This feature is experimental, and the exact syntax and details of | |
6197 | operation are subject to change; using it yields a warning in the | |
6198 | C<experimental::script_run> category. | |
6199 | ||
6200 | =end original | |
6201 | ||
6202 | この機能は実験的で、正確な文法と操作の詳細は変更される予定です; | |
6203 | これを使うと、C<experimental::script_run> カテゴリの警告が出力されます。 | |
6204 | ||
6205 | =begin original | |
6206 | ||
6207 | The C<Script_Extensions> property as modified by UTS 39 | |
6208 | (L<http://unicode.org/reports/tr39/>) is used as the basis for this | |
6209 | feature. | |
6210 | ||
6211 | =end original | |
6212 | ||
6213 | この機能の基礎としては、UTS 39 (L<http://unicode.org/reports/tr39/>) によって | |
6214 | 修正された C<Script_Extensions> 特性が使われます。 | |
6215 | ||
6216 | =begin original | |
6217 | ||
6218 | To summarize, | |
6219 | ||
6220 | =end original | |
6221 | ||
6222 | まとめると: | |
6223 | ||
6224 | =over 4 | |
6225 | ||
6226 | =item * | |
6227 | ||
6228 | =begin original | |
6229 | ||
6230 | All length 0 or length 1 sequences are script runs. | |
6231 | ||
6232 | =end original | |
6233 | ||
6234 | 全ての長さ 0 または長さ 1 の並びは用字連続です。 | |
6235 | ||
6236 | =item * | |
6237 | ||
6238 | =begin original | |
6239 | ||
6240 | A longer sequence is a script run if and only if B<all> of the following | |
6241 | conditions are met: | |
6242 | ||
6243 | =end original | |
6244 | ||
6245 | より長い並びは、以下の条件の B<全て> に合致した場合にのみ用字連続です: | |
6246 | ||
6247 | Z<> | |
6248 | ||
6249 | =over | |
6250 | ||
6251 | =item 1 | |
6252 | ||
6253 | =begin original | |
6254 | ||
6255 | No code point in the sequence has the C<Script_Extension> property of | |
6256 | C<Unknown>. | |
6257 | ||
6258 | =end original | |
6259 | ||
6260 | 並びの中に C<Script_Extension> 特性が C<Unknown> の符号位置がない。 | |
6261 | ||
6262 | =begin original | |
6263 | ||
6264 | This currently means that all code points in the sequence have been | |
6265 | assigned by Unicode to be characters that aren't private use nor | |
6266 | surrogate code points. | |
6267 | ||
6268 | =end original | |
6269 | ||
6270 | これは現在の所、並びの中の全ての符号位置は、私用領域やサロゲート | |
6271 | 符号位置でない、文字として Unicode によって割り当てられていることを | |
6272 | 意味します。 | |
6273 | ||
6274 | =item 2 | |
6275 | ||
6276 | =begin original | |
6277 | ||
6278 | All characters in the sequence come from the Common script and/or the | |
6279 | Inherited script and/or a single other script. | |
6280 | ||
6281 | =end original | |
6282 | ||
6283 | 並びの全ての文字は、Common 用字と Inherited 用字とその他の単一の用字の | |
6284 | いずれかである。 | |
6285 | ||
6286 | =begin original | |
6287 | ||
6288 | The script of a character is determined by the C<Script_Extensions> | |
6289 | property as modified by UTS 39 (L<http://unicode.org/reports/tr39/>), as | |
6290 | described above. | |
6291 | ||
6292 | =end original | |
6293 | ||
6294 | 文字の用字は、前述の UTS 39 (L<http://unicode.org/reports/tr39/>) で | |
6295 | 修正された C<Script_Extensions> 特性によって決定されます。 | |
6296 | ||
6297 | =item 3 | |
6298 | ||
6299 | =begin original | |
6300 | ||
6301 | All decimal digits in the sequence come from the same block of 10 | |
6302 | consecutive digits. | |
6303 | ||
6304 | =end original | |
6305 | ||
6306 | 並びの全ての 10 進数字は 10 の連続した数字の同じブロックからの | |
6307 | ものである。 | |
6308 | ||
6309 | =back | |
6310 | ||
6311 | =back | |
6312 | ||
6313 | =head2 Special Backtracking Control Verbs | |
6314 | ||
6315 | (特殊なバックトラック制御記号) | |
6316 | ||
6317 | =begin original | |
6318 | ||
6319 | These special patterns are generally of the form C<(*I<VERB>:I<arg>)>. Unless | |
6320 | otherwise stated the I<arg> argument is optional; in some cases, it is | |
6321 | mandatory. | |
6322 | ||
6323 | =end original | |
6324 | ||
6325 | これらの特殊なパターンは C<(*I<VERB>:I<arg>)> という一般形式を持っています。 | |
6326 | 特に記されていない限り、I<arg> はオプションです; 一部の場合では、 | |
6327 | これは必須です。 | |
6328 | ||
6329 | =begin original | |
6330 | ||
6331 | Any pattern containing a special backtracking verb that allows an argument | |
6332 | has the special behaviour that when executed it sets the current package's | |
6333 | C<$REGERROR> and C<$REGMARK> variables. When doing so the following | |
6334 | rules apply: | |
6335 | ||
6336 | =end original | |
6337 | ||
6338 | 引数を許可する特殊バックトラック制御記号を含んでいる全てのパターンは、 | |
6339 | それが実行されると現在のパッケージの C<$REGERROR> 及び C<$REGMARK> 変数を | |
6340 | 設定する特殊な振る舞いを持っています。 | |
6341 | これが行われる時以下の手順が適用されます。 | |
6342 | ||
6343 | =begin original | |
6344 | ||
6345 | On failure, the C<$REGERROR> variable will be set to the I<arg> value of the | |
6346 | verb pattern, if the verb was involved in the failure of the match. If the | |
6347 | I<arg> part of the pattern was omitted, then C<$REGERROR> will be set to the | |
6348 | name of the last C<(*MARK:I<NAME>)> pattern executed, or to TRUE if there was | |
6349 | none. Also, the C<$REGMARK> variable will be set to FALSE. | |
6350 | ||
6351 | =end original | |
6352 | ||
6353 | 失敗時には C<$REGERROR> 変数には、記号がマッチングの失敗の中で | |
6354 | 使われていたのならその記号パターンの I<arg> の値がセットされます。 | |
6355 | もしパターンの I<arg> 部分が省略されていたときには、C<$REGERROR> には | |
6356 | 最後に実行された C<(*MARK:I<NAME>)> パターンの名前、またはそれもなければ | |
6357 | 真に設定されます。 | |
6358 | また、C<$REGMARK> 変数は偽に設定されます。 | |
6359 | ||
6360 | =begin original | |
6361 | ||
6362 | On a successful match, the C<$REGERROR> variable will be set to FALSE, and | |
6363 | the C<$REGMARK> variable will be set to the name of the last | |
6364 | C<(*MARK:I<NAME>)> pattern executed. See the explanation for the | |
6365 | C<(*MARK:I<NAME>)> verb below for more details. | |
6366 | ||
6367 | =end original | |
6368 | ||
6369 | マッチングの成功時には、C<$REGERROR> 変数は偽に設定され、C<$REGMARK> 変数には | |
6370 | 最後に実行された C<(*MARK:I<NAME>)> パターンの名前が設定されます。 | |
6371 | 詳細は C<(*MARK:I<NAME>)> 記号の説明を参照してください。 | |
6372 | ||
6373 | =begin original | |
6374 | ||
6375 | B<NOTE:> C<$REGERROR> and C<$REGMARK> are not magic variables like C<$1> | |
6376 | and most other regex-related variables. They are not local to a scope, nor | |
6377 | readonly, but instead are volatile package variables similar to C<$AUTOLOAD>. | |
6378 | They are set in the package containing the code that I<executed> the regex | |
6379 | (rather than the one that compiled it, where those differ). If necessary, you | |
6380 | can use C<local> to localize changes to these variables to a specific scope | |
6381 | before executing a regex. | |
6382 | ||
6383 | =end original | |
6384 | ||
6385 | B<補足:> C<$REGERROR> 及び C<$REGMARK> は C<$1> や他の多くの | |
6386 | 正規表現関連の変数のようにマジック変数ではありません。 | |
6387 | それらはスコープ内にローカルにならず、読み込み専用でもありませんが、 | |
6388 | C<$AUTOLOAD> と似た揮発するパッケージ変数です。 | |
6389 | これらには正規表現が I<実行される> コードを含むパッケージが設定されます | |
6390 | (コンパイルされるコードではありません; これは異なることがあります)。 | |
6391 | 必要な場合は、正規表現を実行する前に | |
6392 | これらの変数の変更を特定のスコープ内に留めるために C<local> を使えます。 | |
6393 | ||
6394 | =begin original | |
6395 | ||
6396 | If a pattern does not contain a special backtracking verb that allows an | |
6397 | argument, then C<$REGERROR> and C<$REGMARK> are not touched at all. | |
6398 | ||
6399 | =end original | |
6400 | ||
6401 | もしパターンが引数を許可する特殊バックトラック記号を含んでなかった場合には、 | |
6402 | C<$REGERROR> 及び C<$REGMARK> は全く触られません。 | |
6403 | ||
6404 | =over 3 | |
6405 | ||
6406 | =item Verbs | |
6407 | ||
6408 | (動詞) | |
6409 | ||
6410 | =over 4 | |
6411 | ||
6412 | =item C<(*PRUNE)> C<(*PRUNE:I<NAME>)> | |
6413 | X<(*PRUNE)> X<(*PRUNE:NAME)> | |
6414 | ||
6415 | =begin original | |
6416 | ||
6417 | This zero-width pattern prunes the backtracking tree at the current point | |
6418 | when backtracked into on failure. Consider the pattern C</I<A> (*PRUNE) I<B>/>, | |
6419 | where I<A> and I<B> are complex patterns. Until the C<(*PRUNE)> verb is reached, | |
6420 | I<A> may backtrack as necessary to match. Once it is reached, matching | |
6421 | continues in I<B>, which may also backtrack as necessary; however, should B | |
6422 | not match, then no further backtracking will take place, and the pattern | |
6423 | will fail outright at the current starting position. | |
6424 | ||
6425 | =end original | |
6426 | ||
6427 | このゼロ幅のパターンは失敗でバックトラックしてきたときに現在の位置で | |
6428 | バックトラックツリーを刈り取ります。 | |
6429 | C</I<A> (*PRUNE) I<B>/> というパターンで I<A> も I<B> も複雑なパターンである時を | |
6430 | 考えてみます。 | |
6431 | C<(*PRUNE)> に達するまでは、I<A> はマッチングに必要であれば | |
6432 | バックトラックしていきます。 | |
6433 | しかし一旦そこに達して I<B> に続くと、そこでも必要に応じてバックトラックします; | |
6434 | しかしながら、B がマッチングしなかったときにはそれ以上のバックトラックは | |
6435 | 行われず、現在の開始位置でのマッチングはすぐに失敗します。 | |
6436 | ||
6437 | =begin original | |
6438 | ||
6439 | The following example counts all the possible matching strings in a | |
6440 | pattern (without actually matching any of them). | |
6441 | ||
6442 | =end original | |
6443 | ||
6444 | 次の例ではパターンに対してマッチングできるすべての文字列を(実際には | |
6445 | マッチングさせずに)数えます。 | |
6446 | ||
6447 | 'aaab' =~ /a+b?(?{print "$&\n"; $count++})(*FAIL)/; | |
6448 | print "Count=$count\n"; | |
6449 | ||
6450 | =begin original | |
6451 | ||
6452 | which produces: | |
6453 | ||
6454 | =end original | |
6455 | ||
6456 | この出力: | |
6457 | ||
6458 | aaab | |
6459 | aaa | |
6460 | aa | |
6461 | a | |
6462 | aab | |
6463 | aa | |
6464 | a | |
6465 | ab | |
6466 | a | |
6467 | Count=9 | |
6468 | ||
6469 | =begin original | |
6470 | ||
6471 | If we add a C<(*PRUNE)> before the count like the following | |
6472 | ||
6473 | =end original | |
6474 | ||
6475 | 次のように数える前に C<(*PRUNE)> を加えると | |
6476 | ||
6477 | 'aaab' =~ /a+b?(*PRUNE)(?{print "$&\n"; $count++})(*FAIL)/; | |
6478 | print "Count=$count\n"; | |
6479 | ||
6480 | =begin original | |
6481 | ||
6482 | we prevent backtracking and find the count of the longest matching string | |
6483 | at each matching starting point like so: | |
6484 | ||
6485 | =end original | |
6486 | ||
6487 | バックトラックを妨げ次のように各開始位置での一番長いマッチング文字列を | |
6488 | 数えるようになります: | |
6489 | ||
6490 | aaab | |
6491 | aab | |
6492 | ab | |
6493 | Count=3 | |
6494 | ||
6495 | =begin original | |
6496 | ||
6497 | Any number of C<(*PRUNE)> assertions may be used in a pattern. | |
6498 | ||
6499 | =end original | |
6500 | ||
6501 | 1つのパターン内で C<(*PRUNE)> 言明はいくつでも使えます。 | |
6502 | ||
6503 | =begin original | |
6504 | ||
6505 | See also C<<< L<< /(?>I<pattern>) >> >>> and possessive quantifiers for | |
6506 | other ways to | |
6507 | control backtracking. In some cases, the use of C<(*PRUNE)> can be | |
6508 | replaced with a C<< (?>pattern) >> with no functional difference; however, | |
6509 | C<(*PRUNE)> can be used to handle cases that cannot be expressed using a | |
6510 | C<< (?>pattern) >> alone. | |
6511 | ||
6512 | =end original | |
6513 | ||
6514 | バックトラックを制御する他の方法として C<<< L<< /(?>I<pattern>) >> >>> | |
6515 | 及び絶対最大量指定子も参照してください。 | |
6516 | 幾つかのケースにおいては C<(*PRUNE)> の利用は機能的な違いなしに | |
6517 | C<< (?>pattern) >> で置き換えることができます; しかしながら C<(*PRUNE)> は | |
6518 | C<< (?>pattern) >> 単独では表現できないケースを扱うために使えます。 | |
6519 | ||
6520 | =item C<(*SKIP)> C<(*SKIP:I<NAME>)> | |
6521 | X<(*SKIP)> | |
6522 | ||
6523 | =begin original | |
6524 | ||
6525 | This zero-width pattern is similar to C<(*PRUNE)>, except that on | |
6526 | failure it also signifies that whatever text that was matched leading up | |
6527 | to the C<(*SKIP)> pattern being executed cannot be part of I<any> match | |
6528 | of this pattern. This effectively means that the regex engine "skips" forward | |
6529 | to this position on failure and tries to match again, (assuming that | |
6530 | there is sufficient room to match). | |
6531 | ||
6532 | =end original | |
6533 | ||
6534 | このゼロ幅のパターンは C<*PRUNE> と似ていますが、実行されている | |
6535 | C<(*SKIP)> パターンまでにマッチングしたテキストはこのパターンの | |
6536 | I<どの>マッチングの一部にもならないことを示します。 | |
6537 | これは正規表現エンジンがこの位置まで失敗として「スキップ」して(マッチングに | |
6538 | 十分な空間があれば)再びマッチングを試みることを効率的に意味します。 | |
6539 | ||
6540 | =begin original | |
6541 | ||
6542 | The name of the C<(*SKIP:I<NAME>)> pattern has special significance. If a | |
6543 | C<(*MARK:I<NAME>)> was encountered while matching, then it is that position | |
6544 | which is used as the "skip point". If no C<(*MARK)> of that name was | |
6545 | encountered, then the C<(*SKIP)> operator has no effect. When used | |
6546 | without a name the "skip point" is where the match point was when | |
6547 | executing the C<(*SKIP)> pattern. | |
6548 | ||
6549 | =end original | |
6550 | ||
6551 | C<(*SKIP:I<NAME>)> パターンの名前部分には特別な意味があります。 | |
6552 | もしマッチングにおいて C<(*MARK:I<NAME>)> に遭遇すると、それは「スキップ | |
6553 | 位置」として使われる位置になります。 | |
6554 | その名前の C<(*MARK)> と東宮していなければ、C<(*SKIP)> 操作は効果を | |
6555 | 持ちません。 | |
6556 | 名前がなければ「スキップ位置」は C<(*SKIP)> パターンの実行されたときに | |
6557 | マッチングポイントが使われます。 | |
6558 | ||
6559 | =begin original | |
6560 | ||
6561 | Compare the following to the examples in C<(*PRUNE)>; note the string | |
6562 | is twice as long: | |
6563 | ||
6564 | =end original | |
6565 | ||
6566 | 以下の例を C<(*PRUNE)> と比べてみてください; | |
6567 | 文字列が2倍になってることに注意してください: | |
6568 | ||
6569 | 'aaabaaab' =~ /a+b?(*SKIP)(?{print "$&\n"; $count++})(*FAIL)/; | |
6570 | print "Count=$count\n"; | |
6571 | ||
6572 | =begin original | |
6573 | ||
6574 | outputs | |
6575 | ||
6576 | =end original | |
6577 | ||
6578 | これの出力は | |
6579 | ||
6580 | aaab | |
6581 | aaab | |
6582 | Count=2 | |
6583 | ||
6584 | =begin original | |
6585 | ||
6586 | Once the 'aaab' at the start of the string has matched, and the C<(*SKIP)> | |
6587 | executed, the next starting point will be where the cursor was when the | |
6588 | C<(*SKIP)> was executed. | |
6589 | ||
6590 | =end original | |
6591 | ||
6592 | いったん文字列の最初の 'aaab' がマッチングして、C<(*SKIP)> が実行されると、 | |
6593 | 次の開始位置は C<(*SKIP)> が実行されたときのカーソルがいた位置になります。 | |
6594 | ||
6595 | =item C<(*MARK:I<NAME>)> C<(*:I<NAME>)> | |
6596 | X<(*MARK)> X<(*MARK:NAME)> X<(*:NAME)> | |
6597 | ||
6598 | =begin original | |
6599 | ||
6600 | This zero-width pattern can be used to mark the point reached in a string | |
6601 | when a certain part of the pattern has been successfully matched. This | |
6602 | mark may be given a name. A later C<(*SKIP)> pattern will then skip | |
6603 | forward to that point if backtracked into on failure. Any number of | |
6604 | C<(*MARK)> patterns are allowed, and the I<NAME> portion may be duplicated. | |
6605 | ||
6606 | =end original | |
6607 | ||
6608 | このゼロ幅のマッチングはパターン内の特定の箇所がマッチングに成功したときに、 | |
6609 | 文字列の中で達した位置を記録するために使われます。 | |
6610 | このマークには名前をつけることもできます。 | |
6611 | 後者の C<(*SKIP)> パターンは失敗時でバックトラックしたときにその箇所まで | |
6612 | スキップします。 | |
6613 | C<(*MARK)> パターンはいくつでも使うことができて、I<NAME> 部分は | |
6614 | 重複することもあります。 | |
6615 | ||
6616 | =begin original | |
6617 | ||
6618 | In addition to interacting with the C<(*SKIP)> pattern, C<(*MARK:I<NAME>)> | |
6619 | can be used to "label" a pattern branch, so that after matching, the | |
6620 | program can determine which branches of the pattern were involved in the | |
6621 | match. | |
6622 | ||
6623 | =end original | |
6624 | ||
6625 | C<(*SKIP)> パターンとの相互動作に加えて、C<(*MARK:I<NAME>)> はパターン分岐の | |
6626 | 「ラベル」としても使うことができます; このためマッチングの後で、プログラムは | |
6627 | そのマッチングにおいてパターンのどの分岐が使われたのかを知ることができます。 | |
6628 | ||
6629 | =begin original | |
6630 | ||
6631 | When a match is successful, the C<$REGMARK> variable will be set to the | |
6632 | name of the most recently executed C<(*MARK:I<NAME>)> that was involved | |
6633 | in the match. | |
6634 | ||
6635 | =end original | |
6636 | ||
6637 | マッチングの成功時に、C<$REGMARK> 変数はマッチングの中で一番最近に | |
6638 | 実行された C<(*MARK:I<NAME>)> の名前を設定します。 | |
6639 | ||
6640 | =begin original | |
6641 | ||
6642 | This can be used to determine which branch of a pattern was matched | |
6643 | without using a separate capture group for each branch, which in turn | |
6644 | can result in a performance improvement, as perl cannot optimize | |
6645 | C</(?:(x)|(y)|(z))/> as efficiently as something like | |
6646 | C</(?:x(*MARK:x)|y(*MARK:y)|z(*MARK:z))/>. | |
6647 | ||
6648 | =end original | |
6649 | ||
6650 | これは書く分岐で別々の捕捉グループを使うことなしにパターンのどの分岐が | |
6651 | マッチングしたのかを知るために使うことができます; これは perl は | |
6652 | C</(?:(x)|(y)|(z))/> を C</(?:x(*MARK:x)|y(*MARK:y)|z(*MARK:z))/> 程度に | |
6653 | 効率的には最適化できないためパフォーマンスの向上をもたらします。 | |
6654 | ||
6655 | =begin original | |
6656 | ||
6657 | When a match has failed, and unless another verb has been involved in | |
6658 | failing the match and has provided its own name to use, the C<$REGERROR> | |
6659 | variable will be set to the name of the most recently executed | |
6660 | C<(*MARK:I<NAME>)>. | |
6661 | ||
6662 | =end original | |
6663 | ||
6664 | マッチングが失敗して、そして他の記号がマッチングの失敗で行われずかつ名前を | |
6665 | 持っているというのでなければ、C<$REGERROR> 変数には一番最近に実行された | |
6666 | C<(*MARK:I<NAME>)> の名前が設定されます。 | |
6667 | ||
6668 | =begin original | |
6669 | ||
6670 | See L</(*SKIP)> for more details. | |
6671 | ||
6672 | =end original | |
6673 | ||
6674 | 詳細は L</(*SKIP)> を参照してください。 | |
6675 | ||
6676 | =begin original | |
6677 | ||
6678 | As a shortcut C<(*MARK:I<NAME>)> can be written C<(*:I<NAME>)>. | |
6679 | ||
6680 | =end original | |
6681 | ||
6682 | C<(*MARK:I<NAME>)> の短縮形として C<(*:I<NAME>)> とも記述できます。 | |
6683 | ||
6684 | =item C<(*THEN)> C<(*THEN:I<NAME>)> | |
6685 | ||
6686 | =begin original | |
6687 | ||
6688 | This is similar to the "cut group" operator C<::> from Perl 6. Like | |
6689 | C<(*PRUNE)>, this verb always matches, and when backtracked into on | |
6690 | failure, it causes the regex engine to try the next alternation in the | |
6691 | innermost enclosing group (capturing or otherwise) that has alternations. | |
6692 | The two branches of a C<(?(I<condition>)I<yes-pattern>|I<no-pattern>)> do not | |
6693 | count as an alternation, as far as C<(*THEN)> is concerned. | |
6694 | ||
6695 | =end original | |
6696 | ||
6697 | これは Perl 6 の "cut group" 演算子 C<::> と似ています。 | |
6698 | C<(*PRUNE)> のように、この記号は常にマッチングし、そして失敗で | |
6699 | バックトラックした時に正規表現エンジンに、代替のある一番内側で閉じている | |
6700 | グループ(キャプチャでもそうでなくとも)で次の代替を試みるようにさせます。 | |
6701 | C<(*THEN)> が有効である限り、 | |
6702 | C<(?(I<condition>)I<yes-pattern>|I<no-pattern>)> の二つの枝は代替とは | |
6703 | 扱われません。 | |
6704 | ||
6705 | =begin original | |
6706 | ||
6707 | Its name comes from the observation that this operation combined with the | |
6708 | alternation operator (C<"|">) can be used to create what is essentially a | |
6709 | pattern-based if/then/else block: | |
6710 | ||
6711 | =end original | |
6712 | ||
6713 | この名前は代替演算子 (C<"|">) と連結されたこの演算子で本質的にパターンベースの | |
6714 | if/then/else ブロックとなるものを作るために使うことが | |
6715 | できることからきています: | |
6716 | ||
6717 | ( COND (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ ) | |
6718 | ||
6719 | =begin original | |
6720 | ||
6721 | Note that if this operator is used and NOT inside of an alternation then | |
6722 | it acts exactly like the C<(*PRUNE)> operator. | |
6723 | ||
6724 | =end original | |
6725 | ||
6726 | この演算子が使われていてそしてそれが代替の内側ではなければ | |
6727 | これはちょうど C<(*PRUNE)> 演算子のように動作します。 | |
6728 | ||
6729 | / A (*PRUNE) B / | |
6730 | ||
6731 | =begin original | |
6732 | ||
6733 | is the same as | |
6734 | ||
6735 | =end original | |
6736 | ||
6737 | は次と同じです | |
6738 | ||
6739 | / A (*THEN) B / | |
6740 | ||
6741 | =begin original | |
6742 | ||
6743 | but | |
6744 | ||
6745 | =end original | |
6746 | ||
6747 | しかし | |
6748 | ||
6749 | / ( A (*THEN) B | C ) / | |
6750 | ||
6751 | =begin original | |
6752 | ||
6753 | is not the same as | |
6754 | ||
6755 | =end original | |
6756 | ||
6757 | は次と同じではありません | |
6758 | ||
6759 | / ( A (*PRUNE) B | C ) / | |
6760 | ||
6761 | =begin original | |
6762 | ||
6763 | as after matching the I<A> but failing on the I<B> the C<(*THEN)> verb will | |
6764 | backtrack and try I<C>; but the C<(*PRUNE)> verb will simply fail. | |
6765 | ||
6766 | =end original | |
6767 | ||
6768 | I<A> にマッチングしたけれど I<B> に失敗した後 C<(*THEN)> 記号は | |
6769 | バックトラックして I<C> を試みます; しかし C<(*PRUNE)> 記号であれば | |
6770 | 単純に失敗します。 | |
6771 | ||
6772 | =item C<(*COMMIT)> C<(*COMMIT:I<arg>)> | |
6773 | X<(*COMMIT)> | |
6774 | ||
6775 | =begin original | |
6776 | ||
6777 | This is the Perl 6 "commit pattern" C<< <commit> >> or C<:::>. It's a | |
6778 | zero-width pattern similar to C<(*SKIP)>, except that when backtracked | |
6779 | into on failure it causes the match to fail outright. No further attempts | |
6780 | to find a valid match by advancing the start pointer will occur again. | |
6781 | For example, | |
6782 | ||
6783 | =end original | |
6784 | ||
6785 | これは Perl 6 の"コミットパターン" C<< <commit> >> または C<:::> です。 | |
6786 | これは C<(*SKIP)> と似たゼロ幅のパターンですが、失敗でバックトラックした | |
6787 | 際にマッチングがすぐに失敗する点で異なります。 | |
6788 | それ以降で開始位置を進めて有効なマッチングを探す試行は行われません。 | |
6789 | 例えば、 | |
6790 | ||
6791 | 'aaabaaab' =~ /a+b?(*COMMIT)(?{print "$&\n"; $count++})(*FAIL)/; | |
6792 | print "Count=$count\n"; | |
6793 | ||
6794 | =begin original | |
6795 | ||
6796 | outputs | |
6797 | ||
6798 | =end original | |
6799 | ||
6800 | これの出力は | |
6801 | ||
6802 | aaab | |
6803 | Count=1 | |
6804 | ||
6805 | =begin original | |
6806 | ||
6807 | In other words, once the C<(*COMMIT)> has been entered, and if the pattern | |
6808 | does not match, the regex engine will not try any further matching on the | |
6809 | rest of the string. | |
6810 | ||
6811 | =end original | |
6812 | ||
6813 | 言い換えると、いったん C<(*COMMIT)> に入った後に、そのパターンが | |
6814 | マッチングしなかったのなら、正規表現エンジンは文字列の残りに対して | |
6815 | それ以上のマッチングを試みません。 | |
6816 | ||
6817 | =item C<(*FAIL)> C<(*F)> C<(*FAIL:I<arg>)> | |
6818 | X<(*FAIL)> X<(*F)> | |
6819 | ||
6820 | =begin original | |
6821 | ||
6822 | This pattern matches nothing and always fails. It can be used to force the | |
6823 | engine to backtrack. It is equivalent to C<(?!)>, but easier to read. In | |
6824 | fact, C<(?!)> gets optimised into C<(*FAIL)> internally. You can provide | |
6825 | an argument so that if the match fails because of this C<FAIL> directive | |
6826 | the argument can be obtained from C<$REGERROR>. | |
6827 | ||
6828 | =end original | |
6829 | ||
6830 | このパターンは何にもマッチングせず常に失敗します。 | |
6831 | これはエンジンを強制的にバックトラックさせるために使えます。 | |
6832 | これは C<(?!)> と等価ですが、より読みやすくなっています。 | |
6833 | 実際、C<(?!)> は内部的には C<(*FAIL)> に最適化されます。 | |
6834 | この C<FAIL> 指示子によってマッチングが失敗したときに | |
6835 | C<$REGERROR> から得られる、引数を指定できます。 | |
6836 | ||
6837 | =begin original | |
6838 | ||
6839 | It is probably useful only when combined with C<(?{})> or C<(??{})>. | |
6840 | ||
6841 | =end original | |
6842 | ||
6843 | これはおそらく C<(?{})> または C<(??{})> と組み合わせた時にだけ | |
6844 | 役に立つでしょう。 | |
6845 | ||
6846 | =item C<(*ACCEPT)> C<(*ACCEPT:I<arg>)> | |
6847 | X<(*ACCEPT)> | |
6848 | ||
6849 | =begin original | |
6850 | ||
6851 | This pattern matches nothing and causes the end of successful matching at | |
6852 | the point at which the C<(*ACCEPT)> pattern was encountered, regardless of | |
6853 | whether there is actually more to match in the string. When inside of a | |
6854 | nested pattern, such as recursion, or in a subpattern dynamically generated | |
6855 | via C<(??{})>, only the innermost pattern is ended immediately. | |
6856 | ||
6857 | =end original | |
6858 | ||
6859 | このパターンマッチングは何もせず C<(*ACCEPT)> パターンと遭遇した場所で | |
6860 | 文字列の中で実際にもっとマッチングするものがあるかどうかにかかわらず | |
6861 | 成功のマッチングを終了させます。 | |
6862 | 再帰、または C<(??{})> といったネストしたパターンの内側では、一番内側の | |
6863 | パターンのみがすぐに終了します。 | |
6864 | ||
6865 | =begin original | |
6866 | ||
6867 | If the C<(*ACCEPT)> is inside of capturing groups then the groups are | |
6868 | marked as ended at the point at which the C<(*ACCEPT)> was encountered. | |
6869 | For instance: | |
6870 | ||
6871 | =end original | |
6872 | ||
6873 | C<(*ACCEPT)> が捕捉グループの内側で使われた場合捕捉グループは | |
6874 | C<(*ACCEPT)> と遭遇した位置で終了とマークされます。 | |
6875 | 例えば: | |
6876 | ||
6877 | 'AB' =~ /(A (A|B(*ACCEPT)|C) D)(E)/x; | |
6878 | ||
6879 | =begin original | |
6880 | ||
6881 | will match, and C<$1> will be C<AB> and C<$2> will be C<"B">, C<$3> will not | |
6882 | be set. If another branch in the inner parentheses was matched, such as in the | |
6883 | string 'ACDE', then the C<"D"> and C<"E"> would have to be matched as well. | |
6884 | ||
6885 | =end original | |
6886 | ||
6887 | はマッチングし、C<$1> は C<AB> になり、C<$2> は C<"B"> に、そして | |
6888 | C<$3> は設定されません。 | |
6889 | 'ACDE' のようにかっこの内側で他の分岐がマッチングしたのなら、 | |
6890 | C<"D"> と C<"E"> もマッチングします。 | |
6891 | ||
6892 | =begin original | |
6893 | ||
6894 | You can provide an argument, which will be available in the var | |
6895 | C<$REGMARK> after the match completes. | |
6896 | ||
6897 | =end original | |
6898 | ||
6899 | マッチングが成功した後に C<$REGMARK> 変数で利用可能な引数を指定できます。 | |
6900 | ||
6901 | =back | |
6902 | ||
6903 | =back | |
6904 | ||
6905 | =head2 Warning on C<\1> Instead of C<$1> | |
6906 | ||
6907 | (C<$1> ではなく C<\1> だったときの警告) | |
6908 | ||
6909 | =begin original | |
6910 | ||
5279 | 6911 | Some people get too used to writing things like: |
5280 | 6912 | |
5281 | 6913 | =end original |
5282 | 6914 | |
5283 | 6915 | 次のように書くことになれている人も中にはいるでしょう: |
5284 | 6916 | |
5285 | 6917 | $pattern =~ s/(\W)/\\\1/g; |
5286 | 6918 | |
5287 | 6919 | =begin original |
5288 | 6920 | |
5289 | 6921 | This is grandfathered (for \1 to \9) for the RHS of a substitute to avoid |
5290 | 6922 | shocking the |
5291 | 6923 | B<sed> addicts, but it's a dirty habit to get into. That's because in |
5292 | 6924 | PerlThink, the righthand side of an C<s///> is a double-quoted string. C<\1> in |
5293 | 6925 | the usual double-quoted string means a control-A. The customary Unix |
5294 | 6926 | meaning of C<\1> is kludged in for C<s///>. However, if you get into the habit |
5295 | 6927 | of doing that, you get yourself into trouble if you then add an C</e> |
5296 | 6928 | modifier. |
5297 | 6929 | |
5298 | 6930 | =end original |
5299 | 6931 | |
5300 | 6932 | (\1 から \9 については) B<sed> 中毒な人をびっくりさせないための RHS 置換の |
5301 | 6933 | 祖先ですが、しかしこれは汚らしい癖です。 |
5302 | 6934 | Perl においては、C<s///> の右側はダブルクォートされた文字列と |
5303 | 6935 | 考えられるためです。 |
5304 | 6936 | 通常のダブルクォートされた文字列の中では C<\1> は control-A を意味します。 |
5305 | 6937 | C<\1> の Unix での習慣的な意味は C<s///> だけのその場しのぎです。 |
5306 | 6938 | しかしながら、この癖に漬かっていると C</e> 修飾子を使ったときに |
5307 | 6939 | トラブルとなるでしょう。 |
5308 | 6940 | |
5309 | 6941 | s/(\d+)/ \1 + 1 /eg; # causes warning under -w |
5310 | 6942 | |
5311 | 6943 | =begin original |
5312 | 6944 | |
5313 | 6945 | Or if you try to do |
5314 | 6946 | |
5315 | 6947 | =end original |
5316 | 6948 | |
5317 | 6949 | または次のようにするかもしれません |
5318 | 6950 | |
5319 | 6951 | s/(\d+)/\1000/; |
5320 | 6952 | |
5321 | 6953 | =begin original |
5322 | 6954 | |
5323 | 6955 | You can't disambiguate that by saying C<\{1}000>, whereas you can fix it with |
5324 | 6956 | C<${1}000>. The operation of interpolation should not be confused |
5325 | 6957 | with the operation of matching a backreference. Certainly they mean two |
5326 | 6958 | different things on the I<left> side of the C<s///>. |
5327 | 6959 | |
5328 | 6960 | =end original |
5329 | 6961 | |
5330 | 6962 | これを解消するために C<\{1}000> としないでください; |
5331 | 6963 | ここでは C<${1}000> とするべきです。 |
5332 | 6964 | 埋め込みの処理は後方参照にマッチングさせる操作より混乱は少ないでしょう。 |
5333 | 6965 | 特に C<s///> の I<左> 側では2つの異なった意味になります。 |
5334 | 6966 | |
5335 | 6967 | =head2 Repeated Patterns Matching a Zero-length Substring |
5336 | 6968 | |
5337 | 6969 | (ゼロ幅の部分文字列にマッチングするパターンの繰り返し) |
5338 | 6970 | |
5339 | 6971 | =begin original |
5340 | 6972 | |
5341 | 6973 | B<WARNING>: Difficult material (and prose) ahead. This section needs a rewrite. |
5342 | 6974 | |
5343 | 6975 | =end original |
5344 | 6976 | |
5345 | 6977 | B<警告>: この先には難しい(そして無味乾燥な)内容があります。 |
5346 | 6978 | このセクションは書き直す必要があるでしょう。 |
5347 | 6979 | |
5348 | 6980 | =begin original |
5349 | 6981 | |
5350 | 6982 | Regular expressions provide a terse and powerful programming language. As |
5351 | 6983 | with most other power tools, power comes together with the ability |
5352 | 6984 | to wreak havoc. |
5353 | 6985 | |
5354 | 6986 | =end original |
5355 | 6987 | |
5356 | 6988 | 正規表現は簡潔でパワフルなプログラミング言語を提供します。 |
5357 | 6989 | 他の多くの強力なツールとともに、力は破壊の源にもなります。 |
5358 | 6990 | |
5359 | 6991 | =begin original |
5360 | 6992 | |
5361 | 6993 | A common abuse of this power stems from the ability to make infinite |
5362 | 6994 | loops using regular expressions, with something as innocuous as: |
5363 | 6995 | |
5364 | 6996 | =end original |
5365 | 6997 | |
5366 | 6998 | この力のよくある乱用は無害な何かとともに、正規表現使った |
5367 | 6999 | 無限ループとなります。 |
5368 | 7000 | |
5369 | 7001 | 'foo' =~ m{ ( o? )* }x; |
5370 | 7002 | |
5371 | 7003 | =begin original |
5372 | 7004 | |
5373 | The C<o?> matches at the beginning of C< | |
7005 | The C<o?> matches at the beginning of "C<foo>", and since the position | |
5374 | 7006 | 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 | |
7007 | because of the C<"*"> quantifier. Another common way to create a similar cycle | |
5376 | is with the looping modifier C</ | |
7008 | is with the looping modifier C</g>: | |
5377 | 7009 | |
5378 | 7010 | =end original |
5379 | 7011 | |
5380 | C<o?> は C< | |
7012 | C<o?> は "C<foo>" の始まりにマッチングし、文字列中での位置はこの | |
5381 | マッチングでは動かないので、C<o?> は C<*> 量指定子によって何回も | |
7013 | マッチングでは動かないので、C<o?> は C<"*"> 量指定子によって何回も | |
5382 | 7014 | マッチングします。 |
5383 | 同じような繰り返しを作るもう一つのよくある形として C</ | |
7015 | 同じような繰り返しを作るもう一つのよくある形として C</g> 修飾子を使った | |
5384 | 7016 | ループがあります: |
5385 | 7017 | |
5386 | 7018 | @matches = ( 'foo' =~ m{ o? }xg ); |
5387 | 7019 | |
5388 | 7020 | =begin original |
5389 | 7021 | |
5390 | 7022 | or |
5391 | 7023 | |
5392 | 7024 | =end original |
5393 | 7025 | |
5394 | 7026 | または |
5395 | 7027 | |
5396 | 7028 | print "match: <$&>\n" while 'foo' =~ m{ o? }xg; |
5397 | 7029 | |
5398 | 7030 | =begin original |
5399 | 7031 | |
5400 | or the loop implied by split(). | |
7032 | or the loop implied by C<split()>. | |
5401 | 7033 | |
5402 | 7034 | =end original |
5403 | 7035 | |
5404 | または split() による暗黙のループ。 | |
7036 | または C<split()> による暗黙のループ。 | |
5405 | 7037 | |
5406 | 7038 | =begin original |
5407 | 7039 | |
5408 | 7040 | However, long experience has shown that many programming tasks may |
5409 | 7041 | be significantly simplified by using repeated subexpressions that |
5410 | 7042 | may match zero-length substrings. Here's a simple example being: |
5411 | 7043 | |
5412 | 7044 | =end original |
5413 | 7045 | |
5414 | 7046 | しかしながら、長きにわたる経験からいくつかのプログラミングタスクは |
5415 | 7047 | ゼロ幅の部分文字列に対するマッチングを行う部分式の繰り返しで大幅に |
5416 | 7048 | 単純にできることがわかりました。 |
5417 | 7049 | 簡単な例を挙げてみます: |
5418 | 7050 | |
5419 | 7051 | @chars = split //, $string; # // is not magic in split |
5420 | 7052 | ($whitewashed = $string) =~ s/()/ /g; # parens avoid magic s// / |
5421 | 7053 | |
5422 | 7054 | =begin original |
5423 | 7055 | |
5424 | 7056 | Thus Perl allows such constructs, by I<forcefully breaking |
5425 | 7057 | the infinite loop>. The rules for this are different for lower-level |
5426 | 7058 | loops given by the greedy quantifiers C<*+{}>, and for higher-level |
5427 | ones like the C</g> modifier or split() operator. | |
7059 | ones like the C</g> modifier or C<split()> operator. | |
5428 | 7060 | |
5429 | 7061 | =end original |
5430 | 7062 | |
5431 | 7063 | このように Perl は I<強制的に無限ループを砕く> ことによってこういった構築を |
5432 | 7064 | 可能にしています。 |
5433 | 7065 | このためのルールは貪欲な量指定子 C<*+{}> によって与えられる |
5434 | 低レベルなループとも、C</g> 修飾子や split() 演算子による | |
7066 | 低レベルなループとも、C</g> 修飾子や C<split()> 演算子による | |
5435 | 7067 | 高レベルなループとも異なります。 |
5436 | 7068 | |
5437 | 7069 | =begin original |
5438 | 7070 | |
5439 | 7071 | The lower-level loops are I<interrupted> (that is, the loop is |
5440 | 7072 | broken) when Perl detects that a repeated expression matched a |
5441 | 7073 | zero-length substring. Thus |
5442 | 7074 | |
5443 | 7075 | =end original |
5444 | 7076 | |
5445 | 7077 | 低レベルなループは Perl がゼロ幅の部分文字列に対してマッチングする式が |
5446 | 7078 | 繰り返されたことを検出すると I<中断> されます (つまり、ループは壊されます)。 |
5447 | 7079 | 従って |
5448 | 7080 | |
5449 | 7081 | m{ (?: NON_ZERO_LENGTH | ZERO_LENGTH )* }x; |
5450 | 7082 | |
5451 | 7083 | =begin original |
5452 | 7084 | |
5453 | 7085 | is made equivalent to |
5454 | 7086 | |
5455 | 7087 | =end original |
5456 | 7088 | |
5457 | 7089 | は次と等価にされます |
5458 | 7090 | |
5459 | 7091 | m{ (?: NON_ZERO_LENGTH )* (?: ZERO_LENGTH )? }x; |
5460 | 7092 | |
5461 | 7093 | =begin original |
5462 | 7094 | |
5463 | 7095 | For example, this program |
5464 | 7096 | |
5465 | 7097 | =end original |
5466 | 7098 | |
5467 | 7099 | 例えば、以下のプログラムは |
5468 | 7100 | |
5469 | 7101 | #!perl -l |
5470 | 7102 | "aaaaab" =~ / |
5471 | 7103 | (?: |
5472 | 7104 | a # non-zero |
5473 | 7105 | | # or |
5474 | 7106 | (?{print "hello"}) # print hello whenever this |
5475 | 7107 | # branch is tried |
5476 | 7108 | (?=(b)) # zero-width assertion |
5477 | 7109 | )* # any number of times |
5478 | 7110 | /x; |
5479 | 7111 | print $&; |
5480 | 7112 | print $1; |
5481 | 7113 | |
5482 | 7114 | =begin original |
5483 | 7115 | |
5484 | 7116 | prints |
5485 | 7117 | |
5486 | 7118 | =end original |
5487 | 7119 | |
5488 | 7120 | 以下を表示します |
5489 | 7121 | |
5490 | 7122 | hello |
5491 | 7123 | aaaaa |
5492 | 7124 | b |
5493 | 7125 | |
5494 | 7126 | =begin original |
5495 | 7127 | |
5496 | 7128 | Notice that "hello" is only printed once, as when Perl sees that the sixth |
5497 | 7129 | iteration of the outermost C<(?:)*> matches a zero-length string, it stops |
5498 | the C<*>. | |
7130 | the C<"*">. | |
5499 | 7131 | |
5500 | 7132 | =end original |
5501 | 7133 | |
5502 | 7134 | "hello" は一度だけ表示されることに注目して下さい; Perl は |
5503 | 7135 | 一番外側の C<(?:)*> の 6 回目の繰り返しがゼロ長文字列にマッチングするのを |
5504 | 見るので、C<*> を止めます。 | |
7136 | 見るので、C<"*"> を止めます。 | |
5505 | 7137 | |
5506 | 7138 | =begin original |
5507 | 7139 | |
5508 | 7140 | The higher-level loops preserve an additional state between iterations: |
5509 | 7141 | whether the last match was zero-length. To break the loop, the following |
5510 | 7142 | match after a zero-length match is prohibited to have a length of zero. |
5511 | This prohibition interacts with backtracking (see L<"Backtracking">), | |
7143 | This prohibition interacts with backtracking (see L</"Backtracking">), | |
5512 | 7144 | and so the I<second best> match is chosen if the I<best> match is of |
5513 | 7145 | zero length. |
5514 | 7146 | |
5515 | 7147 | =end original |
5516 | 7148 | |
5517 | 7149 | 高レベルのループは各繰り返しの間に最後のマッチングがゼロ幅だったかどうかを |
5518 | 7150 | 追加で保持しています。 |
5519 | 7151 | ループを終えるために、ゼロ幅のマッチングの後のマッチングはゼロ幅と |
5520 | 7152 | なることを拒否します。 |
5521 | この禁則処理はバックトラックと相互に動作し(L<"Backtracking"> 参照)、そして | |
7153 | この禁則処理はバックトラックと相互に動作し(L</"Backtracking"> 参照)、そして | |
5522 | 7154 | I<ベストな> マッチングがゼロ幅だったのなら I<2 番目にベストな> |
5523 | 7155 | マッチングが選択されます。 |
5524 | 7156 | |
5525 | 7157 | =begin original |
5526 | 7158 | |
5527 | 7159 | For example: |
5528 | 7160 | |
5529 | 7161 | =end original |
5530 | 7162 | |
5531 | 7163 | 例: |
5532 | 7164 | |
5533 | 7165 | $_ = 'bar'; |
5534 | 7166 | s/\w??/<$&>/g; |
5535 | 7167 | |
5536 | 7168 | =begin original |
5537 | 7169 | |
5538 | 7170 | results in C<< <><b><><a><><r><> >>. At each position of the string the best |
5539 | 7171 | match given by non-greedy C<??> is the zero-length match, and the I<second |
5540 | 7172 | best> match is what is matched by C<\w>. Thus zero-length matches |
5541 | 7173 | alternate with one-character-long matches. |
5542 | 7174 | |
5543 | 7175 | =end original |
5544 | 7176 | |
5545 | 7177 | これは C<< <><b><><a><><r><> >> となります。 |
5546 | 7178 | 文字列の各位置に於いて、貪欲でない C<??> によって得られるベストな |
5547 | 7179 | マッチングはゼロ幅のマッチングです、 |
5548 | 7180 | そして I<2 番目にベストな>マッチングは C<\w> によってマッチングするものです。 |
5549 | 7181 | 従ってゼロ幅のマッチングは 1 文字幅のマッチングの代替となります。 |
5550 | 7182 | |
5551 | 7183 | =begin original |
5552 | 7184 | |
5553 | 7185 | Similarly, for repeated C<m/()/g> the second-best match is the match at the |
5554 | 7186 | position one notch further in the string. |
5555 | 7187 | |
5556 | 7188 | =end original |
5557 | 7189 | |
5558 | 7190 | 同じように、C<m/()/g> の繰り返しでは文字列中の境界一つ遠い位置に 2 番目に |
5559 | 7191 | ベストなマッチングがマッチングします。 |
5560 | 7192 | |
5561 | 7193 | =begin original |
5562 | 7194 | |
5563 | 7195 | 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(). | |
7196 | the matched string, and is reset by each assignment to C<pos()>. | |
5565 | 7197 | Zero-length matches at the end of the previous match are ignored |
5566 | 7198 | during C<split>. |
5567 | 7199 | |
5568 | 7200 | =end original |
5569 | 7201 | |
5570 | 7202 | I<ゼロ幅にマッチングしている> という追加の状態はマッチングした文字列に |
5571 | 関連づけられていて、pos() に対する割り当てによってリセットされます。 | |
7203 | 関連づけられていて、C<pos()> に対する割り当てによってリセットされます。 | |
5572 | 7204 | 前のマッチングの終端でのゼロ幅のマッチングは C<split> の間は無視されます。 |
5573 | 7205 | |
5574 | 7206 | =head2 Combining RE Pieces |
5575 | 7207 | |
5576 | 7208 | (RE の欠片の結合) |
5577 | 7209 | |
5578 | 7210 | =begin original |
5579 | 7211 | |
5580 | 7212 | Each of the elementary pieces of regular expressions which were described |
5581 | 7213 | before (such as C<ab> or C<\Z>) could match at most one substring |
5582 | 7214 | at the given position of the input string. However, in a typical regular |
5583 | 7215 | expression these elementary pieces are combined into more complicated |
5584 | patterns using combining operators C<ST>, C<S|T>, C<S*> etc. | |
7216 | 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). | |
7217 | (in these examples C<"S"> and C<"T"> are regular subexpressions). | |
5586 | 7218 | |
5587 | 7219 | =end original |
5588 | 7220 | |
5589 | 7221 | これまでに説明された (C<ab> や C<\Z> といった) 正規表現の基本的な欠片 |
5590 | 7222 | それぞれは、入力文字列上の与えられた位置で多くとも1つの部分文字列に |
5591 | 7223 | マッチングします。 |
5592 | 7224 | しかしながら、典型的な正規表現ではこれらの基本的な欠片は結合演算 |
5593 | C<ST>、C<S|T>、C<S*> 等(ここで C<S> や C<T> は正規表現の部分式)を使って | |
7225 | C<ST>、C<S|T>、C<S*> 等(ここで C<"S"> や C<"T"> は正規表現の部分式)を使って | |
5594 | 7226 | より複雑なパターンへと合成することができます。 |
5595 | 7227 | |
5596 | 7228 | =begin original |
5597 | 7229 | |
5598 | 7230 | Such combinations can include alternatives, leading to a problem of choice: |
5599 | 7231 | if we match a regular expression C<a|ab> against C<"abc">, will it match |
5600 | 7232 | substring C<"a"> or C<"ab">? One way to describe which substring is |
5601 | actually matched is the concept of backtracking (see L<"Backtracking">). | |
7233 | actually matched is the concept of backtracking (see L</"Backtracking">). | |
5602 | 7234 | However, this description is too low-level and makes you think |
5603 | 7235 | in terms of a particular implementation. |
5604 | 7236 | |
5605 | 7237 | =end original |
5606 | 7238 | |
5607 | 7239 | このような合成には選択の問題を導くために代替を含めることができます: |
5608 | 7240 | 正規表現 C<a|ab> を C<"abc"> に対してマッチングさせようとしたとき、これは |
5609 | 7241 | C<"a"> と C<"ab"> のどちらにマッチングするのか? |
5610 | 7242 | 実際にどちらがマッチングするのかを説明する1つの方法として、 |
5611 | バックトラッキングのコンセプトがあります(L<"Backtracking"> 参照)。 | |
7243 | バックトラッキングのコンセプトがあります(L</"Backtracking"> 参照)。 | |
5612 | 7244 | しかしながら、この説明は低レベルすぎて特定の実装を考えなければなりません。 |
5613 | 7245 | |
5614 | 7246 | =begin original |
5615 | 7247 | |
5616 | 7248 | Another description starts with notions of "better"/"worse". All the |
5617 | 7249 | substrings which may be matched by the given regular expression can be |
5618 | 7250 | sorted from the "best" match to the "worst" match, and it is the "best" |
5619 | 7251 | match which is chosen. This substitutes the question of "what is chosen?" |
5620 | 7252 | by the question of "which matches are better, and which are worse?". |
5621 | 7253 | |
5622 | 7254 | =end original |
5623 | 7255 | |
5624 | 7256 | もう一つの説明は"より良い"/"より悪い"の考え方で始めます。 |
5625 | 7257 | 与えられた正規表現にマッチングするすべての部分文字列は「最良の」 |
5626 | 7258 | マッチングから「最悪の」マッチングへとソートすることができます; そして |
5627 | 7259 | 「最良の」マッチングが選択されます。 |
5628 | 7260 | これは「どれが選ばれるのか?」という問いかけを「どのマッチングがより良くて、 |
5629 | 7261 | それがより悪いのか?」という問いかけに置き換えることができます。 |
5630 | 7262 | |
5631 | 7263 | =begin original |
5632 | 7264 | |
5633 | 7265 | Again, for elementary pieces there is no such question, since at most |
5634 | 7266 | one match at a given position is possible. This section describes the |
5635 | 7267 | notion of better/worse for combining operators. In the description |
5636 | below C<S> and C<T> are regular subexpressions. | |
7268 | below C<"S"> and C<"T"> are regular subexpressions. | |
5637 | 7269 | |
5638 | 7270 | =end original |
5639 | 7271 | |
5640 | 7272 | そして、基本的な要素ではそういった問いかけはありません; |
5641 | 7273 | なぜならこれらは与えられた位置で可能なマッチングは多くとも1つだからです。 |
5642 | 7274 | このセクションでは結合演算のより良い/より悪いの考え方で説明していきます。 |
5643 | 以下の説明では C<S> 及び C<T> は正規表現の部分式です。 | |
7275 | 以下の説明では C<"S"> 及び C<"T"> は正規表現の部分式です。 | |
5644 | 7276 | |
5645 | 7277 | =over 4 |
5646 | 7278 | |
5647 | 7279 | =item C<ST> |
5648 | 7280 | |
5649 | 7281 | =begin original |
5650 | 7282 | |
5651 | Consider two possible matches, C<AB> and C<A'B'>, C<A> and C<A'> are | |
7283 | 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 | |
7284 | substrings which can be matched by C<"S">, C<"B"> and C<B'> are substrings | |
5653 | which can be matched by C<T>. | |
7285 | which can be matched by C<"T">. | |
5654 | 7286 | |
5655 | 7287 | =end original |
5656 | 7288 | |
5657 | 7289 | 2つの可能なマッチング、C<AB> 及び C<A'B'> を考えます; |
5658 | ここで C<A> 及び C<A'> は C<S> にマッチングする部分文字列、 | |
7290 | ここで C<"A"> 及び C<A'> は C<"S"> にマッチングする部分文字列、 | |
5659 | そして C<B> 及び C<B'> は C<T> にマッチングする部分文字列とします。 | |
7291 | そして C<"B"> 及び C<B'> は C<"T"> にマッチングする部分文字列とします。 | |
5660 | 7292 | |
5661 | 7293 | =begin original |
5662 | 7294 | |
5663 | If C<A> is a better match for C<S> than C<A'>, C<AB> is a better | |
7295 | If C<"A"> is a better match for C<"S"> than C<A'>, C<AB> is a better | |
5664 | 7296 | match than C<A'B'>. |
5665 | 7297 | |
5666 | 7298 | =end original |
5667 | 7299 | |
5668 | もし C<A> が C<S> に対して C<A'> よりも良いマッチングであれば、 | |
7300 | もし C<"A"> が C<"S"> に対して C<A'> よりも良いマッチングであれば、 | |
5669 | 7301 | C<AB> は C<A'B'> よりも良いマッチングです。 |
5670 | 7302 | |
5671 | 7303 | =begin original |
5672 | 7304 | |
5673 | If C<A> and C<A'> coincide: C<AB> is a better match than C<AB'> if | |
7305 | 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'>. | |
7306 | C<"B"> is a better match for C<"T"> than C<B'>. | |
5675 | 7307 | |
5676 | 7308 | =end original |
5677 | 7309 | |
5678 | もし C<A> と C<A'> が同じであれば: C<B> が C<T> に対して C<B'> よりも | |
7310 | もし C<"A"> と C<A'> が同じであれば: C<"B"> が C<"T"> に対して C<B'> よりも | |
5679 | 7311 | 良いマッチングであれば C<AB> は C<AB'> よりも良いマッチングです。 |
5680 | 7312 | |
5681 | 7313 | =item C<S|T> |
5682 | 7314 | |
5683 | 7315 | =begin original |
5684 | 7316 | |
5685 | When C<S> can match, it is a better match than when only C<T> can match. | |
7317 | When C<"S"> can match, it is a better match than when only C<"T"> can match. | |
5686 | 7318 | |
5687 | 7319 | =end original |
5688 | 7320 | |
5689 | C<S> がマッチングできる時は C<T> のみがマッチングするよりも良い | |
7321 | C<"S"> がマッチングできる時は C<"T"> のみがマッチングするよりも良い | |
5690 | 7322 | マッチングです。 |
5691 | 7323 | |
5692 | 7324 | =begin original |
5693 | 7325 | |
5694 | Ordering of two matches for C<S> is the same as for C<S>. Similar for | |
7326 | Ordering of two matches for C<"S"> is the same as for C<"S">. Similar for | |
5695 | two matches for C<T>. | |
7327 | two matches for C<"T">. | |
5696 | 7328 | |
5697 | 7329 | =end original |
5698 | 7330 | |
5699 | C<S> に対する2つのマッチングの順序は C<S> と同じです。 | |
7331 | C<"S"> に対する2つのマッチングの順序は C<"S"> と同じです。 | |
5700 | C<T> に対する2つのマッチングも同様です。 | |
7332 | C<"T"> に対する2つのマッチングも同様です。 | |
5701 | 7333 | |
5702 | 7334 | =item C<S{REPEAT_COUNT}> |
5703 | 7335 | |
5704 | 7336 | =begin original |
5705 | 7337 | |
5706 | 7338 | Matches as C<SSS...S> (repeated as many times as necessary). |
5707 | 7339 | |
5708 | 7340 | =end original |
5709 | 7341 | |
5710 | 7342 | C<SSS...S> (必要なだけ繰り返し)としてマッチングします。 |
5711 | 7343 | |
5712 | 7344 | =item C<S{min,max}> |
5713 | 7345 | |
5714 | 7346 | =begin original |
5715 | 7347 | |
5716 | 7348 | Matches as C<S{max}|S{max-1}|...|S{min+1}|S{min}>. |
5717 | 7349 | |
5718 | 7350 | =end original |
5719 | 7351 | |
5720 | 7352 | C<S{max}|S{max-1}|...|S{min+1}|S{min}> としてマッチングします。 |
5721 | 7353 | |
5722 | 7354 | =item C<S{min,max}?> |
5723 | 7355 | |
5724 | 7356 | =begin original |
5725 | 7357 | |
5726 | 7358 | Matches as C<S{min}|S{min+1}|...|S{max-1}|S{max}>. |
5727 | 7359 | |
5728 | 7360 | =end original |
5729 | 7361 | |
5730 | 7362 | C<S{min}|S{min+1}|...|S{max-1}|S{max}> としてマッチングします。 |
5731 | 7363 | |
5732 | 7364 | =item C<S?>, C<S*>, C<S+> |
5733 | 7365 | |
5734 | 7366 | =begin original |
5735 | 7367 | |
5736 | 7368 | Same as C<S{0,1}>, C<S{0,BIG_NUMBER}>, C<S{1,BIG_NUMBER}> respectively. |
5737 | 7369 | |
5738 | 7370 | =end original |
5739 | 7371 | |
5740 | 7372 | それぞれ C<S{0,1}>, C<S{0,BIG_NUMBER}>, C<S{1,BIG_NUMBER}> と同じです。 |
5741 | 7373 | |
5742 | 7374 | =item C<S??>, C<S*?>, C<S+?> |
5743 | 7375 | |
5744 | 7376 | =begin original |
5745 | 7377 | |
5746 | 7378 | Same as C<S{0,1}?>, C<S{0,BIG_NUMBER}?>, C<S{1,BIG_NUMBER}?> respectively. |
5747 | 7379 | |
5748 | 7380 | =end original |
5749 | 7381 | |
5750 | 7382 | それぞれ C<S{0,1}?>, C<S{0,BIG_NUMBER}?>, C<S{1,BIG_NUMBER}?> と同じです。 |
5751 | 7383 | |
5752 | 7384 | =item C<< (?>S) >> |
5753 | 7385 | |
5754 | 7386 | =begin original |
5755 | 7387 | |
5756 | Matches the best match for C<S> and only that. | |
7388 | Matches the best match for C<"S"> and only that. | |
5757 | 7389 | |
5758 | 7390 | =end original |
5759 | 7391 | |
5760 | C<S> の最良のみマッチングします。 | |
7392 | C<"S"> の最良のみマッチングします。 | |
5761 | 7393 | |
5762 | 7394 | =item C<(?=S)>, C<(?<=S)> |
5763 | 7395 | |
5764 | 7396 | =begin original |
5765 | 7397 | |
5766 | Only the best match for C<S> is considered. (This is important only if | |
7398 | 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 | |
7399 | C<"S"> has capturing parentheses, and backreferences are used somewhere | |
5768 | 7400 | else in the whole regular expression.) |
5769 | 7401 | |
5770 | 7402 | =end original |
5771 | 7403 | |
5772 | C<S> の最良のマッチングのみが考慮されます。 | |
7404 | C<"S"> の最良のマッチングのみが考慮されます。 | |
5773 | (これは C<S> がキャプチャかっこを持っていて、そして正規表現全体の | |
7405 | (これは C<"S"> がキャプチャかっこを持っていて、そして正規表現全体の | |
5774 | 7406 | どこかで後方参照が使われている時のみ重要です.) |
5775 | 7407 | |
5776 | 7408 | =item C<(?!S)>, C<(?<!S)> |
5777 | 7409 | |
5778 | 7410 | =begin original |
5779 | 7411 | |
5780 | 7412 | For this grouping operator there is no need to describe the ordering, since |
5781 | only whether or not C<S> can match is important. | |
7413 | only whether or not C<"S"> can match is important. | |
5782 | 7414 | |
5783 | 7415 | =end original |
5784 | 7416 | |
5785 | このグループ演算子では、C<S> がマッチングできるかどうかのみが重要なので、 | |
7417 | このグループ演算子では、C<"S"> がマッチングできるかどうかのみが重要なので、 | |
5786 | 7418 | 順序についての説明は必要ありません。 |
5787 | 7419 | |
5788 | =item C<(??{ EXPR })>, C<(?PARNO)> | |
7420 | =item C<(??{ I<EXPR> })>, C<(?I<PARNO>)> | |
5789 | 7421 | |
5790 | 7422 | =begin original |
5791 | 7423 | |
5792 | 7424 | 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. | |
7425 | the result of I<EXPR>, or the pattern contained by capture group I<PARNO>. | |
5794 | 7426 | |
5795 | 7427 | =end original |
5796 | 7428 | |
5797 | 順序は EXPR の結果の正規表現、または捕捉グループ PARNO に含まれている | |
7429 | 順序は I<EXPR> の結果の正規表現、または捕捉グループ I<PARNO> に含まれている | |
5798 | 7430 | パターンと同じです。 |
5799 | 7431 | |
5800 | =item C<(?(condition)yes-pattern|no-pattern)> | |
7432 | =item C<(?(I<condition>)I<yes-pattern>|I<no-pattern>)> | |
5801 | 7433 | |
5802 | 7434 | =begin original |
5803 | 7435 | |
5804 | Recall that which of | |
7436 | Recall that which of I<yes-pattern> or I<no-pattern> actually matches is | |
5805 | 7437 | already determined. The ordering of the matches is the same as for the |
5806 | 7438 | chosen subexpression. |
5807 | 7439 | |
5808 | 7440 | =end original |
5809 | 7441 | |
5810 | 既に決定している | |
7442 | 既に決定している I<yes-pattern> または I<no-pattern> を実際に | |
5811 | 7443 | マッチングさせます。 |
5812 | 7444 | マッチングの順序は選択された部分式と同じです。 |
5813 | 7445 | |
5814 | 7446 | =back |
5815 | 7447 | |
5816 | 7448 | =begin original |
5817 | 7449 | |
5818 | 7450 | The above recipes describe the ordering of matches I<at a given position>. |
5819 | 7451 | One more rule is needed to understand how a match is determined for the |
5820 | 7452 | whole regular expression: a match at an earlier position is always better |
5821 | 7453 | than a match at a later position. |
5822 | 7454 | |
5823 | 7455 | =end original |
5824 | 7456 | |
5825 | 7457 | ここにあげたレシピはI<与えられた位置での>マッチングの順序について |
5826 | 7458 | 説明しています。 |
5827 | 7459 | 正規表現全体でマッチングがどのように決定されるかを理解するためには |
5828 | 7460 | もう少しルールが必要です: |
5829 | 7461 | より若い位置でのマッチングは後ろの方でのマッチングよりもより良いです。 |
5830 | 7462 | |
5831 | 7463 | =head2 Creating Custom RE Engines |
5832 | 7464 | |
5833 | 7465 | (カスタム RE エンジンの作成) |
5834 | 7466 | |
5835 | 7467 | =begin original |
5836 | 7468 | |
5837 | 7469 | As of Perl 5.10.0, one can create custom regular expression engines. This |
5838 | 7470 | is not for the faint of heart, as they have to plug in at the C level. See |
5839 | 7471 | L<perlreapi> for more details. |
5840 | 7472 | |
5841 | 7473 | =end original |
5842 | 7474 | |
5843 | 7475 | Perl 5.10.0 から、誰でもカスタム正規表現エンジンを作成できます。 |
5844 | 7476 | これは気弱な人向けではありません; C レベルでプラグインする必要があるからです。 |
5845 | 7477 | さらなる詳細については L<perlreapi> を参照して下さい。 |
5846 | 7478 | |
5847 | 7479 | =begin original |
5848 | 7480 | |
5849 | 7481 | As an alternative, overloaded constants (see L<overload>) provide a simple |
5850 | 7482 | way to extend the functionality of the RE engine, by substituting one |
5851 | 7483 | pattern for another. |
5852 | 7484 | |
5853 | 7485 | =end original |
5854 | 7486 | |
5855 | 7487 | 代替案として、オーバーロードされた定数(L<overload> 参照)は |
5856 | 7488 | あるパターンを別のパターンに置き換えることで、RE エンジンの機能を |
5857 | 7489 | 拡張する簡単な方法を提供します。 |
5858 | 7490 | |
5859 | 7491 | =begin original |
5860 | 7492 | |
5861 | 7493 | Suppose that we want to enable a new RE escape-sequence C<\Y|> which |
5862 | 7494 | matches at a boundary between whitespace characters and non-whitespace |
5863 | 7495 | characters. Note that C<(?=\S)(?<!\S)|(?!\S)(?<=\S)> matches exactly |
5864 | 7496 | at these positions, so we want to have each C<\Y|> in the place of the |
5865 | 7497 | more complicated version. We can create a module C<customre> to do |
5866 | 7498 | this: |
5867 | 7499 | |
5868 | 7500 | =end original |
5869 | 7501 | |
5870 | 7502 | 新しい正規表現エスケープシーケンス、空白文字と非空白文字との |
5871 | 7503 | 境界にマッチングする C<\Y|> を作ってみることにします。 |
5872 | 7504 | この位置には実際には C<(?=\S)(?<!\S)|(?!\S)(?<=\S)> がマッチングするので、 |
5873 | 7505 | この複雑なバージョンを C<\Y|> で置き換えたいとします。 |
5874 | 7506 | このために C<customre> モジュールを作ります: |
5875 | 7507 | |
5876 | 7508 | package customre; |
5877 | 7509 | use overload; |
5878 | 7510 | |
5879 | 7511 | sub import { |
5880 | 7512 | shift; |
5881 | 7513 | die "No argument to customre::import allowed" if @_; |
5882 | 7514 | overload::constant 'qr' => \&convert; |
5883 | 7515 | } |
5884 | 7516 | |
5885 | 7517 | sub invalid { die "/$_[0]/: invalid escape '\\$_[1]'"} |
5886 | 7518 | |
5887 | 7519 | # We must also take care of not escaping the legitimate \\Y| |
5888 | 7520 | # sequence, hence the presence of '\\' in the conversion rules. |
5889 | 7521 | my %rules = ( '\\' => '\\\\', |
5890 | 7522 | 'Y|' => qr/(?=\S)(?<!\S)|(?!\S)(?<=\S)/ ); |
5891 | 7523 | sub convert { |
5892 | 7524 | my $re = shift; |
5893 | 7525 | $re =~ s{ |
5894 | 7526 | \\ ( \\ | Y . ) |
5895 | 7527 | } |
5896 | 7528 | { $rules{$1} or invalid($re,$1) }sgex; |
5897 | 7529 | return $re; |
5898 | 7530 | } |
5899 | 7531 | |
5900 | 7532 | =begin original |
5901 | 7533 | |
5902 | 7534 | Now C<use customre> enables the new escape in constant regular |
5903 | expressions, i.e., those without any runtime variable interpolations. | |
7535 | expressions, I<i.e.>, those without any runtime variable interpolations. | |
5904 | 7536 | As documented in L<overload>, this conversion will work only over |
5905 | 7537 | literal parts of regular expressions. For C<\Y|$re\Y|> the variable |
5906 | 7538 | 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): | |
7539 | (but only if the special meaning of C<\Y|> should be enabled inside C<$re>): | |
5908 | 7540 | |
5909 | 7541 | =end original |
5910 | 7542 | |
5911 | 7543 | これで C<use customre> することで正規表現定数の中で新しいエスケープを |
5912 | 7544 | 使うことが出来ます; すなわち、これには何の実行時変数の埋め込みもいりません。 |
5913 | 7545 | L<overload> に書かれているように、この変換は正規表現のリテラル部分にのみ |
5914 | 7546 | 動作します。 |
5915 | 7547 | C<\Y|$re\Y|> であればこの正規表現の変数部分は明示的に変換する |
5916 | 必要があります(とはいえ $re の中でも C<\Y|> を有効にしたい時のみ)。 | |
7548 | 必要があります(とはいえ C<$re> の中でも C<\Y|> を有効にしたい時のみ)。 | |
5917 | 7549 | |
5918 | 7550 | use customre; |
5919 | 7551 | $re = <>; |
5920 | 7552 | chomp $re; |
5921 | 7553 | $re = customre::convert $re; |
5922 | 7554 | /\Y|$re\Y|/; |
5923 | 7555 | |
7556 | =head2 Embedded Code Execution Frequency | |
7557 | ||
7558 | (組み込みコードの実行頻度) | |
7559 | ||
7560 | =begin original | |
7561 | ||
7562 | The exact rules for how often C<(??{})> and C<(?{})> are executed in a pattern | |
7563 | are unspecified. In the case of a successful match you can assume that | |
7564 | they DWIM and will be executed in left to right order the appropriate | |
7565 | number of times in the accepting path of the pattern as would any other | |
7566 | meta-pattern. How non-accepting pathways and match failures affect the | |
7567 | number of times a pattern is executed is specifically unspecified and | |
7568 | may vary depending on what optimizations can be applied to the pattern | |
7569 | and is likely to change from version to version. | |
7570 | ||
7571 | =end original | |
7572 | ||
7573 | パターン中で C<(??{})> と C<(?{})> がどれくらいの頻度で実行されるかの | |
7574 | 正確な規則は未規定です。 | |
7575 | マッチングが成功した場合、それらは DWIM を行い、 | |
7576 | 他のメタパターンと同様、 | |
7577 | パターンの受け入れられたパスの中で左から右の順序で適切な回数 | |
7578 | 実行されることを仮定できます。 | |
7579 | 受け入れられなかったパスとマッチングの失敗がどれくらいパターンの実行回数に | |
7580 | 影響を与えるかは明確に非規定で、 | |
7581 | パターンにどの最適化が適用できるかに依存し、バージョン毎に | |
7582 | 変わる可能性が高いです。 | |
7583 | ||
7584 | =begin original | |
7585 | ||
7586 | For instance in | |
7587 | ||
7588 | =end original | |
7589 | ||
7590 | 例えば: | |
7591 | ||
7592 | "aaabcdeeeee"=~/a(?{print "a"})b(?{print "b"})cde/; | |
7593 | ||
7594 | =begin original | |
7595 | ||
7596 | the exact number of times "a" or "b" are printed out is unspecified for | |
7597 | failure, but you may assume they will be printed at least once during | |
7598 | a successful match, additionally you may assume that if "b" is printed, | |
7599 | it will be preceded by at least one "a". | |
7600 | ||
7601 | =end original | |
7602 | ||
7603 | 失敗時に "a" や "b" が何回表示されるかは未規定ですが、 | |
7604 | マッチングに成功したときに少なくとも 1 回表示されることは仮定でき、 | |
7605 | さらに "b" が表示されるとき、その前には少なくとも 1 回 "a" が | |
7606 | 表示されることも仮定できます。 | |
7607 | ||
7608 | =begin original | |
7609 | ||
7610 | In the case of branching constructs like the following: | |
7611 | ||
7612 | =end original | |
7613 | ||
7614 | 次のような分岐構文の場合: | |
7615 | ||
7616 | /a(b|(?{ print "a" }))c(?{ print "c" })/; | |
7617 | ||
7618 | =begin original | |
7619 | ||
7620 | you can assume that the input "ac" will output "ac", and that "abc" | |
7621 | will output only "c". | |
7622 | ||
7623 | =end original | |
7624 | ||
7625 | 入力が "ac" なら出力は "ac"、入力が "abc" なら出力は | |
7626 | "c" だけと仮定できます。 | |
7627 | ||
7628 | =begin original | |
7629 | ||
7630 | When embedded code is quantified, successful matches will call the | |
7631 | code once for each matched iteration of the quantifier. For | |
7632 | example: | |
7633 | ||
7634 | =end original | |
7635 | ||
7636 | 組み込みコードが量指定された場合、マッチングに成功すると | |
7637 | 量指定子のそれぞれのマッチングした反復毎に 1 回コードを呼び出します。 | |
7638 | 例えば: | |
7639 | ||
7640 | "good" =~ /g(?:o(?{print "o"}))*d/; | |
7641 | ||
7642 | =begin original | |
7643 | ||
7644 | will output "o" twice. | |
7645 | ||
7646 | =end original | |
7647 | ||
7648 | これは "o" を 2 回出力します。 | |
7649 | ||
5924 | 7650 | =head2 PCRE/Python Support |
5925 | 7651 | |
5926 | 7652 | (PCRE/Python サポート) |
5927 | 7653 | |
5928 | 7654 | =begin original |
5929 | 7655 | |
5930 | 7656 | As of Perl 5.10.0, Perl supports several Python/PCRE-specific extensions |
5931 | 7657 | to the regex syntax. While Perl programmers are encouraged to use the |
5932 | 7658 | Perl-specific syntax, the following are also accepted: |
5933 | 7659 | |
5934 | 7660 | =end original |
5935 | 7661 | |
5936 | 7662 | Perl 5.10.0 時点では Perl は幾つかの Python/PCRE 的な正規表現構文拡張を |
5937 | 7663 | サポートします。 |
5938 | 7664 | Perl プログラマはこれらの Perl としての構文を推奨しますが、以下のものも |
5939 | 7665 | 受理されます: |
5940 | 7666 | |
5941 | 7667 | =over 4 |
5942 | 7668 | |
5943 | =item C<< (?PE<lt>NAMEE<gt>pattern) >> | |
7669 | =item C<< (?PE<lt>I<NAME>E<gt>I<pattern>) >> | |
5944 | 7670 | |
5945 | 7671 | =begin original |
5946 | 7672 | |
5947 | Define a named capture group. Equivalent to C<< (?<NAME>pattern) >>. | |
7673 | Define a named capture group. Equivalent to C<< (?<I<NAME>>I<pattern>) >>. | |
5948 | 7674 | |
5949 | 7675 | =end original |
5950 | 7676 | |
5951 | 7677 | 名前付の捕捉グループの定義。 |
5952 | C<< (?<NAME>pattern) >> と等価。 | |
7678 | C<< (?<I<NAME>>I<pattern>) >> と等価。 | |
5953 | 7679 | |
5954 | =item C<< (?P=NAME) >> | |
7680 | =item C<< (?P=I<NAME>) >> | |
5955 | 7681 | |
5956 | 7682 | =begin original |
5957 | 7683 | |
5958 | Backreference to a named capture group. Equivalent to C<< \g{NAME} >>. | |
7684 | Backreference to a named capture group. Equivalent to C<< \g{I<NAME>} >>. | |
5959 | 7685 | |
5960 | 7686 | =end original |
5961 | 7687 | |
5962 | 7688 | 名前付捕捉グループへの後方参照。 |
5963 | C<< \g{NAME} >> と等価。 | |
7689 | C<< \g{I<NAME>} >> と等価。 | |
5964 | 7690 | |
5965 | =item C<< (?P>NAME) >> | |
7691 | =item C<< (?P>I<NAME>) >> | |
5966 | 7692 | |
5967 | 7693 | =begin original |
5968 | 7694 | |
5969 | Subroutine call to a named capture group. Equivalent to C<< (?&NAME) >>. | |
7695 | Subroutine call to a named capture group. Equivalent to C<< (?&I<NAME>) >>. | |
5970 | 7696 | |
5971 | 7697 | =end original |
5972 | 7698 | |
5973 | 7699 | 名前付き捕捉グループへの関数呼び出し。 |
5974 | C<< (?&NAME) >> と等価。 | |
7700 | C<< (?&I<NAME>) >> と等価。 | |
5975 | 7701 | |
5976 | 7702 | =back |
5977 | 7703 | |
5978 | 7704 | =head1 BUGS |
5979 | 7705 | |
5980 | 7706 | =begin original |
5981 | 7707 | |
5982 | Many regular expression constructs don't work on EBCDIC platforms. | |
5983 | ||
5984 | =end original | |
5985 | ||
5986 | 多くの正規表現構文は EBCDIC プラットフォームでは動作しません。 | |
5987 | ||
5988 | =begin original | |
5989 | ||
5990 | 7708 | There are a number of issues with regard to case-insensitive matching |
5991 | in Unicode rules. See C<i> under L</Modifiers> above. | |
7709 | in Unicode rules. See C<"i"> under L</Modifiers> above. | |
5992 | 7710 | |
5993 | 7711 | =end original |
5994 | 7712 | |
5995 | 7713 | Unicode ルールでの大文字小文字を無視したマッチングには多くの問題が |
5996 | 7714 | あります。 |
5997 | 上述の L</Modifiers> の C<i> を参照してください。 | |
7715 | 上述の L</Modifiers> の C<"i"> を参照してください。 | |
5998 | 7716 | |
5999 | 7717 | =begin original |
6000 | 7718 | |
6001 | 7719 | This document varies from difficult to understand to completely |
6002 | 7720 | and utterly opaque. The wandering prose riddled with jargon is |
6003 | 7721 | hard to fathom in several places. |
6004 | 7722 | |
6005 | 7723 | =end original |
6006 | 7724 | |
6007 | 7725 | この文書は、理解が困難なところから、完全かつ徹底的に不明瞭なところまで |
6008 | 7726 | さまざまです。 |
6009 | 7727 | jargon に満ちたとりとめのない散文は幾つかの箇所で理解するのに |
6010 | 7728 | 難儀ではあるでしょう。 |
6011 | 7729 | |
6012 | 7730 | =begin original |
6013 | 7731 | |
6014 | 7732 | This document needs a rewrite that separates the tutorial content |
6015 | 7733 | from the reference content. |
6016 | 7734 | |
6017 | 7735 | =end original |
6018 | 7736 | |
6019 | 7737 | この文書はリファレンス的な内容からチュートリアル的な内容を分離して |
6020 | 7738 | 書き直す必要があります。 |
6021 | 7739 | |
6022 | 7740 | =head1 SEE ALSO |
7741 | ||
7742 | =begin original | |
7743 | ||
7744 | The syntax of patterns used in Perl pattern matching evolved from those | |
7745 | supplied in the Bell Labs Research Unix 8th Edition (Version 8) regex | |
7746 | routines. (The code is actually derived (distantly) from Henry | |
7747 | Spencer's freely redistributable reimplementation of those V8 routines.) | |
7748 | ||
7749 | =end original | |
7750 | ||
7751 | Perl のパターンマッチングで使われる文法は、 | |
7752 | the Bell Labs Research Unix 8th Edition (Version 8) 正規表現ルーチンで | |
7753 | 提供されているものからの派生です。 | |
7754 | (コードは実際には Henry Spencer の自由に再配布可能な V8 ルーチンの再実装から | |
7755 | (遠く)派生しています)。 | |
6023 | 7756 | |
6024 | 7757 | L<perlrequick>. |
6025 | 7758 | |
6026 | 7759 | L<perlretut>. |
6027 | 7760 | |
6028 | 7761 | L<perlop/"Regexp Quote-Like Operators">. |
6029 | 7762 | |
6030 | 7763 | L<perlop/"Gory details of parsing quoted constructs">. |
6031 | 7764 | |
6032 | 7765 | L<perlfaq6>. |
6033 | 7766 | |
6034 | 7767 | L<perlfunc/pos>. |
6035 | 7768 | |
6036 | 7769 | L<perllocale>. |
6037 | 7770 | |
6038 | 7771 | L<perlebcdic>. |
6039 | 7772 | |
6040 | 7773 | =begin original |
6041 | 7774 | |
6042 | 7775 | I<Mastering Regular Expressions> by Jeffrey Friedl, published |
6043 | 7776 | by O'Reilly and Associates. |
6044 | 7777 | |
6045 | 7778 | =end original |
6046 | 7779 | |
6047 | 7780 | O'Reilly and Associates から出版されている、Jeffrey Friedl による |
6048 | 7781 | I<Mastering Regular Expressions> (詳説 正規表現) |
6049 | 7782 | |
6050 | 7783 | =begin meta |
6051 | 7784 | |
6052 | 7785 | Translate: 山科 氷魚 (YAMASHINA Hio) <hio@hio.jp> (5.10.0) |
6053 | 7786 | Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.1-) |
6054 | 7787 | Status: completed |
6055 | 7788 | |
6056 | 7789 | =end meta |