<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post/5483/</link>
<title><![CDATA[[正则入门]请教一下正则表达式与通配符的区别 ，贪婪模式和最少模式的示例]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Fri, 10 Aug 2012 14:49:29 +0000</pubDate> 
<guid>http://www.jackxiang.com/post/5483/</guid> 
<description>
<![CDATA[ 
	贪婪模式和最少模式:<br/><textarea name="code" class="php" rows="15" cols="100">
 &lt;?php
 $str = &quot;&lt;h2&gt;hello&lt;/h2&gt;&lt;h2&gt;world&lt;/h2&gt;&quot;;
 $preg = &quot;/&lt;h2&gt;(.*)&lt;&#92;/h2&gt;/is&quot;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 preg_match($preg,$str,$arr);
 print_r($arr);
</textarea><br/><br/>*贪婪模式：<br/>#php ee.php<br/>Array<br/>(<br/>&nbsp;&nbsp;&nbsp;&nbsp;[0] =&gt; &lt;h2&gt;hello&lt;/h2&gt;&lt;h2&gt;world&lt;/h2&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;[1] =&gt; hello&lt;/h2&gt;&lt;h2&gt;world<br/>)<br/><br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$str = &quot;&lt;h2&gt;hello&lt;/h2&gt;&lt;h2&gt;world&lt;/h2&gt;&quot;;
$preg = &quot;/&lt;h2&gt;(.*?)&lt;&#92;/h2&gt;/is&quot;;
preg_match($preg,$str,$arr);
print_r($arr);
</textarea><br/>?问号的贪婪模式：<br/>#php ee.php <br/>Array<br/>(<br/>&nbsp;&nbsp;&nbsp;&nbsp;[0] =&gt; &lt;h2&gt;hello&lt;/h2&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;[1] =&gt; hello<br/>)<br/><br/><br/><br/><br/>PHP的交互模式实践如下：<br/><textarea name="code" class="php" rows="15" cols="100">
php &gt; preg_match(&quot;#o.*k#&quot;,&#039;oabck&#039;,$result);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
php &gt; var_dump($result);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
array(1) &#123;
&nbsp;&nbsp;[0]=&gt;
&nbsp;&nbsp;string(5) &quot;oabck&quot;
&#125;
php &gt; preg_match(&quot;#o.*k#&quot;,&#039;oabck123ok&#039;,$result);&nbsp;&nbsp; 
php &gt; var_dump($result);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
array(1) &#123;
&nbsp;&nbsp;[0]=&gt;
&nbsp;&nbsp;string(10) &quot;oabck123ok&quot;
&#125;
php &gt; preg_match_all(&quot;#o.*?k#&quot;,&#039;oabck123ok&#039;,$result);&nbsp;&nbsp;&nbsp;&nbsp; 
php &gt; var_dump($result);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
array(1) &#123;
&nbsp;&nbsp;[0]=&gt;
&nbsp;&nbsp;array(2) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;[0]=&gt;
&nbsp;&nbsp;&nbsp;&nbsp;string(5) &quot;oabck&quot;
&nbsp;&nbsp;&nbsp;&nbsp;[1]=&gt;
&nbsp;&nbsp;&nbsp;&nbsp;string(2) &quot;ok&quot;
&nbsp;&nbsp;&#125;
&#125;
</textarea><br/><br/>本人小新手，最近在看vbird的私房菜，在看chapter11和12时有个小疑问：<br/>在两章中分别介绍了通配符和正则表达式的用法，两者在表达上存在差别，比如通配符用（*）表示任意多个字符，正则表达式则用（.）等等，<br/>那么请问大家，两者使用的场合是怎么区别的呢，是根据命令的不同还是正则表达式只用于字符串的搜索匹配呢。<br/><br/><br/>通配符是用来通配的<br/>正则表达式是用来匹配字符串的<br/>在文本过滤工具里，都是用正则表达式，比如像awk，sed，等，是针对文件的内容的。<br/>而通配符多用在文件名上，比如查找find，ls，cp，等等。<br/>这是我个人的一些看法，不知道对不对。<br/><br/>感觉通配符是系统level的<br/><br/>正则表达式需要相关工具的支持: egrep, awk, vi, perl。。<br/><br/> 来源：http://bbs.chinaunix.net/thread-1255137-1-1.html<br/>务必参考：http://blog.csdn.net/whxlovehy/article/details/6052366<br/><br/>如把Html里的脚本给去掉，怎么匹配，正确匹配如下：<br/><textarea name="code" class="php" rows="15" cols="100">preg_replace(&quot; /&lt;script[^&gt;].*?&gt;.*?&lt;/script&gt;/si&quot;, &quot;newinfo&quot;, $script);</textarea><br/>如果没有问号，这样：&lt;script[^&gt;].*&gt;&nbsp;&nbsp;，你会发现这个&gt; 给匹配到最后一个&gt;了，因为是贪婪匹配，所以得加上：? ,后面一个也是一样的，<br/>这里面有一个.*? ,这个?就是最小模式，如果说没有这个?，那么？让它去匹配：<br/>&lt;script type=&quot;text/javascript&quot;&gt;&lt;/script&gt;&lt;/script&gt; ，就会把最后的一个重复的&lt;/script&gt;匹配上，因为是最大长度模式匹配，（第二个用的贪婪匹配），为此，这个?号作用可大了。<br/>上面表达式中，&quot;.?是非贪婪模式匹配。也就是说，只要在一个字符后面跟上限定个数的特殊字符，匹配就是非贪婪模式了。 <br/><br/>最终Ok的匹配且成功的结果：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
function ReplaceScript(&amp;$txt)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$str=&quot;&#039;&lt;script[^&gt;]*?&gt;.*?&lt;/script&gt;&#039;si&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print $txt=preg_replace(&quot;$str&quot;,&quot;&quot;,$txt);
&#125;
$txt=&quot;&lt;html&gt;&lt;head&gt;&lt;script language=&#039;javascript&#039;&gt; var preFrameW = &#039;160,*&#039;; &lt;/script&gt;.&lt;/head&gt;&lt;css&gt;&lt;/css&gt;&lt;/html&gt;&quot;;
ReplaceScript($txt);
echo $txt;
?&gt;
</textarea><br/>结果如下：<br/>&lt;html&gt;&lt;head&gt;.&lt;/head&gt;&lt;css&gt;&lt;/css&gt;&lt;/html&gt;<br/>用RegexBuddy测试好后，Copy，Copy regex as，copy regex as &quot;//&quot;&nbsp;&nbsp;preg string：&nbsp;&nbsp;&#039;%&lt;script[^&gt;].*?&gt;.*?&lt;/script&gt;%&#039; 后修改为：<br/><br/>$str=&#039;%&lt;script[^&gt;].*?&gt;.*?&lt;/script&gt;%&#039;;&nbsp;&nbsp;经测试，也是Ok的，这儿这个小工具帮我们规避掉下面文章所说的问题了。但它默认还是以//，这儿特殊它给我们规避，变为：%罢了，呵呵。<br/>------------------------------------------------来自：http://hi.baidu.com/yglht/item/632a3dabf8a1cc9a15107300&nbsp;&nbsp;------------------------<br/>遇到的问题：PHP正则preg_replace（）函数 Unknown modifier &#039;c&#039;问题<br/>今天想用PHP正则去掉字符串中的JavaScript脚本，于是用了preg_replace()函数尝试。<br/>PHP代码如下：<br/>&lt;?php<br/>function ReplaceScript(&amp;$txt)<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$str=&quot;/&lt;script[^&gt;]*?&gt;.*?&lt;/script&gt;/si&quot;:<br/>&nbsp;&nbsp;&nbsp;&nbsp; //[^&gt;]*?表示匹配任意次除&quot;&gt;&quot;以外任意字符，但尽可能少重复<br/>//.*? 表示匹配任意次任意字符<br/>&nbsp;&nbsp;&nbsp;&nbsp; preg_replace(&quot;$str&quot;,&quot;&quot;,$txt);<br/>&#125;<br/>$txt=&quot;&lt;script language=&#039;javascript&#039;&gt; var preFrameW = &#039;160,*&#039;; &lt;/script&gt;&quot;;<br/>$txt.=&quot;hello world&quot;;<br/>echo $txt;<br/>ReplaceScript($txt);<br/>echo&nbsp;&nbsp; $txt;<br/>?&gt;<br/>预想结果为 连续两次输出 hello world，且查看html源代码时只有第一次会输出JavaScript代码。<br/>但结果如下：<br/>hello world<br/>Warning: preg_replace() [function.preg-replace]: Unknown modifier &#039;c&#039; in ~~~<br/><br/>hello world<br/>除了这个问题很纠结外，打开html看其源代码，依然有两次JavaScript代码输出。说明JavaScript代码并没有被取代为空格。<br/>后面经过几次修改函数中的$str变量，依次出现了 Warning: preg_replace() [function.preg-replace]: Unknown modifier &#039;/&#039; in ~~~ 等稀奇八怪的提示。JavaScript代码也依旧没有被替换<br/>郁闷啊，于是又改回到最初的函数，找出PHP5帮助文档，查看preg_replace() 说明。修改ReplaceScript（）函数为<br/>function ReplaceScript(&amp;$txt)<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$str=&quot;/&lt;script[^&gt;]*?&gt;.*?&lt;/script&gt;/si&quot;:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print preg_replace(&quot;$str&quot;,&quot;&quot;,$txt);<br/>&#125;<br/>结果是输出了三次JavaScript代码，看来还是$str问题。于是乎，奈着性子看了大量的例子。觉得没有错啊，一般匹配字符都是这么写，开始结尾均有个&quot; / &quot;。但是这个时候，其实我并不知道这个 /&nbsp;&nbsp; 有什么作用。呵呵，还是觉得没有错。只好百度之，在百度里看到了例外，有人居然不是用 &quot; / &quot;放在开头结尾，而是用 &quot; &#039; &quot;字符代替。莫非PHP默认这个字符 / 的用处仅仅就是帮助PHP界定匹配字符串的区间，在这个区间外就不匹配。而实际只要用另外的一个字符分别放在匹配字符串的开头和结尾也可以达到同样的目的。于是果断把$str换为&nbsp;&nbsp;&nbsp;&nbsp; $str=&quot;&#039; &lt;script[^&gt;]*?&gt;.*?&lt;/script&gt;&#039; &quot;: 编译again 。<br/>呵呵，万能的神，Warning: preg_replace() [function.preg-replace]: Unknown modifier &#039;c&#039; in ~~~这个问题终于没有了。查看生成的htm源代码，输出了三次helloworld，输出了两次JavaScript(用print函数输出的没有)。看来是识别成功了,只是$txt没有替换。于是再次修改<br/>function ReplaceScript(&amp;$txt)<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$str=&quot;&#039; &lt;script[^&gt;]*?&gt;.*?&lt;/script&gt;&#039;si &quot;:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print $txt=preg_replace(&quot;$str&quot;,&quot;&quot;,$txt);<br/>&#125;<br/>查看结果，这次是输出了三次helloworld，只在第一次输出helloworld前输出了JavaScript，后面两次都被替换掉了。呵呵，OK，问题搞定。<br/>总结，我犯的错误在&nbsp;&nbsp;&nbsp;&nbsp; $str=&quot;/&lt;script[^&gt;]*?&gt;.*?&lt;/script&gt;/si&quot;:&nbsp;&nbsp; 这里面的&lt;/script&gt;已经有一个&quot; / &quot;了，所以开头的 &quot; / &quot;就和这个 &quot; / &quot;形成了匹配区间，而把这个&quot; / &quot;之后的&quot; script&gt;/ &quot; 当成了模式修正符。但实际模式修正符只有i，m，s，x，e等少数几个，而&quot;script&gt;/ &quot;里面的c，r，p，&gt; ，/ 都不是模式修正符，所以才出了Unknown modifier &#039;c‘&nbsp;&nbsp; Unknown modifier &#039;/ ’ 等问题 (没有出现 Unknown modifier &#039;s‘ ，是因为 s 是模式匹配符啊）。<br/>模式修正符号可以参考学习：http://hi.baidu.com/longniao/blog/item/b2af346d658ea9f3431694db.html
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post/5483/#blogcomment63799</link>
<title><![CDATA[[评论] [正则入门]请教一下正则表达式与通配符的区别 ，贪婪模式和最少模式的示例]]></title> 
<author>hxngb5mf &lt;vtfw476@domozmail.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Fri, 31 Aug 2012 17:16:33 +0000</pubDate> 
<guid>http://www.jackxiang.com/post/5483/#blogcomment63799</guid> 
<description>
<![CDATA[ 
	謝謝分享哦。
]]>
</description>
</item>
</channel>
</rss>