Best way to replace a beginning and end character in Perl using Regular Expression?
Here is a single regex that removes , (comma) at the beginig or at the end of a string:

$str =~ s/^,+|,+$//g;


and here is a benchmark that compares this regex with a double one:

use Benchmark qw(:all);

my $str = q/,a,b,c,d,/;
my $count = -3;
cmpthese($count, {
        'two regex' =&gt; sub {
            $str =~ s/^,+//;
            $str =~ s/,+$//;
        },
        'one regex' =&gt; sub {
            $str =~ s/^,+|,+$//g;
        },
    });


The result:

               Rate one regex two regex
one regex  597559/s        --      -58%
two regex 1410348/s      136%        --


We can see that two regex are really faster than one regex that combines the two.