print("This is 1.\n");print("This is 2.");
print("This is 3.\n");
I would like the above to be formated like (which is just adding in
the newline a print that may not have it, but only if it does not
have it):
print("This is 1.\n");print("This is 2.");
print("This is 3.\n");
I presume you mean that second section to read
print("This is 1.\n");print("This is 2.\n");
print("This is 3.\n");
adding in that "\n" after "2."
On the assumption that you don't have quotation-marks escaped in your
strings such as
print ("She said \"hello\" to me");
you should be able to do something like
%s/print\s*(\s*"[^"]*\(\\n\)\@<!\ze"/&\\n/g
which should catch most of the non-pathological cases. If quotes can
span lines, such as
print ("this is a
really long string")
then you can change
[^"]*
to
\_[^"]*
I just noticed that I also need to support the following as well:
print ("small string");
print (
"This is a very long string");
and I need to format it as so:
print ("small string\n");
print (
"This is a very long string\n");
Ideally, I would like to do this in one command and I would also like to
understand the regex itself. So, given the above, here is what I
understand of the regex pattern:
%s/print\s*(\s*"[^"]*\(\\n\)\@<!\ze"/&\\n/g
% - globally
s - substitute
/ - delimeter
print\s*(\s*" - my phrase to match including zero or more matching
spaces at the end print, then a literal paren then zero or more spaces
up until the quote
[^"]* - then everything that is not a quote (zero or more)
( - The beginning of the group ???
\\n - literal \n
) - End group ????
\@<! - Nothing, requires no match behind ???
" - my ending quote to match in the pattern print ("")
/& - ???
\\n - literal \n
/ - delimeter
g - each occurrence on the line
Then we have the spanning multiple lines option:
\_ [^"]*
so,
\_ - match text over multiple lines (Is this like another regex
engine, like the one sed uses?)
[^"]* - everything but a quote (zero or more)
Does this make since? The area I am having difficulty with is /& and how
the grouping is working.
Thanks in advance,
Sean