Home > Software design >  How do I match pattern and delete the whole line from multiple lines string
How do I match pattern and delete the whole line from multiple lines string

Time:09-22

I have this multiple lines string

IP 5002

<IP 5002
<

abcbbdbdbd
abcbbdbdbd
abcbbdbdbd
abcbbdbdbd
;

Im trying to match "<IP" and then subsequently delete the whole line so it would look like this

IP 5002

<

abcbbdbdbd
abcbbdbdbd
abcbbdbdbd
abcbbdbdbd
;

Code that I have only delete the <IP but not the whole line :

my $str = "sdasdasd
<IP 5002
<

dadas
dasdasd
dasdsada

";

$str =~ s/<IP.*//; 

print "\n" . $str . "\n";

Can anyone help, search result mostly involves deleting line by not reading it from a FILE and writing to a new file. Im trying to do it from a string.

thanks

CodePudding user response:

You can delete the newline character:

$str =~ s/<IP.*\n//;

If you want to avoid greedy match:

$str =~ s/<IP.*?\n//;
  • Related