我需要找到希腊字符并替换相应的值及其替换的计数数字:(我需要从100多个替换字符中获取该字符)
这是我的编码:
use strict;
use warnings;
my @grkChars = qw(alpha beta gamma);
my $eachGrk = join "|", @grkChars; #Greek Characters
my $str = 'Trp $\mathbf{R}^a$ locates \alpha \beta distantly $\mathrm{R}^a$ from $\mathit{R}^a$ cys25 in both \gamma and cathepsin K, with \alpha high and moderate $\mathbb{R}^1H$ strengths, respectively. The protein $\mathds{R}^a$ modification $\mathds{R}^1H$ largely \beta affects the binding sites and stability \gamma of the \gamma peptides, and the effects depend on \alpha the elemental compositions of the peptides';
my $count = $str=~s{\\($eachGrk)}{\\\{$1\}}g && print "Content: $&\n";
print "Total Count: $count\n";
我的输出:
Content: \alpha
Total Count: 1
预期输出为:
\alpha changed to \{alpha} and count 3
\beta changed to \{{beta}} and count 2
\gamma changed to \[gamma] and count 3
您能不能请有人对此提出建议。预先感谢。
答案 0 :(得分:1)
尽管您可以执行类似创建 executable 替换操作的操作,该替换操作会增加哈希字段而不是全局$count
变量。最好保持简单。如果要单独计算单词,则需要单独替换单词。不用创建复合正则表达式模式,而只需遍历单词数组
赞
use strict;
use warnings;
my @grkChars = qw/ alpha beta gamma /;
my $str = 'Trp $\mathbf{R}^a$ locates \alpha \beta distantly $\mathrm{R}^a$ from $\mathit{R}^a$ cys25 in both \gamma and cathepsin K, with \alpha high and moderate $\mathbb{R}^1H$ strengths, respectively. The protein $\mathds{R}^a$ modification $\mathds{R}^1H$ largely \beta affects the binding sites and stability \gamma of the \gamma peptides, and the effects depend on \alpha the elemental compositions of the peptides';
my $total = 0;
for my $grk ( @grkChars ) {
next unless my $count = $str =~ s{\\($grk)}{\\\{$1\}}g;
printf "\\%s changed to \\{%s} and count %d\n", $1, $1, $count;
$total += $count;
}
print "Total Count: $total\n";