<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	>
<channel>
	<title>Comments on: 尋找 O(log(n)) 的 regex associative array</title>
	<atom:link href="http://william.cswiz.org/blog/archives/2008-05-10/regex-associative-array/feed/" rel="self" type="application/rss+xml" />
	<link>http://william.cswiz.org/blog/archives/2008-05-10/regex-associative-array/</link>
	<description>readings, ideas, feelings, photos, etc. by William Yeh</description>
	<pubDate>Fri, 09 Jan 2009 23:18:38 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.8-bleeding-edge</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: william</title>
		<link>http://william.cswiz.org/blog/archives/2008-05-10/regex-associative-array/comment-page-1/#comment-8730</link>
		<dc:creator>william</dc:creator>
		<pubDate>Tue, 13 May 2008 03:11:43 +0000</pubDate>
		<guid isPermaLink="false">http://william.cswiz.org/blog/?p=259#comment-8730</guid>
		<description>看起來這問題比我當初設想得更複雜，而且還需要分成兩個子問題：

&lt;ol&gt;
  &lt;li&gt;個別 regex 層次：妥善處理 submatch extraction、backreference。&lt;/li&gt;
  &lt;li&gt;&lt;i&gt;n&lt;/i&gt; 個 regex 層次。&lt;/li&gt;
&lt;/ol&gt;

以第一個層次而言，我贊成 Russ Cox 的&lt;a href="http://swtch.com/~rsc/regexp/regexp1.html" title="Regular Expression Matching Can Be Simple And Fast (but is slow in Java, Perl, PHP, Python, Ruby, …)"&gt;說法&lt;/a&gt;：

&lt;blockquote&gt;
[...] it would be reasonable to use Thompson’s NFA simulation for most regular expressions, and only bring out backtracking when it is needed. A particularly clever implementation could combine the two, resorting to backtracking only to accommodate the backreferences.
&lt;/blockquote&gt;

不過這問題相當複雜，Cox 文章之後引發的討論也相當多。

至於第二個層次，如果這個 regex associative array 資料結構只需 batch insert 及 query，不需 update 及 delete，我想短期之內最快的實現方法，還是用 &lt;a href="http://dinosaur.compilertools.net/"&gt;lex&lt;/a&gt; 之類的工具產生一個 compile-time 的 skeleton。

