2016-09-23 35 views
-1

比方说,你有这个变量,如何从Java或Groovy中的字符串中提取链接?

String message = "This is a link http://www.example.com/" 

或者你有这样的,

String message2 = "This is a link http://www.example.com/ and another link http://www.myfico.com/Images/sample_overlay.gif" 

我怎么能只提取链接到一个列表?

回答

1

像这样的事情会在两个例子中你给工作:

def links = message2.findAll(/http:\/\/\S+/) 

也就是说,找到任何与http://开始,直到某种空白

的还有更复杂的正则表达式over on this question,但我没有尝试过其中任何一种,并且它们可能会对您的情况有所杀伤