Skip to content

Latest commit

 

History

History
18 lines (14 loc) · 714 Bytes

extract-links-from-a-string.md

File metadata and controls

18 lines (14 loc) · 714 Bytes

Extract links from a String

Yesterday I needed to extract all links from a String, ideally I wanted a solution where I didn't have to write any Regexes, I stumbled across the handy little library Autolink-Java.

Using it like so the following method will produce a List of all links contained within the String

	public List<String> extractLinks(String body) {
		LinkExtractor linkExtractor = LinkExtractor.builder()
				.linkTypes(Sets.newHashSet(LinkType.URL))
				.build();

		return Lists.newArrayList(linkExtractor.extractLinks(body))
				.stream()
				.map(link -> body.substring(link.getBeginIndex(), link.getEndIndex()))
				.collect(toList());
	}