regex - 使用正则表达式在 Perl 中计算句子/单词

标签 regex perl count cpu-word

这个正则表达式已经过时了。 :( 还有一个问题: 我需要计算段落中的单词数和句子数。我尝试使用的代码是这样的:

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/

相关文章:

regex - Tableau 正则表达式 Regexp_Extract() 问题

perl - 使用 Perl 处理开发/生产库路径的好方法是什么?

sql - 如何使用 SQL SERVER 计算每个多边形与多少点相交

sql - T-SQL 计数问题

php - 计算 PHP 项目中的行数

C++ std::regex 混淆

python - 正则表达式查找一组数字中的最后两个位置

java - 正则表达式匹配回车后的任何字符并给出单个分隔字符串

perl - SQLite:如何获取绝对行号

perl - 如何在 Perl 或任何语言中四舍五入到小数点左边第一位数字?