Java library to extract links (URLs, email addresses) from plain text; fast, small and smart
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.


Java library to extract links such as URLs and email addresses from plain text. Fast, small and smart about recognizing where links end.

Inspired by Rinku. Similar to it, regular expressions are not used. Instead, the input text is parsed in one pass with limited backtracking.

This library requires at least Java 7 (tested up to Java 11). It works on Android (minimum API level 15). It has no external dependencies.

Maven coordinates (see here for other build systems):


Build status Coverage status Maven Central status


Extract links:

import org.nibor.autolink.*;

String input = "wow, so example:";
LinkExtractor linkExtractor = LinkExtractor.builder()
        .linkTypes(EnumSet.of(LinkType.URL, LinkType.WWW, LinkType.EMAIL))
Iterable<LinkSpan> links = linkExtractor.extractLinks(input);
LinkSpan link = links.iterator().next();
link.getType();        // LinkType.URL
link.getBeginIndex();  // 17
link.getEndIndex();    // 32
input.substring(link.getBeginIndex(), link.getEndIndex());  // ""

Note that by default all supported types of links are extracted. If you're only interested in specific types, narrow it down using the linkTypes method.

There's another method which is convenient for when you want to transform all of the input text to something else. Here's an example of using that to transform the text to HTML and wrapping URLs in an <a> tag (escaping is done using owasp-java-encoder):

import org.nibor.autolink.*;
import org.owasp.encoder.Encode;

String input = "wow such linked";
LinkExtractor linkExtractor = LinkExtractor.builder()
        .linkTypes(EnumSet.of(LinkType.URL)) // limit to URLs
Iterable<Span> spans = linkExtractor.extractSpans(input);

StringBuilder sb = new StringBuilder();
for (Span span : spans) {
    String text = input.substring(span.getBeginIndex(), span.getEndIndex());
    if (span instanceof LinkSpan) {
        // span is a URL
        sb.append("<a href=\"");
    } else {
        // span is plain text before/after link

sb.toString();  // "wow <a href=\"\"></a> such linked"


URL extraction

Extracts URLs of the form scheme://example with any potentially valid scheme. URIs such as example:test are not matched (may be added as an option in the future). If only certain schemes should be allowed, the result can be filtered. (Note that schemes can contain dots, so foo.http://example is recognized as a single link.)

Includes heuristics for not including trailing delimiters such as punctuation and unbalanced parentheses, see examples below.

Supports internationalized domain names (IDN). Note that they are not validated and as a result, invalid URLs may be matched.

Example input and linked result:

Use LinkType.URL for this, and see test cases here.

WWW link extraction

Extract links like They need to start with www. but don't need a scheme://. For detecting the end of the link, the same heuristics apply as for URLs.


Not supported:

  • Uppercase www's, e.g. and
  • Too many or too few w's, e.g.

The domain must have at least 3 parts, so is not valid, but is.

Use LinkType.WWW for this, and see test cases here.

Email address extraction

Extracts emails such as Matches international email addresses, but doesn't verify the domain name (may match too much).


Not supported:

  • Quoted local parts, e.g. "this is sparta"
  • Address literals, e.g. foo@[]

Note that the domain must have at least one dot (e.g. foo@com isn't matched), unless the emailDomainMustHaveDot option is disabled.

Use LinkType.EMAIL for this, and see test cases here.


See file.


Copyright (c) 2015-2018 Robin Stocker and others, see Git history

MIT licensed, see LICENSE file.