title: PHP/REGEX - Changing values of INSERT in sql file
body: <p>I have a sql file that contains 2 million queries, The problem is one the column have incorrect value for each record, So i wanna change the value of INSERT query with the correct one (i have a txt file for fix)</p>
  <p>You may ask why I don't use regular UPDATE query, Well, It cost 80% of my hard I/O for each query (Windows/XAMPP)</p>
  <p><code>threads.sql</code>:</p>
  <pre><code>INSERT INTO `threads` (`id`, `thread_id`, `user_id`, `title`, `body`, `sent_at`, `created_at`, `updated_at`, `category_id`) VALUES
  (1, 69872, 1, 'thread title', 'thread body', '2006-08-21 20:30:00', '2020-05-11 14:30:37', '2020-05-11 14:30:37', 2),
  (2, 358454, 1, 'thread title 2', 'thread body 2', '2020-05-04 19:30:00', '2020-05-11 14:30:41', '2020-05-11 14:30:41', 2),
  (3, 337188, 1, 'thread title 3', 'thread body 3', '2017-03-02 20:30:00', '2020-05-11 14:30:44', '2020-05-11 14:30:44', 2),
  </code></pre>
  <p>The target is <code>thread_id</code> and <code>user_id</code> , only <code>user_id</code> should update with new value look at this text file that i told you which have correct value:</p>
  <p><code>fixes.txt</code> :</p>
  <pre><code>69872 =&gt; 67
  358454 =&gt; 22
  337188 =&gt; 15
  </code></pre>
  <p>It means the <code>thread_id</code> with value of 69872 belongs to a <code>user_id</code> with id of 67, But in sql file this is 1, So i tried this script:</p>
  <pre class="lang-php prettyprint-override"><code>$sql = file_get_contents('threads.sql');
  
  $proper = file('fixes.txt', 2);
  
  foreach($proper as $line){
      $line = explode('=&gt;', $line);
      $threadId = trim($line[0]);
      $userId = trim($line[1]);
      $replaced = preg_replace(&quot;/INSERT INTO `threads` \([\s\S]*?\(\d+, $threadId, (\d+),/&quot;, &quot;$0$userId$2&quot;,$sql);
      $lines .= $replaced;
  }
  
  file_put_content('fixed.sql', $lines);
  </code></pre>
  <p>My regex captures the target but i don't know why the script doesn't work</p>
  <p><a href="https://regex101.com/r/wdvlNi/1" rel="nofollow noreferrer">Demo Regex</a></p>