gpt4 book ai didi

java - 清醒工作 : Java Regular Expressions & GNU Regular Expressions

转载 作者:行者123 更新时间:2023-11-29 09:18:09 32 4
gpt4 key购买 nike

我正在尝试创建正则表达式,以便我可以使用 LucidWorks 对我网站上的某些 URL 进行爬网和索引。

示例网址:http://www.example.com/reviews/assassins-creed-revelations/24475 /评论/示例网址:http://www.example.com/reviews/super-mario-3d-land/64303 /评论/

基本上,我希望 LucidWorks 搜索我的整个站点并仅索引在 URL 末尾具有/reviews/的 URL。

谁能帮我构造一个表达式来做到这一点? :)

更新:

网址:http://www.example.com/

包含路径://*/reviews/*

这种方法可行,但它只会抓取第一页,不会转到下一页有更多评论(1、2、3 等)。

如果我还添加:///reviews/.*

我得到了很多我不想要的索引页面,例如 http://www.example.com/?page=2

最佳答案

Check with this function
public boolean canAcceptURL(String url,String endsWith){
boolean canAccept = false;
String regex = "";
try{
if(endsWith.equals("")){
endsWith = "/reviews/";
}
regex = "[\\x20-\\x7E]*"+endsWith+"$";//Check the url string u passed ends with the endString you hav passed.If end string is null it will take the default value.
canAccept = url.matches(regex);
}catch (PatternSyntaxException pe) {
pe.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("String matches : "+canAccept);
return canAccept;
}

Sample out put :
calling function : canAcceptURL("http://www.example.com/reviews/super-mario-3d-land/64303/reviews/","/reviews/");
String matches : true

if you want to get the url contains *'/reviews/'* just change the regex string to

String regex = "[\\x20-\\x7E]*/reviews/[\\x20-\\x7E]*"; // this will accept a string with white space and special character.

关于java - 清醒工作 : Java Regular Expressions & GNU Regular Expressions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8174619/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com