2016-08-30 23 views
0

複数の行に以下の文字列があります。コマンド出力から来ているので、1行にすることはできません。例えばのため:複数の行を含む文字列内の単語を含むものを削除します。

my $myString = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a 
type specimen book. It has survived not only five centuries, but also 
the leap into electronic typesetting, remaining essentially unchanged. 
It was popularised in the 1960s" 

私は単語までを含むものを取り除きたい、私は以下試してみましたが、動作していないよう

「スクランブル」。

if($myString =~ 's/.*(scrambled)//s') 
{ 
    print "Match: <$&>\n"; 
} 

答えて

2
#!/usr/bin/perl 
use strict; 
use warnings; 

my $myString = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a 
type specimen book. It has survived not only five centuries, but also 
the leap into electronic typesetting, remaining essentially unchanged. 
It was popularised in the 1960s"; 

$myString =~ s/.*\bscrambled\b//s; 

print $myString; 

Demo

+0

おかげで、これは...何 'unscrambled'文字列が含まれている場合について –

+2

を働いていますか?私たちは '\ bscrambled \ b'を使います。 – ssr1012

+0

@ ssr1012:正規表現を修正しました。ありがとう。 –

0

落としてみ引用符:その何かを変更しようとせずにリテラルs/.*(scrambled)//sと一致しようとしている引用符で

if($myString =~ s/.*(scrambled)//s)

。引用符がなければ、 "s"の代用品が見えます。

0

使用はpre & postを使用することができます。

$myString =~ s/\bscrambled\b//ig && print $`.$'; 

か、他

if($myString =~ s/\bscrambled\b//s) 
{ 
    print "Pre: <$`>\n"; 
    print "Match: <$&>\n"; 
    print "Post: <$'>\n"; 
} 
関連する問題