regex搞出了stack overflow求破

109 views
Skip to first unread message

赵一帆

unread,
Jul 25, 2014, 12:33:18 PM7/25/14
to cs40...@googlegroups.com
 Error running child : java.lang.StackOverflowError

这是regex   Pattern.compile("<title>(.+)</title>");

跑small wiki的时候完全正常,wiki的时候180个map有一个map跪在stack overflow上了

Han Jiang

unread,
Jul 25, 2014, 9:03:29 PM7/25/14
to cs402pku
这个pattern应该会匹配出一个大字符串的,java的regex compiler是递归写的,所以碰到大字符串会报错。

比如<title>hello</title>blalabla<title>world</title>

这个pattern其实会匹配整个字符串的。


--
You received this message because you are subscribed to the Google Groups "cs402pku" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cs402pku+u...@googlegroups.com.
To post to this group, send email to cs40...@googlegroups.com.
Visit this group at http://groups.google.com/group/cs402pku.
For more options, visit https://groups.google.com/d/optout.



--
Han Jiang

Team of Search Engine and Web Mining,
School of Electronic Engineering and Computer Science
,
Peking University, China

赵一帆

unread,
Jul 25, 2014, 10:22:21 PM7/25/14
to cs40...@googlegroups.com
果然取个子字串就好了。。。
不过我把那一行grep出来没看到有另外的<title> or </title>,倒是全是乱码,看到了CJK估计是中文页,不至于匹配到大字串啊

在 2014年7月26日星期六UTC+8上午9时03分29秒,Han Jiang写道:

Han Jiang

unread,
Jul 25, 2014, 10:33:27 PM7/25/14
to cs402pku
按行grep确实不会,但你在mapper中有先把字符串切成行再match吗?

赵一帆

unread,
Jul 26, 2014, 2:44:03 AM7/26/14
to cs40...@googlegroups.com
用的默认的TextInputFormat,已经是一行了吧

在 2014年7月26日星期六UTC+8上午10时33分27秒,Han Jiang写道:

xiongh...@gmail.com

unread,
Jul 28, 2014, 8:49:57 AM7/28/14
to cs40...@googlegroups.com
有可能是贪婪模式引起了太多次回溯,可以试试懒惰模式:Pattern.compile("<title>(.+?)</title>");。
贪婪模式和懒惰模式的区别可以百度“java 正则表达式 greedy”,http://blog.csdn.net/lovingprince/article/details/8813501
一般来说,对于表示字符集合的“.”和“[^a]”之类,应避够在其上使用贪婪型重复运算符* + ?,而应使用懒惰的*?  +?  ??。

在 2014年7月26日星期六UTC+8上午12时33分18秒,赵一帆写道:

赵一帆

unread,
Jul 28, 2014, 9:02:30 AM7/28/14
to cs40...@googlegroups.com
thx
之前不知道贪婪和懒惰,昨天好好学了一下regex,觉得应该是这个问题

在 2014年7月28日星期一UTC+8下午8时49分57秒,xiongh...@gmail.com写道:

杨博文

unread,
Aug 6, 2014, 9:49:42 PM8/6/14
to cs40...@googlegroups.com
改成<title>.+?</title>吧

在 2014年7月28日星期一UTC+8下午9时02分30秒,赵一帆写道:
Reply all
Reply to author
Forward
0 new messages