forked from php/doc-ja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreg-match-all.xml
483 lines (454 loc) · 13.3 KB
/
preg-match-all.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 6712ff65342f3c87d03f90fa420782d37b9be58c Maintainer: hirokawa Status: ready -->
<!-- Credits: haruki,mumumu -->
<refentry xml:id="function.preg-match-all" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>preg_match_all</refname>
<refpurpose>繰り返し正規表現検索を行う</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type class="union"><type>int</type><type>false</type></type><methodname>preg_match_all</methodname>
<methodparam><type>string</type><parameter>pattern</parameter></methodparam>
<methodparam><type>string</type><parameter>subject</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter role="reference">matches</parameter><initializer>&null;</initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>offset</parameter><initializer>0</initializer></methodparam>
</methodsynopsis>
<para>
<parameter>subject</parameter> を検索し、
<parameter>pattern</parameter> に指定した正規表現にマッチした
すべての文字列を、<parameter>flags</parameter> で指定した
順番で、<parameter>matches</parameter> に代入します。
</para>
<para>
正規表現にマッチすると、そのマッチした文字列の後から
検索が続行されます。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>pattern</parameter></term>
<listitem>
<para>
検索するパターンを表す文字列。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>subject</parameter></term>
<listitem>
<para>
入力文字列。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>matches</parameter></term>
<listitem>
<para>
マッチしたすべての内容を含む、
<parameter>flags</parameter>
で指定した形式の多次元配列。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
以下のフラグの組み合わせ
(<constant>PREG_PATTERN_ORDER</constant> と
<constant>PREG_SET_ORDER</constant> を組み合わせて使用することは
意味をなさないので注意してください)。
<variablelist>
<varlistentry>
<term><constant>PREG_PATTERN_ORDER</constant></term>
<listitem>
<para>
<varname>$matches[0]</varname> はパターン全体にマッチした文字列の配列、
<varname>$matches[1]</varname> は第 1 のキャプチャ用サブパターンにマッチした文字列の配列、
といった順番となります。
</para>
<para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
"<b>example: </b><div align=left>this is a test</div>",
$out, PREG_PATTERN_ORDER);
echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";
?>
]]>
</programlisting>
&example.outputs;
<screen role="html">
<![CDATA[
<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test
]]>
</screen>
<para>
<varname>$out[0]</varname> はパターン全体にマッチした文字列の配列を有しており、
<varname>$out[1]</varname> はタグで囲まれた文字列の配列を有しています。
</para>
</informalexample>
</para>
<para>
パターンに名前付きサブパターンが含まれる場合は、<varname>$matches</varname>
にはサブパターン名のキーも含まれるようになります。
</para>
<para>
サブパターンの名前が重複している場合は、いちばん右側のサブパターンだけが
<varname>$matches[NAME]</varname> に格納されます。
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
preg_match_all(
'/(?J)(?<match>foo)|(?<match>bar)/',
'foo bar',
$matches,
PREG_PATTERN_ORDER
);
print_r($matches['match']);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] =>
[1] => bar
)
]]>
</screen>
</informalexample>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>PREG_SET_ORDER</constant></term>
<listitem>
<para>
<varname>$matches[0]</varname> は 1 回目のマッチングでキャプチャした値の配列、
<varname>$matches[1]</varname> は 2 回目のマッチングでキャプチャした値の配列、
といった順序となります。
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
"<b>example: </b><div align=\"left\">this is a test</div>",
$out, PREG_SET_ORDER);
echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";
?>
]]>
</programlisting>
&example.outputs;
<screen role="html">
<![CDATA[
<b>example: </b>, example:
<div align="left">this is a test</div>, this is a test
]]>
</screen>
</informalexample>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>PREG_OFFSET_CAPTURE</constant></term>
<listitem>
<para>
このフラグを設定した場合、各マッチに対応する文字列のオフセットも(バイト単位で)返されます。
これは、<parameter>matches</parameter> の値を配列の配列の配列に変更することに注意してください。
配列の要素である配列のすべての要素が、オフセット <literal>0</literal> に、マッチした文字列、
オフセット <literal>1</literal> に、<parameter>subject</parameter> 内でのその文字列のオフセット
からなる配列になります。
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
preg_match_all('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => Array
(
[0] => Array
(
[0] => foobarbaz
[1] => 0
)
)
[1] => Array
(
[0] => Array
(
[0] => foo
[1] => 0
)
)
[2] => Array
(
[0] => Array
(
[0] => bar
[1] => 3
)
)
[3] => Array
(
[0] => Array
(
[0] => baz
[1] => 6
)
)
)
]]>
</screen>
</informalexample>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>PREG_UNMATCHED_AS_NULL</constant></term>
<listitem>
<para>
このフラグを設定すると、サブパターンがマッチしなかった場合に &null; が渡されます。
通常は、空の <type>string</type> が渡されます。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>
<parameter>flags</parameter> を指定しない場合は、
<constant>PREG_PATTERN_ORDER</constant> が指定されたことになります。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>offset</parameter></term>
<listitem>
<para>
通常、検索は対象文字列の先頭から開始されます。
オプションのパラメータ <parameter>offset</parameter> を使用して
検索の開始位置を (バイト単位で) 指定することも可能です。
</para>
<note>
<para>
<parameter>offset</parameter> を用いるのと、
<code>substr($subject, $offset)</code> を
<function>preg_match_all</function>の対象文字列として指定するのとは
等価ではありません。
これは、<parameter>pattern</parameter> には、
<emphasis>^</emphasis>, <emphasis>$</emphasis> や
<emphasis>(?<=x)</emphasis> のような言明を含めることができるためです。
これに関する例については、
<function>preg_match</function> を参照ください。
</para>
</note>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
パターンがマッチした総数を返します(ゼロとなる可能性もあります)。
&return.falseforfailure;
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
&pcre.pattern.warning;
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>7.2.0</entry>
<entry>
<parameter>$flags</parameter> パラメータが
<constant>PREG_UNMATCHED_AS_NULL</constant> をサポートしました。
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>テキストからすべての電話番号を得る</title>
<programlisting role="php">
<![CDATA[
<?php
preg_match_all("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",
"Call 555-1212 or 1-800-555-1212", $phones);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>HTML タグにマッチするものを見付ける(貪欲)</title>
<programlisting role="php">
<![CDATA[
<?php
// \\2 は後方参照の例。これは、pcre に正規表現中の括弧の 2 番目の
// 組、つまりこの場合は ([\w]+)、にマッチする。文字列が二重引用符で
// 括られているため、バックスラッシュの追加が必要。
$html = "<b>bold text</b><a href=howdy.html>click me</a>";
preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $html, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
echo "matched: " . $val[0] . "\n";
echo "part 1: " . $val[1] . "\n";
echo "part 2: " . $val[2] . "\n";
echo "part 3: " . $val[3] . "\n";
echo "part 4: " . $val[4] . "\n\n";
}
?>
]]>
</programlisting>
&example.outputs;
<screen role="html">
<![CDATA[
matched: <b>bold text</b>
part 1: <b>
part 2: b
part 3: bold text
part 4: </b>
matched: <a href=howdy.html>click me</a>
part 1: <a href=howdy.html>
part 2: a
part 3: click me
part 4: </a>
]]>
</screen>
</example>
</para>
<para>
<example>
<title>名前つきサブパターンの使用法</title>
<programlisting role="php">
<![CDATA[
<?php
$str = <<<FOO
a: 1
b: 2
c: 3
FOO;
preg_match_all('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
/* 別のやり方 */
// preg_match_all('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
print_r($matches);
?>
]]>
</programlisting>
&example.outputs;
<screen role="html">
<![CDATA[
Array
(
[0] => Array
(
[0] => a: 1
[1] => b: 2
[2] => c: 3
)
[name] => Array
(
[0] => a
[1] => b
[2] => c
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
)
[digit] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="pcre.pattern">PCRE のパターン</link></member>
<member><function>preg_quote</function></member>
<member><function>preg_match</function></member>
<member><function>preg_replace</function></member>
<member><function>preg_split</function></member>
<member><function>preg_last_error</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->