发布通用Perl嵌套规则表达式函数
sub regex_nesting {
my $start_string = shift;
my $end_string = shift;
my $re;
return if $start_string eq $end_string;
my $sFirst = substr($start_string,0,1);
my $sOther = substr($start_string,1);
my $eFirst = substr($end_string,0,1);
my $eOther = substr($end_string,1);
$start_string =~ s/([\{\}\[\]\(\)\^\$\.\|\*\+\?\\])/\\$1/g;
$end_string =~ s/([\{\}\[\]\(\)\^\$\.\|\*\+\?\\])/\\$1/g;
$sFirst =~ s/([\{\}\[\]\(\)\^\$\.\|\*\+\?\\])/\\$1/g;
$sOther =~ s/([\{\}\[\]\(\)\^\$\.\|\*\+\?\\])/\\$1/g;
$eFirst =~ s/([\{\}\[\]\(\)\^\$\.\|\*\+\?\\])/\\$1/g;
$eOther =~ s/([\{\}\[\]\(\)\^\$\.\|\*\+\?\\])/\\$1/g;
if ($sOther) {
if ($eOther) {
if ($sFirst eq $eFirst) {
$re = "$start_string(??>(?:[^$sFirst]|(?sFirst(?!(?sOther)|(?eOther))))+)|(??{\$re}))*$end_string";
} else {
$re = "$start_string(??>(?:[^$sFirst$eFirst]|(?sFirst(?!$sOther))|(?eFirst(?!$eOther)))+)|(??{\$re}))*$end_string";
}
} else {
if ($sFirst eq $eFirst) {
$re = "$start_string(??>[^$sFirst]+)|(??{\$re}))*$end_string";
} else {
$re = "$start_string(??>(?:[^$sFirst$eFirst]|(?sFirst(?!$sOther)))+)|(??{\$re}))*$end_string";
}
}
} elsif ($eOther) {
if ($sFirst eq $eFirst) {
$re = "$start_string(??>[^$sFirst]+)|(??{\$re}))*$end_string";
} else {
$re = "$start_string(??>(?:[^$sFirst$eFirst]|(?eFirst(?!$eOther)))+)|(??{\$re}))*$end_string";
}
} else {
$re = "$start_string(??>[^$sFirst$eFirst]+)|(??{\$re}))*$end_string";
}
print "$re\n";
return eval('$re = qr/'.$re.'/;');
}