frank1 has asked for the wisdom of the Perl Monks concerning the following question:

i have a JSON response of this

90'+7'

my question is how to strip out of single quotes between +7 and have integer 90+7 = 97

i want to strip out of single quotes when time '+additional time' exits, if not then nothing to strip out

Replies are listed 'Best First'.
Re: strip out of single quotes
by ysth (Canon) on Sep 16, 2025 at 13:35 UTC
    Not sure where JSON is involved, what you show is not JSON.

    To just remove the quotes in this case, you can do:
    $string = "90'+7'"; $string =~ s/'(\+\d+)'/$1/a;
    (assuming it is always + not - and an integer). If it could happen more than once in your string, change /a to /ga.

    If you mean you want the addition actually performed, leaving you with 97, do:
    $string = "90'+7'"; $string =~ s/(\d+)'(\+\d+)'/$1+$2/ae;
    (assuming the first number is a not too large unsigned integer).

      thank you, i appreciate

Re: strip out of single quotes
by ikegami (Patriarch) on Sep 16, 2025 at 14:00 UTC

    This strips all single quotes:

    $str =~ s/'//g;

    According to what you posted ("there's nothing to strip out when '+additional time' doesn't exist"), this should do the trick.

      Or:

      $str =~ tr/'//d;
      Naked blocks are fun! -- Randal L. Schwartz, Perl hacker

        Indeed. And tr/'//d is faster than s/'//g.