作业帮 > 英语 > 作业

用(正则表达式)统计文件中所有英文单词的数量,符号、空格、数字不再识别范围之内(大小写不计算在内)

来源:学生作业帮 编辑:作业帮 分类:英语作业 时间:2024/10/06 08:19:25
用(正则表达式)统计文件中所有英文单词的数量,符号、空格、数字不再识别范围之内(大小写不计算在内)
Now
is the time
for all good-men
to come to the-
aid of -their
party.NOW IS THE TIME FOR all Good men to
come to the AID of their party.party-their now-is time-the-for all-good
men to -come aid of party a&b%c*(d)
a1b2c3d4-
a
b
b c defg
thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled?
thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled2?
thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled3?
%^&*()
#!/usr/bin/perl
open IN,"as.txt";
while(){
@line=split/\s+/,$_;
foreach $line(@line){
$count++ if ($line=~/^\w+$/);
}
}
print $count."\n";
$count=36
再问: 这个就行??? 还能具体点不
再答: 只要单词中出现符号、空格、数字就不当作单词对待,你想要什么功能
再问: 就是想要用正则表达式写出我发的那个文件中出现的所有单词的数量就行
再答: 你可以自己统计下,是不是36 $count++ if ($line=~/^\w+$/); 这个正则就是匹配单词的
再问: 不是这个意思,我的意思你误解了,我要的程序是需要我发的这个文件中,不同单词出现的次数,老师给的答案是这个,你参考下 a: 3 aid: 3 all: 3 b: 4 c: 3 come: 3 d: 2 defg: 1 for: 3 good: 3 is: 3 men: 3 now: 3 of: 3 party: 4 the: 5 their: 3 thisisaverylongwordwithnospacesbetweenthewordsandhowwillitbehandled: 3 time: 3 to: 5
再答: 我改下 #!/usr/bin/perl open IN,"as.txt"; while(){ @line=split/[\W\d_]+/,$_; foreach $line(@line){ if ($line=~/^(\w+)$/){ $turn=lc($1); $hash{$turn}++; } } } foreach $key(sort keys %hash){ print "$key: $hash{$key}\n"; }
再问: 他说不能用PHP语言, 他给的是4种语言 1. Javascript 2. Python 3. Java 4. C# 你看看能不能用我给出的这4种语言中的一种来编写一个程序来满足这个要求
再答: 这四种我都不熟啊,不过思路已经给你了,接下来就是语法的问题而已