这个正则表达式已经过时了。 :( 还有一个问题: 我需要计算段落中的单词数和句子数。我尝试使用的代码是这样的:
my $sentencecount = $file =~ s/((^|\s)\S).*?(\.|\?|\!)/$1/g;
my $count = $file =~ s/((^|\s)\S)/$2/g;
print "Input file $ARGV[1] contains $sentencecount sentences and $count words.";
我的结果两项都返回 63。我知道这是不正确的,至少就字数而言。这是使用替代计数过程的结果吗?如果是这样,我该如何纠正这个问题?
最佳答案
我建议研究一下 perl split
函数,请参阅 perlfunc(1)
:
If EXPR is omitted, splits the $_ string. If PATTERN is also
omitted, splits on whitespace (after skipping any leading
whitespace). Anything matching PATTERN is taken to be a
delimiter separating the fields. (Note that the delimiter may
be longer than one character.)
关于regex - 使用正则表达式在 Perl 中计算句子/单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4846768/