ssr1012 ssr1012 - 21 days ago 9
Perl Question

Replace and Delete when fetching some string in the same regex in perl

My Input:

$str = 'In order to study the opportunity cost of allocating time to the less beneficial act, we need appropriate schedules of reinforcement description <?processing-instruction \value{$$@@%}?> list we need appropriate schedules of reinforcement';

my $get_val = ($str=~m/<\?processing\-instruction\s*\\value\{([^\}\?>]*)\}\?>/gi)[0];

print $get_val;


However I need to delete the whole processing instruction tag while finding the same. Is it possible on the same pattern?

I tried this however not success.

my ($get_val) = ($str=~s/<\?processing\-instruction\s*\\value\{([^\}\?>]*)\}\?>//gi)[0];

print $get_val;


The above output prints '1'. It would be appreciate if someone could help on this one.

Thanks in advance.

Answer

If the substitution is successful, then the value you want will be in $1.

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

my $str = 'In order to study the opportunity cost of allocating time to the less beneficial act, we need appropriate schedules of reinforcement description <?processing-instruction \value{$$@@%}?> list we need appropriate schedules of reinforcement';

if ($str =~ s/<\?processing\-instruction\s*\\value\{([^\}\?>]*)\}\?>//gi) {
  say $1;
}

say $str;

Output:

$$@@%
In order to study the opportunity cost of allocating time to the less beneficial act, we need appropriate schedules of reinforcement description  list we need appropriate schedules of reinforcement