Skip to content

Instantly share code, notes, and snippets.

@Alhadis
Created April 6, 2019 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alhadis/20ccc2da4f3dce698e50395a563574ce to your computer and use it in GitHub Desktop.
Save Alhadis/20ccc2da4f3dce698e50395a563574ce to your computer and use it in GitHub Desktop.
Match URL
#!/usr/bin/env perl
# Adopted from https://github.com/atom/language-hyperlink
use strict;
use warnings;
use v5.14;
my $matchURL = qr~ \b
# Protocol
( https?
| s?ftp
| ftps
| file
| smb
| git
| ssh
| rsync
| afp
| nfs
| (?:x-)?man(?:-page)?
| gopher
| txmt
| issue
| atom
) ://
# Path specifier
(?:
(?! \#\w*\#)
(?: [-:\@\w.,\~\%+_/?=\&\#;|!])
)+
# Don't include trailing punctuation
(?<![-.,?:\#;])
~aix;
my $matchEmail = qr~ \b
mailto: (?:
(?! \#\w*\#)
(?: [-:\@\w.,\~\%+_/?=\&\#;|!])
)+
(?<![-.,?:\#;])
~aix;
$matchURL = qr~$matchURL|$matchEmail~;
# Usage:
my $input = <<EOF;
Do this and go to https://some.site/file.html?okay=1.
http://deno.land?
mailto:this\@guy.com.
EOF
-t 1
? $input =~ s/$matchURL/\e[4m$&\e[0m/g
: $input =~ s/$matchURL/<a href="$&">$&<\/a>/g;
say $input;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment