Chamara Keragala
Chamara Keragala

Reputation: 5777

Remove the stuff up to and including the word in a string with multiple lines

I have the following string with multiple lines. I cannot make it one line since it is coming from a command output. for e.g:

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"

I want to strip off the stuff up to and including the word "scrambled"

I have tried below but does not seem to work.

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

Upvotes: -1

Views: 52

Answers (3)

Chankey Pathak
Chankey Pathak

Reputation: 21666

#!/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

Upvotes: 2

user6572950
user6572950

Reputation:

Use can use pre & post.

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

or else

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

Upvotes: -1

Gilbert
Gilbert

Reputation: 3776

Try dropping quotes:

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

With the quotes its trying to match the literal s/.*(scrambled)//s without trying to change anything. Without the quotes the "s" substitute will be seen.

Upvotes: 0

Related Questions