in reply to Bracketing Substring(s) in the String
sub put_bracket { my ($str,$ar) = @_; foreach my $subs ( @$ar ) { # Construct a regexp with [\[\]] between all the letters my $newsub = join('[\[\]]?',split(//,$subs)); $str =~ s/($newsub)/[$1]/g; } # Now de-nest the brackets in the string my $depth = 0; my $newstr = ''; foreach my $c (split(//,$str)) { if ($c eq "\[") { $newstr .= $c if ($depth++ == 0); } elsif ($c eq "\]") { $newstr .= $c if (--$depth == 0); } else { $newstr .= $c; } } print "$newstr\n"; return ; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Bracketing Substring(s) in the String
by monkfan (Curate) on Oct 28, 2005 at 07:16 UTC |