<?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>我爱正则表达式 &#187; string</title>
	<atom:link href="http://iregex.org/blog/tag/string/feed" rel="self" type="application/rss+xml" />
	<link>http://iregex.org</link>
	<description>原创、翻译、转载关于正则表达式的文章</description>
	<lastBuildDate>Sun, 27 Jun 2010 04:20:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/><atom:link rel="hub" href="http://www.feedsky.com/api/RPC2"/><atom:link rel="hub" href="http://blogsearch.google.com/ping/RPC2"/><atom:link rel="hub" href="http://blog.yodao.com/ping/RPC2"/><atom:link rel="hub" href="http://www.feedsky.com/api/RPC2"/><atom:link rel="hub" href="http://www.xianguo.com/xmlrpc/ping.php"/><atom:link rel="hub" href="http://www.zhuaxia.com/rpc/server.php"/><atom:link rel="hub" href="http://rpc.technorati.com/rpc/ping"/><atom:link rel="hub" href="http://rpc.pingomatic.com/"/>	
<!-- Start Of Script Generated By WP-PostViews Plus -->
<script type='text/javascript' src='http://iregex.org/wp-includes/js/jquery/jquery.js?ver=1.4.2'></script>
<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */
</script>
<!-- End Of Script Generated By WP-PostViews Plus -->
	<item>
		<title>正则表达式匹配ABCD随机字串</title>
		<link>http://iregex.org/blog/regex-against-abcd.html</link>
		<comments>http://iregex.org/blog/regex-against-abcd.html#comments</comments>
		<pubDate>Fri, 05 Dec 2008 02:03:21 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[问答]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=37</guid>
		<description><![CDATA[前一段时间在chinaunix论坛上发现这样一则问题： 要求abcd四个字母连续，但每个字母有且仅出现一次，并且顺序可以不固定，也就是要匹配abcd adbc bcda等等情况 我说一下自己的解决思路。 第一... ]]></description>
			<content:encoded><![CDATA[<p>前一段时间在<a href="http://bbs.chinaunix.net/thread-1300693-1-1.html" target="_blank" rel="nofollow">chinaunix论坛</a>上发现这样一则问题：</p>
<blockquote style="border-left:2px solid #DDDDDD; margin:15px 30px 0 10px; padding-left:20px;"><p>要求abcd四个字母连续，但每个字母有且仅出现一次，并且顺序可以不固定，也就是要匹配<tt class="string">abcd adbc bcda</tt>等等情况</p></blockquote>
<p>我说一下自己的解决思路。</p>
<p><span id="more-37"></span></p>
<ul>
<li>第一个字母是<tt class="string">abcd</tt>中的任意一个，因此正则式<tt class="regex">[abcd]</tt>即符合要求。第一个字母最简单。</li>
<li>第二个字母是<tt class="string">abcd</tt>中的任意一个，但是不能使用第一步中已经使用过的字母。因此回退一步，将第一个字母括起来，以备引用<tt class="regex">([abcd])</tt>。不能使用<tt class="regex">\1</tt>，亦即<tt class="regex">(?!\1)</tt>，它表示当前位置不出现<tt class="regex">\1</tt>，即第一个字母。至此，正则式如下：<tt class="regex">([abcd])(?!\1)([abcd])</tt>。</li>
<li>第三个字母与第二个字母的原理相同，但是它既不能使用第一个字母，也不能使用第二个字母。正则式如下：<tt class="regex">([abcd])(?!\1)([abcd])(?!\1|\2)([abcd])</tt>。</li>
<li>第四步无需多言，完整正则式如下：<tt class="regex">([abcd])(?!\1)([abcd])(?!\1|\2)([abcd])(?!\1|\2|\3)([abcd])</tt>。</li>
</ul>
<p>回顾刚才的正则式，对于新手来说，或许<tt class="regex">(?!\1)</tt>是个不容易理解的地方。我就对此作用简单介绍。<tt class="regex">(?!...)</tt>的形式称为<a href="http://www.regular-expressions.info/lookaround.html">look around</a>，与<tt class="regex">^</tt>或<tt class="regex">$</tt>一样，它<b>只匹配位置</b>，<b>而不实际消耗字符</b>。如果想匹配“某字串之后不出现某字串”这种情况，那么<tt class="regex">(?!)</tt>negative lookahead是不可缺少的。例如，如果想匹配<tt class="regex">q</tt>之后不出现<tt class="regex">u</tt>的字串，就可以使用<tt class="regex">q(?!u)</tt>，它表示<tt class="regex">q</tt>后边不出现<tt class="regex">u</tt>。它可以匹配<tt class="match">"Iraq"</tt>,<tt class="match">"icq"</tt>,<tt class="match">"qq"</tt>等字串。</p>
<p>如果您读到这里还没有头痛，请思考它与<tt class="regex">q[^u]</tt>的区别。</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/regex-against-abcd.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