不知道 sendmail、spam filter 之類的系統是如何做的？</description>
		<content:encoded><![CDATA[<p>看起來這問題比我當初設想得更複雜，而且還需要分成兩個子問題：</p>
<ol>
<li>個別 regex 層次：妥善處理 submatch extraction、backreference。</li>
<li><i>n</i> 個 regex 層次。</li>
</ol>
<p>以第一個層次而言，我贊成 Russ Cox 的<a href="http://swtch.com/~rsc/regexp/regexp1.html" title="Regular Expression Matching Can Be Simple And Fast (but is slow in Java, Perl, PHP, Python, Ruby, …)">說法</a>：</p>
<blockquote><p>
[...] it would be reasonable to use Thompson’s NFA simulation for most regular expressions, and only bring out backtracking when it is needed. A particularly clever implementation could combine the two, resorting to backtracking only to accommodate the backreferences.
</p></blockquote>
<p>不過這問題相當複雜，Cox 文章之後引發的討論也相當多。</p>
<p>至於第二個層次，如果這個 regex associative array 資料結構只需 batch insert 及 query，不需 update 及 delete，我想短期之內最快的實現方法，還是用 <a href="http://dinosaur.compilertools.net/">lex</a> 之類的工具產生一個 compile-time 的 skeleton。</p>
<p>不知道 sendmail、spam filter 之類的系統是如何做的？</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 路人</title>
		<link>http://william.cswiz.org/blog/archives/2008-05-10/regex-associative-array/comment-page-1/#comment-8712</link>
		<dc:creator>路人</dc:creator>
		<pubDate>Mon, 12 May 2008 06:16:26 +0000</pubDate>
		<guid isPermaLink="false">http://william.cswiz.org/blog/?p=259#comment-8712</guid>
		<description>因為 perl regex 有 back reference，所以它並不等同於 formal language 的 regular expression，也就不等同於 regular language 或 FA。

若限定只接受 FA 可處理的 perl regex，是可以造出一個 FA 來判斷 input string 是否被 &lt;i&gt;n&lt;/i&gt; regex or 起來的 FA accept，complexity = &lt;i&gt;O&lt;/i&gt;(strlen(input string))。
若不只是判斷 match，還要在 &lt;i&gt;n&lt;/i&gt; 個 regex 裡，找到一個能與該字串吻合的 regex，也許也可以做到，不過還要想想就是。

至於 &lt;i&gt;O&lt;/i&gt;(log(&lt;i&gt;n&lt;/i&gt;)) 感覺是不大可能，要能達到 log，似乎需要一次能讓 search space 減為 1/&lt;i&gt;k&lt;/i&gt; 的方法，目前能想到滿足這條件的通常要有 ordered 或 monotonic function 這樣的特性 (想想 binary search or search tree)。FA 似乎沒有這種特性?

順道一提，我猜如果問題是：在 &lt;i&gt;n&lt;/i&gt; 個 regex 裡，找所有能與該字串吻合的 regex，complexity 應該會是 &lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)。
想法和上面差不多，當答案可能有 &lt;i&gt;n&lt;/i&gt; 個時，似乎不存在一個比 &lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;) 還好的描述法來描述答案。若是有 ordered 特性的話，不管答案有幾個，都可以用二個值：上下界來描述答案 (C++ STL 的 &lt;code&gt;equal_range&lt;/code&gt; 就是如此)，但 in general regex 似乎沒有更好的改進描述法的特性?</description>
		<content:encoded><![CDATA[<p>因為 perl regex 有 back reference，所以它並不等同於 formal language 的 regular expression，也就不等同於 regular language 或 FA。</p>
<p>若限定只接受 FA 可處理的 perl regex，是可以造出一個 FA 來判斷 input string 是否被 <i>n</i> regex or 起來的 FA accept，complexity = <i>O</i>(strlen(input string))。<br />
若不只是判斷 match，還要在 <i>n</i> 個 regex 裡，找到一個能與該字串吻合的 regex，也許也可以做到，不過還要想想就是。</p>
<p>至於 <i>O</i>(log(<i>n</i>)) 感覺是不大可能，要能達到 log，似乎需要一次能讓 search space 減為 1/<i>k</i> 的方法，目前能想到滿足這條件的通常要有 ordered 或 monotonic function 這樣的特性 (想想 binary search or search tree)。FA 似乎沒有這種特性?</p>
<p>順道一提，我猜如果問題是：在 <i>n</i> 個 regex 裡，找所有能與該字串吻合的 regex，complexity 應該會是 <i>O</i>(<i>n</i>)。<br />
想法和上面差不多，當答案可能有 <i>n</i> 個時，似乎不存在一個比 <i>O</i>(<i>n</i>) 還好的描述法來描述答案。若是有 ordered 特性的話，不管答案有幾個，都可以用二個值：上下界來描述答案 (C++ STL 的 <code>equal_range</code> 就是如此)，但 in general regex 似乎沒有更好的改進描述法的特性?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: william</title>
		<link>http://william.cswiz.org/blog/archives/2008-05-10/regex-associative-array/comment-page-1/#comment-8707</link>
		<dc:creator>william</dc:creator>
		<pubDate>Sun, 11 May 2008 12:33:24 +0000</pubDate>
		<guid isPermaLink="false">http://william.cswiz.org/blog/?p=259#comment-8707</guid>
		<description>@小影：你列的論文，就是單中杰提出的那篇。

@jeffhung：你提的方法，就是 Adblock Plus 用的方法，差別只在於 “shortcut” 的取法。</description>
		<content:encoded><![CDATA[<p>@小影：你列的論文，就是單中杰提出的那篇。</p>
<p>@jeffhung：你提的方法，就是 Adblock Plus 用的方法，差別只在於 “shortcut” 的取法。</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jeffhung</title>
		<link>http://william.cswiz.org/blog/archives/2008-05-10/regex-associative-array/comment-page-1/#comment-8705</link>
		<dc:creator>jeffhung</dc:creator>
		<pubDate>Sun, 11 May 2008 11:15:29 +0000</pubDate>
		<guid isPermaLink="false">http://william.cswiz.org/blog/?p=259#comment-8705</guid>
		<description>我之前的碰到這個問題的作法很簡單，就是先從 regex 裡抽出 keyword：透過簡單地對 regex 分析，可以抽出「一定要有的 sub string」出來。實際在用時，就先用這個 keyword 過濾，然後才丟給 regex，這樣就可以大量減少 regex 比對的次數。</description>
		<content:encoded><![CDATA[<p>我之前的碰到這個問題的作法很簡單，就是先從 regex 裡抽出 keyword：透過簡單地對 regex 分析，可以抽出「一定要有的 sub string」出來。實際在用時，就先用這個 keyword 過濾，然後才丟給 regex，這樣就可以大量減少 regex 比對的次數。</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 小影</title>
		<link>http://william.cswiz.org/blog/archives/2008-05-10/regex-associative-array/comment-page-1/#comment-8704</link>
		<dc:creator>小影</dc:creator>
		<pubDate>Sun, 11 May 2008 05:01:08 +0000</pubDate>
		<guid isPermaLink="false">http://william.cswiz.org/blog/?p=259#comment-8704</guid>
		<description>這篇論文有講過...

Regular Expression Matching Can Be Simple And Fast
(but is slow in Java, Perl, PHP, Python, Ruby, ...)

http://swtch.com/~rsc/regexp/regexp1.html</description>
		<content:encoded><![CDATA[<p>這篇論文有講過&#8230;</p>
<p>Regular Expression Matching Can Be Simple And Fast<br />
(but is slow in Java, Perl, PHP, Python, Ruby, &#8230;)</p>
<p><a href="http://swtch.com/~rsc/regexp/regexp1.html" rel="nofollow"></a><a href='http://swtch.com/~rsc/regexp/regexp1.html'>http://swtch.com/~rsc/regexp/regexp1.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: william</title>
		<link>http://william.cswiz.org/blog/archives/2008-05-10/regex-associative-array/comment-page-1/#comment-8702</link>
		<dc:creator>william</dc:creator>
		<pubDate>Sun, 11 May 2008 01:44:20 +0000</pubDate>
		<guid isPermaLink="false">http://william.cswiz.org/blog/?p=259#comment-8702</guid>
		<description>@Thinker：謝謝！的確是我想探討的事。

@單中杰：Perl 的 grep 的確與 Unix 的 grep 不同，但你所提供的文獻卻令我驚訝。

兩個問題需要再檢討：

&lt;ol&gt;
  &lt;li&gt;Perl6 是否有改進？&lt;/li&gt;
  &lt;li&gt;Java 的 &lt;a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html#compile(java.lang.String)"&gt;Pattern.compile()&lt;/a&gt; 是否真的有在內部轉換成 FA？&lt;/li&gt;
&lt;/ol&gt;</description>
		<content:encoded><![CDATA[<p>@Thinker：謝謝！的確是我想探討的事。</p>
<p>@單中杰：Perl 的 grep 的確與 Unix 的 grep 不同，但你所提供的文獻卻令我驚訝。</p>
<p>兩個問題需要再檢討：</p>
<ol>
<li>Perl6 是否有改進？</li>
<li>Java 的 <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html#compile(java.lang.String)">Pattern.compile()</a> 是否真的有在內部轉換成 FA？</li>
</ol>
]]></content:encoded>
	</item>
	<item>
		<title>By: 單中杰</title>
		<link>http://william.cswiz.org/blog/archives/2008-05-10/regex-associative-array/comment-page-1/#comment-8700</link>
		<dc:creator>單中杰</dc:creator>
		<pubDate>Sat, 10 May 2008 23:16:11 +0000</pubDate>
		<guid isPermaLink="false">http://william.cswiz.org/blog/?p=259#comment-8700</guid>
		<description>Perl 的 grep 與 Unix 的 grep 不同，與 regex 沒有直接關係，只是在一個 list 中選取符合任意條件的元素罷了，相當於許多其他語言稱之為 filter 的功能。

print join(&#34;,&#34;, grep { 123 % $_ == 0 } 1..123), &#34;\n&#34;;
1,3,41,123

所以應該沒有什麼 &lt;i&gt;O&lt;/i&gt;(log(&lt;i&gt;n&lt;/i&gt;)) 的效率可言。更何況 &lt;a href="http://swtch.com/~rsc/regexp/regexp1.html"&gt;Perl 根本沒有做到 regex 的基本算法&lt;/a&gt;，所以就算是拿單一字串配對單一 regex 都可能花費 exponential time。

要是 Perl 有做到 regex 的基本算法的話，就可以於 runtime 把許多要配對的 regex 組合成一個 regex 再進行配對，以達成優於 &lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;) 的效率。</description>
		<content:encoded><![CDATA[<p>Perl 的 grep 與 Unix 的 grep 不同，與 regex 沒有直接關係，只是在一個 list 中選取符合任意條件的元素罷了，相當於許多其他語言稱之為 filter 的功能。</p>
<p>print join(&quot;,&quot;, grep { 123 % $_ == 0 } 1..123), &quot;\n&quot;;<br />
1,3,41,123</p>
<p>所以應該沒有什麼 <i>O</i>(log(<i>n</i>)) 的效率可言。更何況 <a href="http://swtch.com/~rsc/regexp/regexp1.html">Perl 根本沒有做到 regex 的基本算法</a>，所以就算是拿單一字串配對單一 regex 都可能花費 exponential time。</p>
<p>要是 Perl 有做到 regex 的基本算法的話，就可以於 runtime 把許多要配對的 regex 組合成一個 regex 再進行配對，以達成優於 <i>O</i>(<i>n</i>) 的效率。</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thinker</title>
		<link>http://william.cswiz.org/blog/archives/2008-05-10/regex-associative-array/comment-page-1/#comment-8697</link>
		<dc:creator>Thinker</dc:creator>
		<pubDate>Sat, 10 May 2008 11:02:35 +0000</pubDate>
		<guid isPermaLink="false">http://william.cswiz.org/blog/?p=259#comment-8697</guid>
		<description>FYI,
http://heaven.branda.to/~thinker/GinGin_CGI.py/show_id_doc/320</description>
		<content:encoded><![CDATA[<p>FYI,<br />
<a href="http://heaven.branda.to/~thinker/GinGin_CGI.py/show_id_doc/320" rel="nofollow"></a><a href='http://heaven.branda.to/~thinker/GinGin_CGI.py/show_id_doc/320'>http://heaven.branda.to/~thinker/GinGin_CGI.py/show_id_doc/320</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
