URL::Search(3pm)

URL::Search3pm

Search for urls in plain text

Usage

  use URL::Search qw( $URL_SEARCH_RE extract_urls partition_urls );
  if ($text =~ /($URL_SEARCH_RE)/) {
      print "the first URL in text was: $1\n";
  }
  my @all_urls = extract_urls $text;

Overview

This module searches plain text for URLs and extracts them. It exports (on request) the following entities:

$url_search_re

This variable is the core of this module. It contains a regex that matches a URL.

NOTE: This regex uses capturing groups internally, so if you embed it in a bigger pattern, the numbering of any following capture groups will be off. If this is an issue, use named capture groups of the form "(?<NAME>...)" instead. See "Capture groups" in perlre.

It only matches URLs with an explicit schema (one of "http" or "https"). The pattern is deliberately not anchored at the beginning, i.e. it will match "http://foo" in "click herehttp://foo". If you don't want that, use "/\b$URL_SEARCH_RE/".

It tries to exclude artifacts of the surrounding text:

  Is mayonnaise an instrument? (https://en.wikipedia.org/wiki/Instrument,
  https://en.wikipedia.org/wiki/Mayonnaise_(instrument))

In this example it will match "https://en.wikipedia.org/wiki/Instrument" and "https://en.wikipedia.org/wiki/Mayonnaise_(instrument)", without the comma after "Instrument" and the final closing parenthesis.

It understands all common URL elements: username, hostname, port, path, query string, fragment identifier. The hostname can be an IP address (IPv4 and IPv6 are both supported).

Unicode is supported (e.g. "http://поддомен.example.com/déjà-vu?utf8=✓" is matched correctly).

"extract_urls"

This function takes a string and returns a list of all contained URLs.

It uses $URL_SEARCH_RE to find matches.

Example:

  my $text = 'Visit us at http://html5zombo.com. Also, https://archive.org';
  my @urls = extract_urls $text;
  # @urls = ('http://html5zombo.com', 'https://archive.org')

"partition_urls"

This function takes a string and splits it up into text and URL segments. It returns a list of array references, each of which has two elements: The type (the string 'TEXT' or 'URL') and the portion of the input string that was classified as text or URL, respectively.

Example:

  my $text = 'Visit us at http://html5zombo.com. Also, https://archive.org';
  my @parts = partition_urls $text;
  # @parts = (
  #   [ 'TEXT', 'Visit us at ' ],
  #   [ 'URL', 'http://html5zombo.com' ],
  #   [ 'TEXT', '. Also, ' ],
  #   [ 'URL', 'https://archive.org' ],
  # )

You can reassemble the original string by concatenating the second elements of the returned arrayrefs, i.e. "join('', map { $_->[1] } partition_urls($text)) eq $text".

This function can be useful if you want to render plain text as HTML but hyperlink all embedded URLs:

  use URL::Search qw(partition_urls);
  use HTML::Entities qw(encode_entities);
  my $text = ...;
  my $html = '';
  for my $part (partition_urls $text) {
      my ($type, $str) = @$part;
      $str = encode_entities $str;
      if ($type eq 'URL') {
          $html .= "<a rel='nofollow' href='$str'>$str</a>";
      } else {
          $html .= $str;
      }
  }
  # result is in $html

Support and documentation

After installing, you can find documentation for this module with the "perldoc" command.

    perldoc URL::Search

You can also look for information at <https://metacpan.org/pod/URL::Search>.

To see a list of open bugs, visit <https://rt.cpan.org/Dist/Display.html?Name=URL-Search>.

To report a new bug, send an email to "bug-URL-Search [at] rt.cpan.org".

Releases

3 releases since Debian 11 (Bullseye); the text changed 1 time.

  1. 11
  2. 12–13

Author

Lukas Mai, "<l.mai at web.de>"

From liburl-search-perl 0.06-1 in Debian 13 (Trixie). Parsed by carta from the page's man source.