简体   繁体   English

Google搜索结果的正则表达式在android中不起作用

[英]regex for google search result not work in android

I try to scrape google search result with this code 我尝试使用此代码抓取Google搜索结果

 Pattern p = Pattern.compile("<h3.*?><a.*?href=\"(.*?)\".*?</h3>");
        Matcher m = p.matcher(html);

in android studio its work, but in android devices takes too much time ... 在android studio中工作,但是在android设备中花费太多时间...

how can i fix that ? 我该如何解决?

This is due to your regex causing catastrophic backtracking. 这是由于您的正则表达式导致灾难性的回溯。

Adding the first .*? 添加第一个.*? to an atomic group will help. 加入原子团会有所帮助。 An atomic group prevents backtracking in that particular group. 原子组可防止该特定组中的回溯。

(?><h3.*?>)<a.*?href=\"(.*?)\".*?<\/h3>

See https://regex101.com/r/hl2ARK/1 and try removing the first group to replicate your original regex. 请参阅https://regex101.com/r/hl2ARK/1,然后尝试删除第一个组以复制原始正则表达式。

Pretty much everything you need to know about catasrophic backtracking can be found here http://www.regular-expressions.info/catastrophic.html 您可以在以下网址找到有关催化式回溯的几乎所有信息:http://www.regular-expressions.info/catastrophic.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM