2012-04-18 19 views
1

次の文の単語と単語境界を一致させる方法で、単語の右または左にあるダッシュ、ブラケット、カンマ、完全停止などの単語と一致する必要があります。Perlを使って文中の単語境界と単語を一致させる正規表現ですか?

例:

$str = "The quick brown fox (jump) over the lazy dog yes jumped, fox is quick jump, and jump-up and jump."; 

どのように私はPerlの正規表現を使用して、サンプル文に単語「ジャンプ」が4のoccuranceを一致させることができますか?

注:「ジャンプ」という単語に一致する必要はありません。

答えて

2

ワード境界(「\ bは」) のいずれかで、それの一方の側に 「\ W」を有する2つの文字間のスポットとその反対側には「\ W」(あります順序)、 の最初と最後の虚数を "\ W"と一致する文字列として数えます。

からperldoc perlre > Assertions

foreach($str=~/\b(jump)\b/g){ 
    print "$1\n"; 
} 
4
my @words = $str =~ m{\bjump\b}g; 
print "@words\n"; 
0
print "$_\n" for $str =~ /\bjump\b/g; 
0
my @arr = $string =~ m{\bjump\b}g; 
print "@arr\n"; 
関連する問題