use strict;
my $str = '== Good ==
https://facebook.com/marko/polo
https://facebook.com/marko/pol2o
www.moshe.io
http://marko.polo.com
subdomain.pizza.com
bitly.com/14awOx4
== Good in random text
Dude, I\'m telling you, every other https://facebook.com/marko/polo URL regex just so sucked. it even crashed https://facebook.com/marko/pol2o. In one case + there are case where www.moshe.io would be cought but this one works likes charm http://marko.polo.com exactly what I needed // // look it doesn\'t catch subdomain.pizza.com anyhthing else ! it\'s totally amazing !
bitly.com/fdsafdf
== Bad ==
https://www.........more..........no/com
4.5x10.9
etc..!
me.you
you.them
';
my $regex = qr/((?<=[^a-zA-Z0-9])(?:https?\:\/\/|[a-zA-Z0-9]{1,}\.{1}|\b)(?:\w{1,}\.{1}){1,5}(?:com|org|edu|gov|uk|net|ca|de|jp|fr|au|us|ru|ch|it|nl|se|no|es|mil|iq|io|ac|ly|sm){1}(?:\/[a-zA-Z0-9]{1,})*)/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html