2

この正規表現は古くなっています。:(さらに別の質問:段落内の単語数と文数を数える必要があります。使用しようとしたコードは次のとおりです。

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を返します。少なくとも単語数に関しては、これが正しくないことを私は知っています。これは、カウントプロセスの代わりに使用した結果ですか?もしそうなら、どうすればこれを修正できますか?

4

3 に答える 3

2

perlsplit関数を調べることをお勧めします。以下を参照してください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.)
于 2011-01-31T01:19:24.397 に答える
1
my $wordCount = 0;
++$wordCount while $file =~ /\S+/g;

my $sentenceCount = 0;
++$sentenceCount while $file =~ /[.!?]+/g;

ここにあるようにスカラーコンテキストでマッチングを行うこと//gで、すべての単語またはすべての文の膨大なリストを作成することを回避し、ファイルが大きい場合にメモリを節約します。文カウントコードは、任意の数の文末区切り文字を1つの文としてカウントします(たとえばHello... world!、2つの文としてカウントされます)。

于 2011-01-31T01:51:02.633 に答える
0

これは、から文と文字の数を取得します$file

$file="This is praveen worki67ng in RL websolutions";
my $count = () = $file =~ /\S+/g;
my $counter = () = $file =~ /\S/g;
于 2013-04-27T09:06:20.730 に答える