I'm attempting to replace all strings in a text file surrounded by single quotes and ending with an @ first followed by any number and then any mix of any other letter(s) or #(s) like these:
'[email protected]'
'otherpackage@14'
'[email protected]'
and I wish to select only the single quotes and remove them like so:
export MYVAR="/dir/'[email protected]':$MYVAR"
to:
export MYVAR="/dir/[email protected]:$MYVAR"
in the entire file. I figured out a way to remove the proceeding single quote and preceding single quote in two separate commands (using vim's zs
and ze
, similar to positive and negative lookarounds):
:%s/'.*@.[0-9]*.*\zs'//g
:%s/\zs'\ze.*@.[0-9]*.*//g
However, I am curious about doing it in a single operation as I want to learn more about using Neovim, and apply the answer to future operations.
I am actually using Neovim, if that comes with any additional features related to this.
You need to use
%s/'\([^@'0-9]*@[0-9][^']*\)'/\1/g
Details:
'
- a single quote\(
- start of a capturing group
[^@'0-9]*
- zero or more chars other than @
, '
and digits@
- a @
char[0-9]
- a digit[^']*
- zero or more chars other than a '
char\)'
- end of the capturing group.The replacement is \1
, the Group 1 backreference/placeholder.