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

Note that the final assertion has changed from (?! \( ) to (?! \]\( ).


Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^4: regex to collect local links in Markdown
by punkish (Priest) on Oct 27, 2007 at 20:03 UTC
    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

      '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