2

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',
);
2
  • 1
    Do they all start with error_? Commented Aug 19, 2015 at 18:42
  • You could use submatch, as explained by @Marth; but you should first simplify your original regex, e.g., %s/\('.\+'\)\zs,/ => \1,/g Commented Aug 19, 2015 at 18:52

2 Answers 2

3

You could use

%s/\v'(.+)'/\="'" . submatch(1) . "' => '" . substitute(submatch(1), "_", " ", "g") . "'" 

\= starts a 'sub-replace-expression' (see :h sub-replace-\=) in which you use submatch(1) to access \1, and substitute() (see :h substitute()) to replace "_" by " " in its second occurrence (change "g" to "" if you only want to substitute the first "_" occurrence in \1).


Capturing the surrounding single quotes in \1 makes the replace part a bit easier to read :

:%s/\v('.+')/\=submatch(1) . " => " . substitute(submatch(1), "_", " ", "g") 

Of course if the pattern is simple enough, Two-Bit Alchemist's solution is probably faster/easier to use/remember.

Sign up to request clarification or add additional context in comments.

4 Comments

wouldn't it be better if you put a g flag?
@HuStmpHrrr: It doesn't appear to be necessary in OP's question (also the real reason is that I've had gdefault on for so long that I pretty much always forget about it when I answer a question).
I just had to add the 'g' flag and then it worked like a charm. The actual strings are a little more complex than error_, like: "error_cannot_read_file_from_provided_path". Thank you so much!
@user2937998: I'm guessing you mean the g flag in the substitute() call, which I just realized is probably what @HuStmpHrrr meant too. I'll edit my answer and add it to it.
2

If they all start with error_, just use that:

%s/'error_\(\d\)'/'error_\1' => 'error \1'/g

Otherwise if all you can work with is the underscore, you'll just have to define a pattern for that as well and capture 2 groups:

%s/'\(\w\+\)_\(\d\)'/'\1_\2' => '\1 \2'/g

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.