Java 从文本中提取链接
我想从以“q=http”开头和“&”之前的文本中提取链接,即我想得到这两个文本之间的文本,我的输出应该是Java 从文本中提取链接,java,regex,Java,Regex,我想从以“q=http”开头和“&”之前的文本中提取链接,即我想得到这两个文本之间的文本,我的输出应该是 http://ibnlive.in.com/news/noidas-nithari-killings-sc-stays-execution-of-convict-surinder-koli-for-a-week/497153-3-242.html 哪些统计数据来自“http”并在“和”之前结束 我试过的是 Pattern p = Pattern.compile(".
http://ibnlive.in.com/news/noidas-nithari-killings-sc-stays-execution-of-convict-surinder-koli-for-a-week/497153-3-242.html
哪些统计数据来自“http”并在“和”之前结束
我试过的是
Pattern p = Pattern.compile(".*?q=(http:.*?)&");
Matcher m = p.matcher(content);
String pageid = "";
if (m.find())
{
pageid = m.group(1);
}
System.out.println(pageid);
下面是我的text.html文件的内容…
q=http://www.thehindu.com/news/national/supreme-court-execution-of-nithari-killer-surinder-koli/article6390120.ece&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CBQQpwI&usg=AFQjCNFDcbVK87iUjDwI21jbIZUg0aU8gQ"><img class="th" height="100" src="http://t1.gstatic.com/images?q=tbn:ANd9GcSUUkUw1JxXWJQj2SCQr3XxoIcY5OpWLzDgHIqvLzDgmrfntT9nRi99Lvuuheh05L50VDbs-pY" width="100" border="1"><br></a><span class="_pJb _yhd">The Hindu</span></td><td valign="top"><div style="margin-top:5px"><a href="/url?q=http://ibnlive.in.com/news/noidas-nithari-killings-sc-stays-execution-of-convict-surinder-koli-for-a-week/497153-3-242.html&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CBYQqQIwAA&usg=AFQjCNFPPZfQCJQH3vFo1I9Avu-ug8EcSg">Noida's <b>Nithari killings</b>: SC stays execution of convict Surinder Koli for a week</a><div style="padding-top:2px"><cite>IBNLive</cite><span class="f"> - <span class="nobr">1 hour ago</span><span class="nobr"></span></span></div><div class="j" style="margin-top:1px;margin-bottom:4px"><span class="st">New Delhi: The Supreme Court has stayed the execution of Noida's <b>Nithari</b> <b>killings</b> convict Surinder Koli for one week. An official of the apex ...</span></div></div><div style="margin-top:4px"><a href="/url?q=http://www.firstpost.com/india/sc-defers-nithari-killings-convict-surendra-kolis-hanging-week-1701475.html&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CBgQqQIwAQ&usg=AFQjCNGsfNy0HC_rfyMfPSSpU66FmUydIw">SC defers <b>Nithari killings</b> convict Surendra Koli's hanging by a week</a><div style="padding-top:2px"><cite>Firstpost</cite><span class="f"> - <span class="nobr">1 hour ago</span><span class="nobr"></span></span></div></div><div style="margin-top:4px"><a href="/url?q=http://www.hindustantimes.com/india-news/nithari-killer-surinder-koli-to-be-hanged-on-sept-12-jail-authorities/article1-1260116.aspx&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CBoQqQIwAg&usg=AFQjCNEjeDnCXSwCCWtXO87tIhj6athCCA"><b>Nithari</b> case: Surinder Koli to be hanged on Sept 12</a><div style="padding-top:2px"><cite>Hindustan Times</cite><span class="f"> - <span class="nobr">3 days ago</span><span class="nobr"></span></span></div></div></td></tr></table></div></li><li class="g"><h3 class="r"><a href="/url?q=http://en.wikipedia.org/wiki/Noida_serial_murders&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CBwQFjAD&usg=AFQjCNGATKIaCWR1Hl-yqEqXcb1XnXKu9g">Noida serial <b>murders</b> - Wikipedia, the free encyclopedia</a></h3><div class="s"><div class="kv" style="margin-bottom:2px"><cite>en.wikipedia.org/wiki/Noida_serial_<b>murders</b></cite><div class="_nBb"><div style="display:inline" onclick="google.sham(this);" aria-expanded="false" aria-haspopup="true" tabindex="0" data-ved="0CB0Q7B0wAw"><span class="_O0"></span></div><div style="display:none" class="am-dropdown-menu" role="menu" tabindex="-1"><ul><li class="_Ykb"><a class="_Zkb" href="/url?q=http://webcache.googleusercontent.com/search%3Fhl%3Den-IN%26q%3Dcache:ITALXEhw0j8J:http://en.wikipedia.org/wiki/Noida_serial_murders%252Bnithari%2Bkillings%2Bnews%26gbv%3D2%26%26ct%3Dclnk&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CB8QIDAD&usg=AFQjCNFx4v82ZSgfuIZHJmenK1Xv6jxYpw">Cached</a></li><li class="_Ykb"><a class="_Zkb" href="/search?hl=en-IN&gbv=2&q=related:en.wikipedia.org/wiki/Noida_serial_murders+nithari+killings%09news&tbo=1&sa=X&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CCAQHzAD">Similar</a></li></ul></div></div></div><span class="st">The Noida serial <b>murders</b> (also <b>Nithari</b> serial <b>murders</b>, <b>Nithari</b> Kand) took ... The <br>
police then sealed the house and did not allow <b>news</b> media anywhere near the ...</span><br><div class="osl"><a href="/url?q=http://en.wikipedia.org/wiki/Noida_serial_murders%23Events_leading_to_primary_investigation&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CCIQ0gIoADAD&usg=AFQjCNFoFLoEv_CGAkKNe2WFNpQdqTyRag">Events leading to primary ...</a> - <a href="/url?q=http://en.wikipedia.org/wiki/Noida_serial_murders%23Primary_investigation&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CCMQ0gIoATAD&usg=AFQjCNFFSz2pBFdWoUAGkp2sZ_KpAmBoUg">Primary investigation</a> - <a href="/url?q=http://en.wikipedia.org/wiki/Noida_serial_murders%23CBI_investigation&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CCQQ0gIoAjAD&usg=AFQjCNHlovBCPUSSGlExpuZHJxtDHQUZ7A">CBI investigation</a> - <a href="/url?q=http://en.wikipedia.org/wiki/Noida_serial_murders%23Victims&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CCUQ0gIoAzAD&usg=AFQjCNFgkfguy3vUxvh8JmS-ncgfIxOLNA">Victims</a></div></div></li><li class="g"><h3 class="r"><a href="/url?q=http://www.ndtv.com/topic/nithari-killings&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CCcQFjAE&usg=AFQjCNESpfXGZ4DE-uVDo8LvQ42kHVU4Bg"><b>Nithari Killings</b>: Latest <b>News</b>, Photos, Videos on <b>Nithari</b> <b>...</b> - NDTV.com</a></h3><div class="s"><div class="kv" style="margin-bottom:2px"><cite>www.ndtv.com/topic/<b>nithari</b>-<b>killings</b></cite><div class="_nBb"><div style="display:inline" onclick="google.sham(this);" aria-expanded="false" aria-haspopup="true" tabindex="0" data-ved="0CCgQ7B0wBA"><span class="_O0"></span></div><div style="display:none" class="am-dropdown-menu" role="menu" tabindex="-1"><ul><li class="_Ykb"><a class="_Zkb" href="/url?q=http://webcache.googleusercontent.com/search%3Fhl%3Den-IN%26q%3Dcache:a6vXEobpypEJ:http://www.ndtv.com/topic/nithari-killings%252Bnithari%2Bkillings%2Bnews%26gbv%3D2%26%26ct%3Dclnk&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CCoQIDAE&usg=AFQjCNGiRwJ84qtiMaU-6ag_SMMyugi2-g">Cached</a></li><li class="_Ykb"><a class="_Zkb" href="/search?hl=en-IN&gbv=2&q=related:www.ndtv.com/topic/nithari-killings+nithari+killings%09news&tbo=1&sa=X&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CCsQHzAE">Similar</a></li></ul></div></div></div><span class="st">Find <b>Nithari Killings</b> Latest <b>News</b>, Videos & Pictures on <b>Nithari Killings</b> and see <br>
latest updates, <b>news</b>, information from NDTV.COM. Explore more on <b>Nithari</b> ...</span><br></div></li><li class="g"><h3 class="r"><a href="/url?q=http://timesofindia.indiatimes.com/India/SC-stays-execution-of-Nithari-killer-Surinder-Koli/articleshow/41998225.cms&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CC0QFjAF&usg=AFQjCNHAHTcrXEtqeYz_1KPmz-6AxK93RA">SC stays execution of <b>Nithari</b> killer Surinder Koli - The Times of India</a></h3><div class="s"><div class="kv" style="margin-bottom:2px"><cite>timesofindia.indiatimes.com/India/SC...<b>Nithari</b>.../41998225.cms</cite><div class="_nBb"><div style="display:inline" onclick="google.sham(this);" aria-expanded="false" aria-haspopup="true" tabindex="0" data-ved="0CC4Q7B0wBQ"><span class="_O0"></span></div><div style="display:none" class="am-dropdown-menu" role="menu" tabindex="-1"><ul><li class="_Ykb"><a class="_Zkb" href="/url?q=http://webcache.googleusercontent.com/search%3Fhl%3Den-IN%26q%3Dcache:OvKjSrI26NwJ:http://timesofindia.indiatimes.com/India/SC-stays-execution-of-Nithari-killer-Surinder-Koli/articleshow/41998225.cms%252Bnithari%2Bkillings%2Bnews%26gbv%3D2%26%26ct%3Dclnk&sa=U&ei=qTUNVOalHMe2uATE_YGQDw&ved=0CDAQIDAF&usg=AFQjCNHV4qIIoJ8sR79KTPOCIyUhWNwwCg">Cached</a></li></ul></div>
q=http://www.thehindu.com/news/national/supreme-court-execution-of-nithari-killer-surinder-koli/article6390120.ece&sa=U&;ei=QTunvoalhme2评估值;ved=0CBQPWI&;usg=AFQjCNFDcbVK87iUjDwI21jbIZUg0aU8gQ“>
印度教生活-新德里1小时:最高法院已暂停执行诺伊达和尼塔里谋杀案罪犯苏林德·科利的死刑一周。apex的一名官员…第一邮报-印度教斯坦时报-3天前en.wikipedia.org/wiki/Noida\u serial\u谋杀案<努伊达连环谋杀案(也叫尼塔里连环谋杀案,尼塔里-坎德)占据了…的
警方随后封锁了这所房子,不允许新闻媒体靠近……
- - - www.ndtv.com/topic/nithari-g
查找有关Nithari谋杀案的最新新闻、视频和图片,请参阅
来自NDTV.COM的最新更新、新闻和信息。了解更多有关Nithari…
timesofindia.indiatimes.COM/India/SC…Nithari…/41998225.cms
只需使用lookback来查找字符串q=http:
和lookahead来查找字符串&
(?<=q=http:).*?(?=\\s*&)
或
这将只匹配以q=
开头并以http:
另一种选择:
q=(http(?>[^&]+|&(?!amp))*)
然后捕获group1
试试这个。这会显示所有匹配项
看这里
@chopu:Pattern.compile((?@AvinashRaj)中缺少双引号。你的答案也是正确的。但它只给了我一个链接,而不是满足上述规则的整套链接。我不知道出了什么问题。你添加了多行修饰符吗?,
Pattern.compile(“(?仍不工作…只需将我提到的内容保存到一个文件中,格式为text.txt/html,并传递变量“content”"在模式类内部,您的输入是否只包含3个换行符?我想检查以q=http开头的字符。bcoz除了以相同开头的链接之外,还有其他文本。您的答案也是正确的Walid..但它只给了我一个链接,而不是满足上述要求的整套链接rules@chopu我得到了14场比赛。还有更多吗?没错……但当我指定不同的关键字时,它可能会有所不同。结果可能会根据@vksLet us关键字的不同而有所不同。
//ibnlive.in.com/news/noidas-nithari-killings-sc-stays-execution-of-convict-surinder-koli-for-a-week/497153-3-242.html
//www.hindustantimes.com/india-news/nithari-killer-surinder-koli-to-be-hanged-on-sept-12-jail-authorities/article1-1260116.aspx
String s = "q=http://ibnlive.in.com/news/noidas-nithari-killings-sc-stays-execution-of-convict-surinder-koli-for-a-week/497153-3-242.html & 1 hour agoNoida's Nithari killings: SC stays execution of convict Surinder Koli for a weekIBNLive - 1 hour agoNew Delhi: The Supreme Court has stayed the execution of Noida's Nithari killings convict\n"
+ " Surinder Koli for one week.\n"
+ "q=http://www.hindustantimes.com/india-news/nithari-killer-surinder-koli-to-be-hanged-on-sept-12-jail-authorities/article1-1260116.aspx &\n";
Pattern regex = Pattern.compile("(?<=q=)http:.*?(?=\\s*&)", Pattern.MULTILINE);
Matcher matcher = regex.matcher(s);
while(matcher.find()){
System.out.println(matcher.group(0));
}
http://ibnlive.in.com/news/noidas-nithari-killings-sc-stays-execution-of-convict-surinder-koli-for-a-week/497153-3-242.html
http://www.hindustantimes.com/india-news/nithari-killer-surinder-koli-to-be-hanged-on-sept-12-jail-authorities/article1-1260116.aspx
q=(http(?>[^&]+|&(?!amp))*)
.*?q=(http:.*?)&