title: Perl Regex - Get offset as line number instead of character position in case of a MATCH
body: <p>I am reading a complete file in a string and then doing a regex match as below:</p>
  
  <pre><code>if($str =~ m/$regex/gc) {
       $offset = $+[0];
  }
  </code></pre>
  
  <p>Using this code, I can capture the position where the last successful match ends.</p>
  
  <p>Now this will give the position as character number. </p>
  
  <p><strong>Is there any way that I can get offset as line number?</strong></p>
  
  <p>What I am doing for now is that I am counting the number of newline characters from beginning of <code>$str</code> upto end <code>$offset</code>. </p>
  
  <p>I want to know is there a direct way to capture line number for a regex match.</p>