Very nearly right. The assertion that '(' doesn't follow the link should be an assertion that '](' doesn't follow the link. Note that in the fixed code I've added a call to the sub so that the code runs as a complete sample:
use strict; use warnings; my $text = "Hello, I am a [Perl Monk], still not good at [Regular Expressions]. I have been helped immensely by the good monks at [Perl Monks](http://www.perlmonks.org). This is what I look like ![mymugshot]. Thank you."; listlink ($text); sub listlink { my ($text) = @_; my @links = ( $text =~ / (?<!\!\[) # make sure the match is not preceded by a ![ (?<=\[) # but is in fact preceded by just a [ .*? # the match (?=\]) # followed by a ] (?!\]\() # but not followed by a ( /xg ); foreach my $link (@links) { print "$link\n"; } }
Prints:
Perl Monk Regular Expressions
In reply to Re: regex to collect local links in Markdown
by GrandFather
in thread regex to collect local links in Markdown
by punkish
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |