Is it possible to have just one regex which can extract the pattern "Mango-1.whatever.." in any of these 3 conditions (i.e. $dummy). I keep printing everything after this pattern. If I use comma in the end, then it fails to print anything in CASE-2.

CASE-1: my $dummy = "Apple-1.fruit,Banana,Peach,Mango-1.Fruit";
CASE-2: my $dummy = "Mango-1.Fruitssss";
CASE-3: my $dummy = "Apple-1.fruit,Mango-1.Fruits,Orange-1.Fruit,Papaya";

$dummy =~ /(Mango-1.*),/i;
$dummy = $1;

print "$dummy";
  • What exactly do you want as output? The "Mango-1.whatever" -- ending at the next comma, or to the end of the string?
    – Jeff Schaller
    Jun 26, 2019 at 15:44
  • The code you showed doesn't print anything for cases 1 and 2.
    – choroba
    Jun 26, 2019 at 15:45
  • If you want the output Mango-1.whatever.., do printf "%s\n" "Mango-1.whatever..".  If that’s not what you want, please do a better job of explaining what you want.  A table of sample inputs and corresponding expected outputs would be a great addition to your question. … … … Please do not respond in comments; edit your question to make it clearer and more complete. Jun 26, 2019 at 16:08

1 Answer 1


What about

$dummy =~ /(Mango-1[^,]*)/i;

i.e. Mango-1 plus anything that follows up to a comma or end of string.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .