1

Given a file with multiple lines matching this pattern (apologies for the length!)

$this->execSQL('INSERT INTO `table_lowercase_and_underscores` (`id`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (\'first-guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', 0, \'2018-04-09 03:58:59\', 0);');

I would like to visually select multiple of these lines and apply a macro or regex replacement to yield the following pattern below each target line:

$this->execSQL('DELETE FROM `table_lowercase_and_underscores` WHERE id =  \'first-guuid-lowercase-numbers-hyphens\';');

I know I can capture the first backtick contents with something like

s/`\([a-z_]*\)`

And could grab the first match of \'something'\ into 2 capture groups, but how would I then get those into a new line following the first?

Would this be easier to tackle as a macro to duplicate the line and then replace a few parts?

1 Answer 1

2

I think this can be done with just a :s:

:s/\v.{-}(`.{-}`).{-}(\\'.{-}\\').*/&\r$this->execSQL('DELETE FROM \1 WHERE id = \2;');/

The regex \v.{-}(.{-}).{-}(\\'.{-}\\').*:

  • enables very magic (\v)
  • uses lazy matching instead of greedy ({-})
  • matches the table name in the first group (`.{-}`), and the first GUID in the second (\\'.{-}\\'), by virtue of lazy matching.
  • covers the entire line.

So the replacement:

  • replaces the line with itself, followed by a newline (&\r)
  • and constructs the new query with the two groups.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.