title: A non-greedy Perl regular expression
body: <p>I need to write a script which does the following:</p>
  <pre><code>$ cat testdata.txt
  this is my file containing data
  for checking pattern matching with a patt on the back!
  only one line contains the p word.
  
  $ ./mygrep5 pat th testdata.txt
  this is my file containing data
  for checking PATTERN MATCHING WITH a PATT ON THe back!
  only one line contains the p word.
  </code></pre>
  <p>I have been able to print the line which is amended with the &quot;a&quot; capitalized as well. I have no idea how to only take what is needed.</p>
  <p>I have been messing around (below is my script so far) and all I manage to return is the &quot;PATT ON TH&quot; part.</p>
  <pre><code>#!/usr/bin/perl
  
  use strict;
  use warnings;
  use feature 'say';
  use Data::Dump 'pp';
  
  my ($f, $s, $t) = @ARGV;
  my @output_lines;
  
  open(my $fh, '&lt;', $t);
  
  while (my $line = &lt;$fh&gt;) {
      if ($line =~ /$f/ &amp;&amp; $line =~ /$s/) {
          $line =~ s/($f.+?$s)/$1/g;
          my $sub_phrase = uc $1;
          $line =~ s/$1/$sub_phrase/g;
          print $line;
      }
      #else {
      #       print $line;
      #}
  }
  
  close($fh);
  </code></pre>
  <p>which returns: &quot;for checking pattern matching with a PATT ON THe back!&quot;</p>
  <p>How can I fix this problem?</p>