在包含多行的字符串中删除包含该单词的内容

时间:2016-08-30 13:07:14

标签: linux perl shell

我有以下多行的字符串。我不能把它变成一行,因为它来自命令输出。例如:

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"

我想把这些东西去掉,包括“#34; scrambled"

我在下面试过,但似乎没有用。

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

3 个答案:

答案 0 :(得分:2)

id = 2 is test2

Demo

答案 1 :(得分:0)

尝试删除引号:

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

使用引号尝试匹配文字s/.*(scrambled)//s 而不尝试更改任何内容。如果没有引号,将会看到“s”的替代品。

答案 2 :(得分:-1)

  

使用可以使用pre&amp; post

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

或者

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