I am doing some customization of John Gruber's Markdown, and in the process, struggling with some regex work that hopefully you can give my some guidance on.
I want to collect all the local links in my text. So, in the example below, I want only
Perl Monk Regular ExpressionsThe stuff in []() is not local, and the stuff in ![] is an image, hence I don't want them. I have not succeeded in making a good regex for what I need. Here is what I have...
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."; 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"; } }
Update: there could be anything inside [] and it would be valid. So, we could have [Some (stuff) !Wow] and it would be valid. The only links that are not local are [link text](link)
In reply to regex to collect local links in Markdown by punkish
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |