use warnings;
use strict;
#= ordered hash
my @patterns=(
named => '\[ \[ (?.*?) \] \[ (?.*?) \] \]',
http => 'https?://[a-zA-Z./]+',
camel => '[A-Z][a-z]+[A-Z][a-z]+',
blank => '\s+',
unknown => '.+?',
);
my %patterns=@patterns;
#= build regex pattern
my @regexes;
my @names;
while ( my ($name,$regex) = splice @patterns,0,2) {
push @names, $name;
push @regexes,"(?<$name>$regex)";
}
my $regex = '(' . join ("\n|", map {"\n\t$_"} @regexes) . "\n)";
print "Pattern:\n$regex\n\n";
# = return which pattern matched
sub transform {
my @matching= grep { $-{$_}[0] } @names ;
return "@matching\t=>\t $1\n";
}
#= apply regex
while (){
chomp;
print "Line:\n$_\n\n";
s/$regex/transform()/gex;
print "Result:\n$_\n\n";
}
__DATA__
http://www.gmx.de CamelCase/WikiLink [[http://gmx.de][BlubBlub]]
####
Pattern:
(
(?\[ \[ (?.*?) \] \[ (?.*?) \] \])
|
(?https?://[a-zA-Z./]+)
|
(?[A-Z][a-z]+[A-Z][a-z]+)
|
(?\s+)
|
(?.+?)
)
Line:
http://www.gmx.de CamelCase/WikiLink [[http://gmx.de][BlubBlub]]
Result:
http => http://www.gmx.de
blank =>
camel => CamelCase
unknown => /
camel => WikiLink
blank =>
named => [[http://gmx.de][BlubBlub]]