Perl 中字符串之间的字符匹配计数 [英] Character match count between strings in Perl
本文介绍了Perl 中字符串之间的字符匹配计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串(比如字符串 1)需要与另一个字符串(字符串 2)匹配.两个字符串将具有相同的长度并且不区分大小写.
I have a string (say string 1) that needs to be matched to another string (string2). Both the strings will have the same length and are case in-sensitive.
我想打印两个字符串之间的字符匹配数.
I want to print the number of character matches between both the strings.
E.g.: String 1: stranger
String 2: strangem
Match count = 7
我试过了:
$string1 = "stranger";
$string2 = "strangem";
my $count = $string1 =~ m/string2/ig;
print "$count\n";
我该如何解决这个问题?
How can I fix this?
推荐答案
Exclusive or,然后计算空字符(其中字符串相同):
Exclusive or, then count the null characters (where the strings were the same):
my $string1 = "stranger";
my $string2 = "strangem";
my $count = ( lc $string1 ^ lc $string2 ) =~ tr/\0//;
print "$count\n";
我错过了不区分大小写"的部分.
I missed the "case in-sensitive" bit.
这篇关于Perl 中字符串之间的字符匹配计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文