[英]Perl regular expressions explanation
I was hoping to get a little explanation I have the following script: 我希望得到一些解释,我有以下脚本:
open (FILE, '2.txt');
@DNA = <FILE>;
$DNA = join ('', @DNA);
print "DNA = ". $DNA . "\n";
$a=0;
while ($DNA =~ //ig) {$a++;}
print "Total characters = ".$a."\n";
$b=0;
while ($DNA =~ /fl/ig) {$b++;}
print "Total fl = ".$b."\n";
$c=0;
while ($DNA =~ /[^fl]/ig) {$c++;}
print "Total character less fl = ".$c."\n";
exit;
The text document "2.txt" contains the following characters: 文本文档“ 2.txt”包含以下字符:
flkkkklllkkfewnofnewofewfl
When I run the script I get the following outputs: 运行脚本时,我得到以下输出:
DNA = flkkkklllkkfewnofnewofewfl
Total characters = 27
Total fl = 2
Total character less fl = 16
My question is, why when I do 我的问题是,为什么要做
while ($DNA =~ /fl/ig) {$b++;}
if counts all the instances of fl together, while ($DNA =~ /fl/ig) {$b++;}
如果一起计算fl的所有实例,
but when I do 但是当我这样做
while ($DNA =~ /[^fl]/ig) {$c++;}
it counts the number of characters that while ($DNA =~ /[^fl]/ig) {$c++;}
会计算
are neither an f or and l (ie the f & the l are treated separately). 都不是f或and l(即f和l分别处理)。
I was looking for the script to count the number of characters that are not fl (ie treated together) 我在寻找脚本来计算不是fl(即一起处理)的字符数
[fl]
is a character class, means f or l . [fl]
是字符类,表示f或l 。
It doesn't mean the substring fl
. 这并不意味着子串
fl
。
So [^fl]
counts all the characters that are not f or l. 因此
[^fl]
计算所有不是f或l的字符。
However, you could do that with a regex like this - 但是,您可以使用这样的正则表达式来做到这一点-
/[^fl]|f(?!l)|(?<!f)l/
Formatted: 格式:
[^fl] # Not f nor l
| f (?! l ) # f not followed by l
| (?<! f ) l # l not following f
Keeping it simple, maybe consider dropping all the instances of "fl" first, then simply counting the remaining characters: 保持简单,可以考虑先删除所有“ fl”的实例,然后简单地计算剩余的字符:
$DNA =~ s/fl//g;
print "Total characters less fl = ".length($DNA)."\n";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.