File contains following line:
[assembly: AssemblyVersion("1.0.0.0")]
Bash script that replaces one version to another:
echo "%s/AssemblyVersion\s*\(.*\)/AssemblyVersion(\"$newVersionNumber\")]/g
w
q
" | ex $filePath
The question is why this catch whole line to the end so i have to add ]
at the end of replacement string?
The problem arises because .*
matches all the chars to the end of the line, and \(
and \)
create a capturing group (unlike most of NFA regex engines, Vim regex matches a (
char with an unescaped (
and )
with an unescaped )
in the pattern).
You may use
%s/AssemblyVersion\s*([^()]*)/AssemblyVersion(\"$newVersionNumber\")/g
Here, AssemblyVersion
will match the word, then \s*
will match any 0+ whitespace chars, (
will match a literal (
, [^()]*
will match 0+ chars other than (
and )
, and )
will match a literal )
.
Another regex substitution command you may use is
:%s/AssemblyVersion\s*(\zs[^()]*\ze)/\"$newVersionNumber\"/g
Here, AssemblyVersion\s*(
will match AssemblyVersion
, 0+ whitespaces and (
and \zs
will omit that part from the match, then 0+ chars other than (
and )
will get matched, and then \ze)
will check if there is )
to the right of the current location, but won't add it to the match.
\zs
sets the next character to be the first character of the match. Any text before the \zs
pattern will not be included into the match.
\ze
sets the end of the match. Anything after the \zs
pattern will not be part of the match.