Regex 查找和替换并获取被替换内容值的计数
我需要使用regex已知或未知模式查找和替换内容,然后我们需要存储查找内容、替换内容、替换内容计数:(我需要从100多个替换中获取此信息) 这是我们需要得到的 1)查找内容Regex 查找和替换并获取被替换内容值的计数,regex,perl,Regex,Perl,我需要使用regex已知或未知模式查找和替换内容,然后我们需要存储查找内容、替换内容、替换内容计数:(我需要从100多个替换中获取此信息) 这是我们需要得到的 1)查找内容 2) 替换内容和 3) 计数值: 示例:-->计数 此外,我们不能假设这和那可能是我们正在查找和替换的内容,我们应该得到这份报告 我尽了最大努力: use strict; use warnings; my $str = 'Trp $\mathbf{R}^a$ locates \alpha \beta distantly $
2) 替换内容和
3) 计数值: 示例:-->计数 此外,我们不能假设这和那可能是我们正在查找和替换的内容,我们应该得到这份报告 我尽了最大努力:
use strict;
use warnings;
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';
#Sample :
$str=~s{<findcontent>}{<replacedcontent>}g;
#Tried something *different*
my $cnt=0; my (@pushStatsreport,$Statsreport) = "";
$str=~s/\\alpha/my $find=$&; my $rep = "\\boldsymbol\{\\alpha\}"; $cnt++; $Statsreport = "Find: $find\tRep: $rep\tCnt: $cnt\n"; ($rep); /ge;
push(@pushStatsreport, $Statsreport); $cnt=0;
$str=~s/\\math(it|rm|bf)\s*([a-z])\b/my $find = $&; my $rep = "\\checkmath$1\{$2\}"; $cnt++; $Statsreport = "Find: $find\tRep: $rep\tCnt: $cnt\n"; ($rep); /ge;
push(@pushStatsreport, $Statsreport);
print join "\n", @pushStatsreport;
使用严格;
使用警告;
my$str='Trp$\mathbf{R}^a$在\gamma和组织蛋白酶K中分别位于\alpha\beta远处的$\mathrm{R}^a$和$\mathit{R}^a$cys25,分别具有\alpha高和中度的$\mathbb{R}^1H$强度。蛋白质$\mathds{R}^a$修饰$\mathds{R}^1H$主要影响\γ肽的结合位点和稳定性\γ,其影响取决于\α肽的元素组成';
#样本:
$str=~s{}{}g;
#尝试了一些不同的东西*
我的$cnt=0;我的(@pushStatsreport,$Statsreport)=“”;
$str=~s/\\alpha/my$find=$&;my$rep=“\\boldsymbol\{\\alpha\}”$cnt++$Statsreport=“Find:$Find\tRep:$rep\tCnt:$cnt\n”;($rep)/通用电气;
推送(@pushStatsreport,$Statsreport)$cnt=0;
$str=~s/\\math(it | rm | bf)\s*([a-z])\b/my$find=$&;my$rep=“\\checkmath$1\{$2\}”$cnt++$Statsreport=“Find:$Find\tRep:$rep\tCnt:$cnt\n”;($rep)/通用电气;
推送(@pushStatsreport,$Statsreport);
打印join“\n”,@pushStatsreport;
我的结果和期望输出:
查找:\alpha Rep:\boldsymbol{\alpha}Cnt:3查找:\mathbf a Rep:\checkmathbf{a}Cnt:2
然而,我不能接受这是更好的方式。有人可以在这方面帮助我。您可以尝试将搜索和替换字符串放入数组中,然后迭代进行替换并收集计数和报告:
my @findreplace = (
[ q<\\\\alpha>, q<\boldsymbol{\alpha}>],
[ q<\\\\math(it|rm|bf)\s*([a-z])\b>, q<\checkmath$1{$2}>],
);
my (@pushStatsreport, $Statsreport);
for my $item (@findreplace) {
my ( $regex, $replace ) = @$item;
my $cnt = $str =~ s{$regex}{'"$replace"'}eeg;
$Statsreport = "Find: $regex\tRep: $replace\tCnt: $cnt";
push @pushStatsreport, $Statsreport;
}
my@findreplace=(
[q,q],
[q,q],
);
我的(@pushStatsreport,$Statsreport);
对于我的$item(@findreplace){
my($regex,$replace)=@$项目;
my$cnt=$str=~s{$regex}{''$replace'}eeg;
$Statsreport=“Find:$regex\tRep:$replace\tCnt:$cnt”;
push@pushStatsreport,$Statsreport;
}
< /代码>请考虑使用代码“>代码> PurLtDy 来格式化代码;在同一行上有几个语句,像$str=~s{}{}{}g代码>--这是什么意思?
和
是真正的字符串吗?一个更清晰的问题会让人们更容易回答。输入字符串$str
只包含一个\mathbf{R}
的实例,这与\mathbf A
的期望输出如何一致?请澄清