in reply to Link regex
This is a good reason to use a /ex block to perform the magic.
local $/; my $data = <DATA>; $data =~ s{\[([^\[]*)\]} { my @bits = split '\|', $1; @bits= map{ s/^\s+|\s+$//g; $_}grep{ ! m/^\s*$/ } @bits; if ( @bits == 0 ) { # error handling just return original qq![$1]!; } else { # may want to auto add $bits[0] = "http://$bits[0]" unless $bits[0] =~ m/^(?:https?|ftp)/ or $bits[0] =~ m/[a-z][A-Z]|[A-Z][a-z]/; #camelCa +se if ( @bits == 1 ) { qq!<a href="$bits[0]">$bits[0]</a>!; } elsif ( @bits == 2 ) { qq!<a href="$bits[0]">$bits[1]</a>!; } elsif ( @bits == 3 ) { qq!<a href="$bits[0]" target="$bits[2]">$bits[1]</a>!; } } }gex; print $data; __DATA__ This is an error [] This is OK [www.perlmonks.org] This is OK [www1.test.com|test page1] This is OK [www2.test.com|test page2|new] This is errorish [www2.test.com|test page2|] This is errorish [ www2.test.com | test page2 | ] This is camelCase [ camelCase|Local Link in Blog | new ]
This allows you to handle errors gracefully (ie tolerate whitespace errors) and say add http:// to apparently external links and ignore for camelCase links (assuming this is for a blog) ie:
This is an error [] This is OK <a href="http://www.perlmonks.org">http://www.perlmonks.org +</a> This is OK <a href="http://www1.test.com">test page1</a> This is OK <a href="http://www2.test.com" target="new">test page2</a> This is errorish <a href="http://www2.test.com">test page2</a> This is errorish <a href="http://www2.test.com">test page2</a> This is camelCase <a href="camelCase" target="new">Local Link in Blog< +/a>
cheers
tachyon
|
|---|