I thought it might be interesting to benchmark the various solutions:

#!/usr/bin/env perl use warnings; use strict; use Benchmark qw/cmpthese/; use JSON::PP (); use Cpanel::JSON::XS (); my $IN = '"654321_1111"'; my $EXP = '654321_1111'; use constant TEST=>0; print "Testing is ", TEST?"enabled":"disabled", "\n"; my $json_pp = JSON::PP->new->allow_nonref; my $json_xs = Cpanel::JSON::XS->new->allow_nonref; cmpthese(-2, { regex_strip => sub { # GrandFather (my $out = $IN) =~ s/^"|"$//g; $out eq $EXP or die $out if TEST; }, regex_match => sub { # BillKSmith my ($out) = $IN =~ /^\"(.+)\"$/; $out eq $EXP or die $out if TEST; }, tr_strip => sub { # kcott and hippo (my $out = $IN) =~ y/"//d; $out eq $EXP or die $out if TEST; }, substr => sub { # syphilis and AnomalousMonk my $out = substr $IN, 1, -1; $out eq $EXP or die $out if TEST; }, reverse => sub { # rsFalse my $out = $IN; for (1..2) { $out = reverse $out; chop $out; } $out eq $EXP or die $out if TEST; }, json_pp => sub { # haukex my $out = $json_pp->decode($IN); $out eq $EXP or die $out if TEST; }, json_xs => sub { # haukex my $out = $json_xs->decode($IN); $out eq $EXP or die $out if TEST; }, }); __END__ Testing is disabled Rate json_pp regex_strip regex_match reverse json_xs + tr_strip substr json_pp 161266/s -- -85% -96% -96% -97% + -97% -99% regex_strip 1081962/s 571% -- -73% -74% -81% + -81% -91% regex_match 3989148/s 2374% 269% -- -2% -29% + -30% -67% reverse 4091430/s 2437% 278% 3% -- -28% + -28% -66% json_xs 5654082/s 3406% 423% 42% 38% -- + -0% -53% tr_strip 5663601/s 3412% 423% 42% 38% 0% + -- -53% substr 12107824/s 7408% 1019% 204% 196% 114% + 114% --

However, it's also very noteworthy that all of the solutions have their caveats, as already highlighted by AnomalousMonk and hippo:

This underscores what others have already said: it would be better if you could better specify your input format.


In reply to Re: remove first and last character of string - benchmarks by haukex
in thread remove first and last character of string by flieckster

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.