I need to replace several lines in a PHP file like the following (this is only an example but it represents the real problem):
<?php
$errors = array(
'error_1',
'error_2',
'error_3',
'error_4',
'error_5',
);
I want to make them an associative array, no problem with that:
:%s/'\(.\+\)',/'\1' => '\1',/g
And I will get the following output:
<?php
$errors = array(
'error_1' => 'error_1',
'error_2' => 'error_2',
'error_3' => 'error_3',
'error_4' => 'error_4',
'error_5' => 'error_5',
);
But the problem is: I want to remove the underscore from the array values (the keys have to contain undescores), is there a way that I can replace underscores for white-spaces before replacing with \1?
That's the output that I want:
<?php
$errors = array(
'error_1' => 'error 1',
'error_2' => 'error 2',
'error_3' => 'error 3',
'error_4' => 'error 4',
'error_5' => 'error 5',
);
error_?submatch, as explained by @Marth; but you should first simplify your original regex, e.g.,%s/\('.\+'\)\zs,/ => \1,/g