in reply to Re^3: regex to collect local links in Markdown
in thread regex to collect local links in Markdown

my $text = "Hello, I am a [Perl Monk], still not good at [Regular Expr +essions]. I have been helped immensely by the good monks at [Perl Mon +ks](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"; } } ####### Perl Monk Regular Expressions Perl Monks](http://www.perlmonks.org). This is what I look like ![mymu +gshot

Update: took out superfluous code.

--

when small people start casting long shadows, it is time to go to bed

Replies are listed 'Best First'.
Re^5: regex to collect local links in Markdown
by GrandFather (Saint) on Oct 27, 2007 at 20:13 UTC

    'twas the line breaks made the difference. The fix is to use [^\]]* for the match.

    (. doesn't match new line characters unless you ask nicely with the /s switch.)


    Perl is environmentally friendly - it saves trees
      indeed, you are correct. Care to explain [^\]]* instead of .*?
      --

      when small people start casting long shadows, it is time to go to bed

        . by default doesn't match \n. [^\]] matches anything, including \n, except ]. The /s switch allows . to match \n.


        Perl is environmentally friendly - it saves trees