my $line = "The quick brown fox jumps over the lazy dog.";
while ($line){
$line =~ s/["",]//ig; #[] means to get rid of
#print $line
$line = lc($line); #lc is lowercase
while ($line=~m/\b(\w \s\w )\b/ig){ #[^ ] means any character except spaces and newline #($line=~m/\b(\s\w \s\w )\b/ig)
my $word =$1;
print "$word\n";
$wordcount{$word} = 1;
}
last;
}
close(INPUT);
close(OUTPUT);
期望的輸出將是:快速,快速的棕色狐貍,棕色狐貍,狐貍跳躍......但是,對于上面的代碼,我只得到快速,棕色狐貍,跳躍......
uj5u.com熱心網友回復:
要捕獲兩者但不消耗第二個,以便對重疊,前瞻很有用
use warnings;
use strict;
use feature 'say';
my $string = shift // 'The quick brown fox jumps over the lazy dog.';
while ( $string =~ /(\w )\s(?=(\w ))/g ) {
say "$1 $2";
}
根據需要列印。
uj5u.com熱心網友回復:
您可以使用
(\w )\s(?=(\w \b))
正則運算式解釋
(捕獲組\w匹配一個詞
)關閉組\s匹配一個空格(?=Lookahead assertion - 斷言以下正則運算式匹配(捕獲組\w \b匹配一個詞
)關閉組
)關閉前瞻
見正則運算式演示
Perl 示例
my $line = "The quick brown fox jumps over the lazy dog.";
while ($line =~ /(\w )\s(?=(\w \b))/g) {
print("$1 $2\n");
}
輸出
The quick
quick brown
brown fox
fox jumps
jumps over
over the
the lazy
lazy dog
uj5u.com熱心網友回復:
如果將字串拆分為單詞陣列,則根本不需要對正則運算式做任何花哨的事情:
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
my $line = "The quick brown fox jumps over the lazy dog.";
$line =~ s/[^\w\s]//g; # Remove non-word, non-whitespace characters
my @words = split ' ', $line;
for my $i (0 .. $#words - 1) {
say "$words[$i] $words[$i 1]";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/489480.html
