title: Match regexp at the end of the string with AWK
body: <p>I am trying to match two different Regexp to long strings with awk, removing the part of the string that matches in a 35 characters window.
  The problem is that the same bunch of code works when I am looking for the first (which matches at the beginnng) whereas fails to match with the second one (end of string). 
  Input:</p>
  
  <pre><code>Regexp1(1)(2)(3)(4)(5)xxxxxxxxxxxxxxx(20)(21)(22)(23)Regexp2
  </code></pre>
  
  <p>Desired output</p>
  
  <pre><code>(1)(2)(3)(4)(5)xxxxxxxxxxxxxxx(20)(21)(22)(23)
  </code></pre>
  
  <p>So far I used this code that extracts correctly Regexp1, but, unfortunately, is not able to extract also Regexp2 since indexed of RSTART and RLENGTH for Regexp2 are incorrect.
  Code for extracting Regexp1 (correct output):</p>
  
  <pre><code>awk -v F="Regexp1" '{if (match(substr($1,1,35),F)) print   substr($1,RSTART,RLENGTH)}' file
  </code></pre>
  
  <p>Code for extracting Regexp2 (wrong output)</p>
  
  <pre><code>awk -v F="Regexp2" '{if (match(substr($1,length($1)-35,35),F)) print substr($1,RSTART,RLENGTH)}' file
  </code></pre>
  
  <p>Despite the indexes for Regexp1 are correct, for Regexp2 indexes are wrond (RSTART=13). I cannot figure out how to extract the second Regexp.</p>