简体   繁体   中英

How to extract a link from a String in Java or Groovy?

Let's say you have this variable,

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

or you have this,

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

How can I extract only the link into a list?

Something like this will work for the two examples you give:

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

That is, find anything that starts with http:// up until some sort of whitespace

There are more complex regular expressions over on this question , but I've not tried any of them, and they may be overkill for your situation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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