Easy, with regex search!
- Press Ctrl+R for show replace bar
- Enable regex mode (button looks like .* near search field)
- Use regex in search field:
'(.+)'\s=>\s'(.+)',
- Use expression for replace:
'$2' => '$1',
You can see a tooltip with a value that will be after replace
It is possible even you have different quotes and spaces between arrow
Use regex
(?:'|")(.+)(?:'|")\s{0,}=>\s{0,}(?:'|")(.+)(?:'|"),
And the same replace expression '$2' => '$1',
Named capture groups also available:
Use regex
(?:'|")(?<src>.+)(?:'|")\s{0,}=>\s{0,}(?:'|")(?<tgt>.+)(?:'|"),
And the same replace expression '${tgt}' => '${src}',
Top comments (2)
As a test I have tried on vim
OBS: I have used the caret as if the pattern started at the begining of line but we can also cosider the space optional
Another tip: Using neovim we have the "inccommand" that allow us to match our regex icrementally. Then we can refer to our search simply by using
//
"Easy" and "with regex search" are mutually exclusive concepts, and I am saying this as a regex enthusiast who routinely fixes other people's code by replacing naïve matching like
strpos()
withpreg_match()
.