Home > Blockchain >  How to extract the rest of a string starting with a substring in Perl?
How to extract the rest of a string starting with a substring in Perl?

Time:01-12

Data is something like this

MWF-ZoRyDL50.mp4

I want to extract the rest of the string after the hyphen character (-) so output will be

ZoRyDL50.mp4

It will be awesome if I can pass in a variable substring and the program can just extract the rest of the string after it. So in this example, the input substring is "MWF-"

Do you how to do that in perl? Thanks

my $string = 'MWF-ZoRyDL50.mp4'
my $input = 'MWF-'

$string =~ s/$input.*//;

CodePudding user response:

Get rid of .*, that's matching the rest of the string and removing everything. You also should anchor with ^ since you only want to match $input at the beginning.

And it's a good idea to use the \Q regexp operator in case $input contains any regexp metacharacters, so they'll be matched literally.

$string =~ s/^\Q$input//;
  • Related