<?xml version="1.0" encoding="UTF-8"?>
<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/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CNXCT小组的博客 &#187; 反向引用</title>
	<atom:link href="http://www.cnxct.com/tag/%e5%8f%8d%e5%90%91%e5%bc%95%e7%94%a8/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cnxct.com</link>
	<description>技术这个东西如同一个圆 ,刚开始的时候我们就如同站在圆心,一旦投入学习下去 ,圆就慢慢变大 ,圆的边缘以外也就会越来越大,接触的多了 知道的多了, 就会发现自己真的很无知!</description>
	<lastBuildDate>Tue, 31 Jan 2012 07:56:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>coolcode转SyntaxHighlighter与Mysql正则表达式</title>
		<link>http://www.cnxct.com/coolcode%e8%bd%acsyntaxhighlighter%e4%b8%8emysql%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/</link>
		<comments>http://www.cnxct.com/coolcode%e8%bd%acsyntaxhighlighter%e4%b8%8emysql%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 15:04:29 +0000</pubDate>
		<dc:creator>CFC4N</dc:creator>
				<category><![CDATA[所谓技术]]></category>
		<category><![CDATA[coolcode]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[SyntaxHighlighter]]></category>
		<category><![CDATA[反向引用]]></category>
		<category><![CDATA[正则]]></category>
		<category><![CDATA[正则表达式]]></category>

		<guid isPermaLink="false">http://www.cnxct.com/coolcode%e8%bd%acsyntaxhighlighter%e4%b8%8emysql%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/</guid>
		<description><![CDATA[<a href="http://www.cnxct.com/wp-content/uploads/2010/06/reg_11.jpg"><img src="http://www.cnxct.com/wp-content/uploads/2010/06/reg_11-150x150.jpg" alt="" title="简单的正则表达式匹配字符串" width="150" height="150" class="alignleft sided inline" /></a>
blog的代码高亮插件原来是coolcode的，coolcode的高亮插件确实很酷，显示效果也很棒，但是占用的位子太大了。最近，我抽空改成SyntaxHighlighter。由于coolcode插件的开头标签是或者[coolcode]这样的，而SyntaxHighlighter是[code]这样的（或者其他）。遂只能想办法把老的格式转化成新的格式。当然，肯定用到正则表达式了。]]></description>
			<content:encoded><![CDATA[<p>blog的代码高亮插件原来是coolcode的，coolcode的高亮插件确实很酷，显示效果也很棒，但是占用的位子太大了。最近，我抽空改成SyntaxHighlighter。由于coolcode插件的开头标签是
<pre class="brush: plain; title: ; notranslate">&lt;coolcode&gt;</pre>
<p>或者[coolcode]这样的，而SyntaxHighlighter是
<pre class="brush: plain; title: ; notranslate">1。<br />
mysql里执行两句sql即可</p>
<pre class="brush: sql; title: ; notranslate">
UPDATE wp_posts SET post_content = REPLACE(post_content,'&lt;/coolcode&gt;','[\/code]');  //注意后面多了个反斜杠，记得去掉
UPDATE wp_posts SET post_content = REPLACE(post_content,'[/coolcode]','[\/code]');  //注意后面多了个反斜杠，记得去掉
</pre>
<p>总结：<br />
本文牵扯的正则表达式并无高级用法，都是平常很简单的用法。关于PCRE引擎正则表达式的递归(迭代),组命名，反向引用，零宽断言等，CFC4N会在以后的时间里，找合适的例子写出来。当然，这些高级用法，<a href="http://www.cnxct.com/cfc4n%e5%b0%8f%e8%af%95%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/">CFC4N在帮朋友写的正则表达式</a>里已经用到了，大家可以看看，欢迎批评和指点。<br />
PS：如果需要coolcode转SyntaxHighlighter的完整PHP程序，留言即可，我抽空写出来。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cnxct.com/coolcode%e8%bd%acsyntaxhighlighter%e4%b8%8emysql%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>CFC4N小试正则表达式</title>
		<link>http://www.cnxct.com/cfc4n%e5%b0%8f%e8%af%95%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/</link>
		<comments>http://www.cnxct.com/cfc4n%e5%b0%8f%e8%af%95%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/#comments</comments>
		<pubDate>Mon, 31 May 2010 07:42:33 +0000</pubDate>
		<dc:creator>CFC4N</dc:creator>
				<category><![CDATA[所谓技术]]></category>
		<category><![CDATA[反向引用]]></category>
		<category><![CDATA[正则]]></category>
		<category><![CDATA[递归]]></category>
		<category><![CDATA[零宽断言]]></category>

		<guid isPermaLink="false">http://www.cnxct.com/?p=402</guid>
		<description><![CDATA[朋友甲：要求根据一串字符串，反转成PHP数组，其给出的字符串为php的print_r打印出来的。我们在暂不考虑此方法是否能确保数据的准确性，以及其他意外等情况，仅根据要求写正则。
<strong>朋友乙：要求批量给html字符串中a标签中不包含title属性的标签添加title，而且，其title内容为</strong>&#60;a href...&#62;到&#60;/a&#62;<strong>之间的文本。。</strong>
朋友丙:要求过滤非本域名，或者非本子域名的其他域名的UBB标签链接，一旦包含，直接替换成其中间的文本
朋友丁:要求读取squid的配置文件中，起作用的行，也就是没有#开头进行注释的行]]></description>
			<content:encoded><![CDATA[<p><strong>朋友甲：要求根据一串字符串，反转成PHP数组，其给出的字符串为php的print_r打印出来的。我们在暂不考虑此方法是否能确保数据的准确性，以及其他意外等情况，仅根据要求写正则。</strong><br />
其字符串为</p>
<pre class="brush: php; title: ; notranslate">
Array
(
    [tt] =&gt; Array
    (
        [table] =&gt; qqttcode
        [hitcode] =&gt; 1
    )

    [ww] =&gt; Array
        (
        [table] =&gt; qqwwcode
        [hitcode] =&gt;
        )

    [pp] =&gt; Array
        (
        [table] =&gt; qqppcode
        [hitcode] =&gt; Array
            (
            [table] =&gt; qqppcode
            [hitcode] =&gt;
            )
    )

)
</pre>
<p>CFC4N给出一下结果：</p>
<pre class="brush: php; title: ; notranslate">
$strRge1 = '/(\[([^]]+)]\s?=&gt;\s?)?Array[\s\S]+?\(([^()]|(?R))*\)/i';
$arrReturn = array();
if (preg_match_all($strRge1,$str,$tt1))
{
    $arrReturn = getarray($tt1[0][0]);
}
    $arrReturn2 = array();
foreach ($arrReturn as $k =&gt; $v)
{
    $arrReturn2[$k] = $v[$k];
}
print_r($arrReturn2);

function getarray ($strContents)
{
    $arrTemp = array();
    $strRge = '/\[([^]]+)]\s?=&gt;\s?Array[\s\S]+?\(([^()]|(?R))*\)/i';
    $strReg2 = '/\[([^]]+?)]\s?=&gt;\s?([\d\w]+)?/';
    if (preg_match_all($strRge,$strContents,$strTemp))
    {
        $num = count($strTemp[1]);
        if ($num &gt; '1')
        {
            for ($i=0; $i&lt;$num; $i++)
            {
                if (preg_match_all($strRge,$strTemp[0][$i],$arrTTT))
                {
                    $arrTemp[$strTemp[1][$i]] = array();
                    $arrTemp[$strTemp[1][$i]] = getarray($strTemp[0][$i]);
                }
                else
                {
                    $arrTemp[$strTemp[1][$i]] = $strTemp[0][$i];
                }
            }
        }
        else
        {

            $arrTemp[$strTemp[1][0]] = array();
            $arrTemp2 = array();
            if (preg_match_all($strReg2, $strTemp[0][0],$straa))
            {
                $num = count($straa[0]);
                for ($i=0; $i&lt;$num-1; $i++)
                {
                    $arrTemp2[$straa[1][$i+1]] = $straa[2][$i+1];
                }
            }
            $arrTemp[$strTemp[1][0]] = $arrTemp2;
        }

    }
    return $arrTemp;
}
</pre>
<p>结果是可以用的。但是发现其只能用于固定的三层嵌套，假如N层的话，无法用这个函数了，后来，我又改造一下那个正则，改为</p>
<pre class="brush: php; title: ; notranslate">
$strRge1 = '/\[(([^]]+)]\s?=&gt;\s?Array[\s]+?\(([^()])+|(?R))\)+/i';
</pre>
<p>但是，并不能解决问题。。各位看官，您认为，我的误区在哪里呢？<br />
附 第一个正则截图<br />
<a href="http://www.cnxct.com/wp-content/uploads/2010/05/array_regex_by_php_1.jpg" rel="lightbox[402]"><img src="http://www.cnxct.com/wp-content/uploads/2010/05/array_regex_by_php_1-150x150.jpg" alt="" title="php正则表达式递归(迭代)匹配字符串" width="150" height="150" class="aligncenter size-thumbnail wp-image-426" /></a><br />
更改后正则匹配截图<br />
<a href="http://www.cnxct.com/wp-content/uploads/2010/05/array_regex_by_php_2.jpg" rel="lightbox[402]"><img src="http://www.cnxct.com/wp-content/uploads/2010/05/array_regex_by_php_2-150x150.jpg" alt="" title="php正则表达式递归(迭代)匹配字符串_2" width="150" height="150" class="aligncenter size-thumbnail wp-image-427" /></a></p>
<p><strong>朋友乙：要求批量给html字符串中a标签中不包含title属性的标签添加title，而且，其title内容为</strong>&lt;a href&#8230;&gt;到&lt;/a&gt;<strong>之间的文本。。</strong><br />
CFC4N给出答案为:</p>
<pre class="brush: php; title: ; notranslate">
$str = '&lt;a &gt;ssss&lt;/a&gt;&lt;a href=&quot;ss&quot; &gt;ssss&lt;/a&gt;&lt;a title=&quot;ss&quot; &gt;ssss&lt;/a&gt;&lt;a href=&quot;&quot;&gt;ssss&lt;/a&gt;&lt;a title=&quot;ss&quot;&gt;ssss&lt;/a&gt;&lt;a title=&quot;ss&quot;&gt;ssss&lt;/a&gt;&lt;a title=&quot;ssf&quot;&gt;ssss&lt;/a&gt;';
$str = preg_replace('%&lt;a((?:(?!title=&quot;[^&quot;]+?&quot;)[\s\S])+?)&gt;(?:(?&lt;!&lt;/a&gt;)[\s\S])+?&lt;/a&gt;%im','&lt;a title=&quot;\\2&quot; \\1&gt;\\2&lt;/a&gt;',$str);
print_r($str);
</pre>
<p>各位看官，您认为，CFC4N写的正则表达式里，哪些还可以优化呢？这个效率是不是不高？？</p>
<p><strong>朋友丙:要求过滤非本域名，或者非本子域名的其他域名的UBB标签链接，一旦包含，直接替换成其中间的文本，比如例子字符串如下</strong></p>
<pre class="brush: plain; title: ; notranslate">
[url=http://www.sadas.cn]baidu[/url]

[url=www.ggasdwe.com]百度[/url]
[url=http://www.qq.com/index.php]QQ[/url]

[url=http://www.miyifun.com/index.html]其他

[/url]
[url=pc.qq.com/index.php]PC QQ[/url]
</pre>
<p>其中，字符串中不确定有几个换行等其他字符，而且，不确定url的UBB标签中的网址中是否包含http://，不确定二级域名或者三级域名</p>
<p>CFC4N给出的正则以及PHP代码如下</p>
<pre class="brush: php; title: ; notranslate">
$str = '[url=http://www.sadas.cn]baidu[/url]

[url=www.ggasdwe.com]百度[/url]
[url=http://www.qq.com/index.php]QQ[/url]

[url=http://www.miyifun.com/index.html]其他

[/url]
[url=pc.qq.com/index.php]PC QQ[/url]';
print_r(preg_replace('%\[url=(http://)?(?:(?!qq\.com)[^\]])*\][\r|\r\n]*([\s\S]+?)[\r|\r\n]*\[/url\]%i','\\2',$str));
</pre>
<p>各位看官，您认为这里哪里是多余的？还可以进行哪些正则的优化来提高效率?如果没看懂，那您的疑问在哪里？</p>
<p><strong>朋友丁:要求读取squid的配置文件中，起作用的行，也就是没有#开头进行注释的行</strong>。<br />
其中，squid的配置文件内字符串见附件中<br />
<a href="http://www.cnxct.com/wp-content/uploads/2010/05/squid.zip">squid的配置文件内容</a><br />
CFC4N给出正则代码如下</p>
<pre class="brush: php; title: ; notranslate">
preg_match_all('/^(?!#).+?$/m', file_get_contents('squid.conf'), $regs);
print_r($regs[0]);
</pre>
<p>运行截图</p>
<div id="attachment_415" class="wp-caption aligncenter" style="width: 160px"><a href="http://www.cnxct.com/wp-content/uploads/2010/05/squid.jpg" rel="lightbox[402]"><img class="size-thumbnail wp-image-415" title="PHP正则匹配squid.conf文件中启用的参数" src="http://www.cnxct.com/wp-content/uploads/2010/05/squid-150x150.jpg" alt="" width="150" height="150" /></a><p class="wp-caption-text">PHP正则匹配squid.conf文件中启用的参数</p></div>
<p>看官们，您认为，这个正则还有哪些没注意到的点？能否正确无误的匹配到朋友丁所需要的内容？您有疑问吗？</p>
<p>PS：以上正则，均为PCRE引擎。。其中，PHP代码的正则递归(迭代)部分，仅限于支持递归正则的引擎代码适用。。<br />
感谢<a href="http://iregex.org/">rex老大</a>指点关于(?!)零宽断言非匹配的特性后接匹配规则可能无效的问题。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cnxct.com/cfc4n%e5%b0%8f%e8%af%95%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

