in reply to Regular Expressions: Call for Examples

Here are 3 regexes i used recently for Node Link Checker. The problem is to turn PM link settings into their respective HTML links. First, the lookup table:
my %TAG = ( ftp => 'ftp://', http => 'http://', https => 'https://', kobe => 'http://theoryx5.uwinnipeg.ca/mod_perl/cpan-search?filet +ype=+distribution+name+or+description&j&case=clike&search=', kobes => 'http://theoryx5.uwinnipeg.ca/mod_perl/cpan-search?filet +ype=+distribution+name+or+description&j&case=clike&search=', cpan => 'http://search.cpan.org/search?mode=module&query=', isbn => 'http://shop.barnesandnoble.com/booksearch/isbnInquiry.a +sp?isbn=', google => 'http://www.google.com/search?q=', lucky => 'http://www.google.com/search?btnI=I&q=', jargon => 'http://www.science.uva.nl/cng/search/htsearch.CGI?restr +ict=%2F%7Emes%2F&jargon%2Fwords=', id => '/index.pl?node_id=', pad => '/index.pl?node_id=108949&user=', DEFAULT => '/index.pl?node=', );
Next, the regexes:
# takes care of [tag://target|alt] $chunk =~ s/\[(\w+):\/\/(.*?)\|([^\]]+)\]/<a href="$TAG{$1}$2">$3<\/a> +/g; # takes care of [tag://target] $chunk =~ s/\[(\w+):\/\/([^\]]+)\]/<a href="$TAG{$1}$2">$2<\/a>/g; # takes care of [target] $chunk =~ s/\[([^\]]+)\]/<a href="$TAG{DEFAULT}$1">$1<\/a>/g;
The 3 regexes have to be executed in that order. Maybe they could be combined into one regex, but this worked for me. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) Re: Regular Expressions: Call for Examples
by jryan (Vicar) on Jul 22, 2002 at 01:56 UTC

    I wrote this mostly to annoy jeffa because of:

    they could be combined into one regex
    But, he insisted that I post this here :)
    $chunk =~ s! \[ (?(?= [^:]*://) (?: (\w+):// ([^|\]]+) (?: \| ([^\[]+) )? ) | (\w+) ) \] ! $_ = "<a href=\"".((defined$1)?(qq($TAG{$1}$2">).((defined$3)?$3:$2)): qq($TAG{DEFAULT}$4">$4))."</a>"!gex;

    I truely think that jeffa's approach is better than the above. It's much smarter to break a 3 case problem into 3 steps rather than use 1 gigantic regex. Just look at the "substitution" section; its hidious (I'd normally have used a sub to handle the above "substitution" section, but then wouldn't be "one regex" :)

    .