<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>MT312 &#187; 2007 &#187; 10 月</title>
	<link>http://www.mt312.com</link>
	<description></description>
	<pubDate>Sun, 13 Jul 2008 21:49:58 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>ja</language>
			<item>
		<title>正規表現メモ</title>
		<link>http://www.mt312.com/php/46/</link>
		<comments>http://www.mt312.com/php/46/#comments</comments>
		<pubDate>Mon, 01 Oct 2007 13:21:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.mt312.com/php/46/</guid>
		<description><![CDATA[パターン中のメタ文字を纏めてエスケープする
PHP4.3.3以降では、\Q～\Eで囲むとメタ文字を纏めてエスケープできます。
PLAIN TEXT
PHP:




preg_match&#40;'/^\Q(+*%&#38;)\E$/', '(+*%&#38;)'&#41;; 






部分的にオプションを変更する
(?オプション文字)を使うとオプションの設定と解除が可能です。
PLAIN TEXT
PHP:




// 設定


preg_match&#40;'/abc((?i)def)ghi/', 'abcDEFghi', $matches&#41;;


print_r&#40;$matches&#41;;


// Array ( [0] =&#62; abcDEFghi [1] =&#62; DEF )


&#160;


// 解除


preg_match&#40;'/abc((?-i)def)ghi/i', 'ABCdefGHI', $matches&#41;;


print_r&#40;$matches&#41;;


// Array ( [0] =&#62; ABCdefGHI [1] =&#62; def ) 






部分的にキャプチャを避ける
(?:)を使うとサブパターンがキャプチャされなくなります。
PLAIN TEXT
PHP:




preg_match&#40;'/(?:say) (hello)/', 'say hello', $matches&#41;;


print_r&#40;$matches&#41;;


// Array ( [0] =&#62; say hello [1] =&#62; hello ) 






キャプチャにキーを指定する
(?P&#60;キー名&#62;パターン)でキーを指定できます。
PLAIN TEXT
PHP:




preg_match&#40;'/say (?P&#60;key&#62;hello)/', 'say hello', $matches&#41;;


print_r&#40;$matches&#41;;


// Array ( [0] =&#62; [...]]]></description>
			<content:encoded><![CDATA[<h3>パターン中のメタ文字を纏めてエスケープする</h3>
<p>PHP4.3.3以降では、\Q～\Eで囲むとメタ文字を纏めてエスケープできます。</p>
<div class="igBar"><span id="lphp-8"><a href="#" onclick="javascript:showCodeTxt('php-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-8">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#0000CC;">preg_match</span></a><span style="color:#006600;">&#40;</span><span style="color:#990000;">'/^<span style="color:#990000;">\Q</span>(+*%&amp;)<span style="color:#990000;">\E</span>$/'</span>, <span style="color:#990000;">'(+*%&amp;)'</span><span style="color:#006600;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>部分的にオプションを変更する</h3>
<p>(?オプション文字)を使うとオプションの設定と解除が可能です。</p>
<div class="igBar"><span id="lphp-9"><a href="#" onclick="javascript:showCodeTxt('php-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-9">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// 設定</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#0000CC;">preg_match</span></a><span style="color:#006600;">&#40;</span><span style="color:#990000;">'/abc((?i)def)ghi/'</span>, <span style="color:#990000;">'abcDEFghi'</span>, <span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#0000CC;">print_r</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// Array ( [0] =&gt; abcDEFghi [1] =&gt; DEF )</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// 解除</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#0000CC;">preg_match</span></a><span style="color:#006600;">&#40;</span><span style="color:#990000;">'/abc((?-i)def)ghi/i'</span>, <span style="color:#990000;">'ABCdefGHI'</span>, <span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#0000CC;">print_r</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// Array ( [0] =&gt; ABCdefGHI [1] =&gt; def ) </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>部分的にキャプチャを避ける</h3>
<p>(?:)を使うとサブパターンがキャプチャされなくなります。</p>
<div class="igBar"><span id="lphp-10"><a href="#" onclick="javascript:showCodeTxt('php-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-10">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#0000CC;">preg_match</span></a><span style="color:#006600;">&#40;</span><span style="color:#990000;">'/(?:say) (hello)/'</span>, <span style="color:#990000;">'say hello'</span>, <span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#0000CC;">print_r</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// Array ( [0] =&gt; say hello [1] =&gt; hello ) </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>キャプチャにキーを指定する</h3>
<p>(?P&lt;キー名&gt;パターン)でキーを指定できます。</p>
<div class="igBar"><span id="lphp-11"><a href="#" onclick="javascript:showCodeTxt('php-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-11">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#0000CC;">preg_match</span></a><span style="color:#006600;">&#40;</span><span style="color:#990000;">'/say (?P&lt;key&gt;hello)/'</span>, <span style="color:#990000;">'say hello'</span>, <span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#0000CC;">print_r</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// Array ( [0] =&gt; say hello [key] =&gt; hello [1] =&gt; hello ) </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>先読み言明</h3>
<p>ある文字の直後を限定します。<br />
言明パターンはキャプチャされません。</p>
<div class="igBar"><span id="lphp-12"><a href="#" onclick="javascript:showCodeTxt('php-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-12">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// 肯定: 直後がbarのfoo</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#0000CC;">preg_match</span></a><span style="color:#006600;">&#40;</span><span style="color:#990000;">'/foo(?=bar)/'</span>, <span style="color:#990000;">'foobar'</span>, <span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#0000CC;">print_r</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// Array ( [0] =&gt; foo )</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// 否定: 直後がvaa以外のfoo</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#0000CC;">preg_match</span></a><span style="color:#006600;">&#40;</span><span style="color:#990000;">'/foo(?!vaa)/'</span>, <span style="color:#990000;">'foobar'</span>, <span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#0000CC;">print_r</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// Array ( [0] =&gt; foo ) </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>戻り読み言明</h3>
<p>ある文字の直前を限定します。<br />
言明パターンはキャプチャされません。</p>
<div class="igBar"><span id="lphp-13"><a href="#" onclick="javascript:showCodeTxt('php-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-13">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// 肯定: 直前がfooのbar</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#0000CC;">preg_match</span></a><span style="color:#006600;">&#40;</span><span style="color:#990000;">'/(?&lt;=foo)bar/'</span>, <span style="color:#990000;">'foobar'</span>, <span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#0000CC;">print_r</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// Array ( [0] =&gt; bar )</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// 否定: 直前がhoo以外のbar</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#0000CC;">preg_match</span></a><span style="color:#006600;">&#40;</span><span style="color:#990000;">'/(?&lt;!hoo)bar/'</span>, <span style="color:#990000;">'foobar'</span>, <span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#0000CC;">print_r</span></a><span style="color:#006600;">&#40;</span><span style="color:#0000CC;">$matches</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// Array ( [0] =&gt; bar ) </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>繰り返し指定の再試行をキャンセル</h3>
<p>サブパターン中に*、+、{}によって繰り返しが指定されている場合、その続きのマッチングに失敗すると、繰り返し部分の桁が調整され、再度マッチングが行われます。この繰り返しが無駄だと分かっている場合には、再試行無しのサブパターン(?>)を使ってキャンセルできます。<br />
このパターンもキャプチャされません。</p>
<div class="igBar"><span id="lphp-14"><a href="#" onclick="javascript:showCodeTxt('php-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-14">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// 再試行あり</span></div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#0000CC;">preg_match</span></a><span style="color:#006600;">&#40;</span><span style="color:#990000;">'/(<span style="color:#990000;">\d</span>+)bar/'</span>, <span style="color:#990000;">'0123456789foo'</span><span style="color:#006600;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:IG_LINE_COLOUR_2;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#F27900;">// 再試行なし</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:IG_LINE_COLOUR_1;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/preg_match"><span style="color:#0000CC;">preg_match</span></a><span style="color:#006600;">&#40;</span><span style="color:#990000;">'/(?&gt;<span style="color:#990000;">\d</span>+)bar/'</span>, <span style="color:#990000;">'0123456789foo'</span><span style="color:#006600;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>参考文献</h3>
<p><a href="http://jp.php.net/manual/ja/reference.pcre.pattern.syntax.php">PHPパターン構文</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mt312.com/php/46/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
