punkish has asked for the wisdom of the Perl Monks concerning the following question:
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)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex to collect local links in Markdown
by GrandFather (Saint) on Oct 27, 2007 at 19:34 UTC | |
by punkish (Priest) on Oct 27, 2007 at 19:49 UTC | |
by GrandFather (Saint) on Oct 27, 2007 at 19:53 UTC | |
by punkish (Priest) on Oct 27, 2007 at 20:03 UTC | |
by GrandFather (Saint) on Oct 27, 2007 at 20:13 UTC | |
| |
|
Re: regex to collect local links in Markdown
by graff (Chancellor) on Oct 27, 2007 at 18:03 UTC | |
|
Re: regex to collect local links in Markdown
by andyford (Curate) on Oct 27, 2007 at 19:03 UTC |