AmberThai has asked for the wisdom of the Perl Monks concerning the following question:

If you have something like :"abc clothes doing shopping at the .mall abc something doing shopping at the .mallabc" why does something like :

use warnings 'all'; use strict; use autodie; open my $input, '<', $ARGV[0]; open my $out, '>', $ARGV[1]; while (<$input>) { my ($before, $between, $after) = /(.*?.clothes|.something +)(.*?)(\.mall.*)/; print {$out} "$before $after"; }

does not read the values :"abc abcabc" and after the explanations can someone tell me how could I do this?

Replies are listed 'Best First'.
Re: Working with files
by Corion (Patriarch) on Jan 18, 2016 at 12:10 UTC

    Read perlre about "greedy" quantifiers. You're using a non-greedy quantifier, which will stop at the first .mall instead of the last.

    As a recommendation, you get much better answers if you make it easy for people to replicate your problem. Instead of reading from a file, simply assign your data to a string:

    my $line = 'abc clothes doing shopping at the .mall abc something doin +g shopping at the .mallabc'; my ($before, $between, $after) = /(.*?.clothes|.something)(.*?)(\.mall +.*)/; print "Matched: '$before', '$after'\n";

    It's also often very good to show what you get and what you expect, ideally also with an explanation why you think you should get what you expect.

    Also, why did you open a new thread? Re^2: Reading from files would likely also have received an answer.

      Matched:'abc clothes','.mall abc something doing shopping at the .mallabc'

      I expected it to match :"abc abcabc"

      And I opened a new thread not knowing if I will get an answer ,sorry :D

        If you change the middle quantifier from non-greedy to greedy, it will try to match the longest possible part.