Regex 在perl中获取同一正则表达式中的某些字符串时替换和删除

Regex 在perl中获取同一正则表达式中的某些字符串时替换和删除,regex,perl,Regex,Perl,我的意见: $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 reinfo

我的意见:

$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;
$str='为了研究将时间分配给不利行为的机会成本,我们需要适当的强化计划描述列表,我们需要适当的强化计划';
我的$get\u val=($str=~m/]*)\\\?>/gi)[0];
打印$get\u val;
但是,我需要删除整个处理指令标记,同时查找相同的标记。在同一个图案上可能吗

我试过了,但没有成功

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

print $get_val;
my($get\u val)=($str=~s/]*)\}\?>///gi[0];
打印$get\u val;
以上输出打印“1”。如果有人能在这方面提供帮助,我们将不胜感激

提前感谢。

试试这个:

<\?processing-instruction.*?\?>

替换为空字符串

Perl代码示例:

use strict;

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';
my $regex = qr/<\?processing-instruction.*?\?>/p;
my $subst = '';

my $result = $str =~ s/$regex/$subst/rg;

print $result;
使用严格;
my$str='为了研究将时间分配给不利行为的机会成本,我们需要适当的强化计划描述列表,我们需要适当的强化计划';
my$regex=qr//p;
我的$subst='';
我的$result=$str=~s/$regex/$subst/rg;
打印$result;

如果替换成功,则所需的值将为
$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;

@toolic:虽然匹配成功,但当替换相同的元素时,它不会打印。perl中的替换运算符返回所做替换的数量,而匹配运算符返回加工和收集的元素列表。有关更完整的解释,请参见
perldoc perlop
。是。我需要在代码中学习这种逻辑和思维方式。令人惊叹的。
$$@@%
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