JREPL.bat regex replacing inside quotes - batch-file

Im using JREPL.BAT to find and replace specific instances and my regex I have works for find and replace in VSC code and also in the couple regex editors I've used.
CALL ./framework/config/JREPL.BAT "(Error)+\(([^()]*|\(([^()]*|\([^()]*\))*\))*\)" "Error(\"\")" /f ./dist/index.html /o
so what I'm expecting is it to find any case of
Error("")
or
Error( skjdksjdskd() + "" + )
etc
Find and replace works perfectly but jrepl takes
Error( skjdksjdskd() + "" + )
and changes it to
Error()( skjdksjdskd() + "" + )
does anyone know with more JREPL experience know why its ignoring the quotes and also not replacing the () area?

JREPL is hybrid JScript/batch that uses CSCRIPT - the Windows script host.
CSCRIPT has an inherent limitation that prevents double quote literals from being passed as parameters - there is no CSCRIPT escape sequence that includes a " literal.
To include a " literal in your query string, you can use \x22 instead. All of the standard JScript escape sequences can be used in the query string. By default, escape sequences are not recognized in the replace string.
But you want a quote literal in your replace string. This requires the /XSEQ option so you can use the JREPL extended escape sequence of \q. A significant advantage of this option is you can also use the extended escape sequences in the replace string. You could also use \x22 for both the search and replace strings if you prefer, but I find \q much easier to remember.
You have one other potential problem - the CALL command doubles all quoted carets, so [^()] (any character other than ( or )) becomes [^^()] (any character other than ^, ( or )). This is definitely not what you want. That is the reason I added the \c = ^ extended escape sequence.
So I believe the following will give your expected result:
CALL .\framework\config\JREPL.BAT "(Error)+\(([\c()]*|\(([\c()]*|\([\c()]*\))*\))*\)" "Error(\q\q)" /xseq /f .\dist\index.html /o -
FYI - The effect of the ^ beginning of string anchor is not harmed by caret doubling - you don't need the \c escape sequence for the beginning of string anchor because "^MatchStringBeginning" and "^^MatchStringBeginning" yield identical regex results.
You can get more information about the extended escape sequences by issuing jrepl /?/xseq, or jrepl /??/xseq for paged help.
>jrepl /?/xseq
/XSEQ - Enables extended escape sequences for both Search strings and
Replacement strings, with support for the following sequences:
\\ - Backslash
\b - Backspace
\c - Caret (^)
\f - Formfeed
\n - Newline
\q - Quote (")
\r - Carriage Return
\t - Horizontal Tab
\v - Vertical Tab
\xnn - Extended ASCII byte code expressed as 2 hex digits nn.
The code is mapped to the correct Unicode code point,
depending on the chosen character set. If used within
a Find string, then the input character set is used. If
within a Replacement string, then the output character
set is used. If the selected character set is invalid or
not a single byte character set, then \xnn is treated as
a Unicode code point. Note that extended ASCII character
class ranges like [\xnn-\xnn] should not be used because
the intended range likely does not map to a contiguous
set of Unicode code points - use [\x{nn-mm}] instead.
\x{nn-mm} - A range of extended ASCII byte codes for use within
a regular expression character class expression. The
The min value nn and max value mm are expressed as hex
digits. The range is automatically expanded into the
full set of mapped Unicode code points. The character
set mapping rules are the same as for \xnn.
\x{nn,CharSet} - Same as \xnn, except explicitly uses CharSet
character set mapping.
\x{nn-mm,CharSet} - Same as \x{nn-mm}, except explicitly uses
CharSet character set mapping.
\unnnn - Unicode code point expressed as 4 hex digits nnnn.
\u{N} - Any Unicode code point where N is 1 to 6 hex digits
JREPL automatically creates an XBYTES.DAT file containing all 256
possible byte codes. The XBYTES.DAT file is preferentially created
in "%ALLUSERSPROFILE%\JREPL\" if at all possible. Otherwise the
file is created in "%TEMP%\JREPL\" instead. JREPL uses the file
to establish the correct \xnn byte code mapping for each character
set. Once created, successive runs reuse the same XBYTES.DAT file.
If the file gets corrupted, then use the /XBYTES option to force
creation of a new XBYTES.DAT file. If JREPL cannot create the file
for any reason, then JREPL silently defaults to using pre v7.4
behavior where /XSEQ \xnn is interpreted as Windows-1252. Creation
of XBYTES.DAT requires either CERTUTIL.EXE or ADO. It is possible
that both may be missing from an XP machine.
Without the /XSEQ option, only standard JSCRIPT escape sequences
\\, \b, \f, \n, \r, \t, \v, \xnn, \unnnn are available for the
search strings. And the \xnn sequence represents a unicode
code point, not extended ASCII.
Extended escape sequences are supported even when the /L option
is used. Both Search and Replace support all of the extended
escape sequences if both the /XSEQ and /L options are combined.
Extended escape sequences are not applied to JScript code when
using any of the /Jxxx options. Use the decode() function if
extended escape sequences are needed within the code.
Final Answer for this is to escape the quotes and backslashes as \" AND \\ when using CALL in Webpack-shell-plugin.
'call "./framework/config/JREPL.BAT" \"(Error)\\(([\\c()]*|\\(([\\c()]*|\\([\\c()]*\\))*\\))*\\)\" \"Error(\\q\\q)\" /xseq /f ./dist/index.html /o ./dist/indexFinal.html'

Related

SQL Server SqlPackage variables: are quotes needed around string variable?

When running sqlpackage.exe for deployments, do string variables require quotes around the word? It seems to be running successfully both ways. What is the correct syntax?
Two options shown here:
/v:CompanyName=ABCD
/v:CompanyName="ABCD"
Resource: https://learn.microsoft.com/en-us/sql/tools/sqlpackage/sqlpackage?view=sql-server-ver15
#Jeroen Mostert is right. It's more related to the command line not only the SqlPackage.
If the string variable contains spaces equality signs, slashes, or anything else that would interfere with option syntax, the value must be surrounded in "quotes".
Here is the example blog: https://www.addictivetips.com/windows-tips/enter-file-or-folder-paths-with-spaces-in-command-prompt-on-windows-10/
If all of the following conditions are met, then quote characters on the command line are preserved:
No /S switch (Strip quotes)
Exactly two quote characters
No special characters between the two quote characters, where special is one of: & < >( ) # ^ |
There are one or more whitespace characters between the the two quote characters
The string between the two quote characters is the name of an executable file.
Ref: https://ss64.com/nt/syntax-cmd.html
HTH.

Improperly terminated macro invocation when using # before the replacement text

I am new to C so this question might be dumb. Nevertheless, I was trying to play around with macro.
I was using # to make the replacement text into a quoted string and utilizing the feature that the macro will automatically add escape characters where appropriate.
In VS2019 the following code gives me an error saying improperly terminated macro invocation. Why is that?
#include <stdio.h>
#define toStr(x) #x
main(){
printf(toStr(the "\" is the escape character));
}
As #alinsoar mentioned in their comment, the text string is not properly written. The escape character, '\', escapes the text following according to the rules. The result is that when you have "\" what you have specified is that the quote following the backslash is being escaped which results in the text string not having a terminating quote. See How to print out a slash (/ or \) in C?
To actually print the escape character you need to escape it as in "\\" which will result in a single escape character being printed and the text string is also properly closed with a terminating quote.
See C Preprocessor, Stringify the result of a macro which provides information about the Stringify operator of the C Preprocessor, which is what the # for macro expansion does.
You should review the various rules for the escape character and its usage in C. This list for C++ is pretty much the same as for C. https://en.cppreference.com/w/cpp/language/escape
See as well Explain Backslash in C

What is the utility of escape sequence '\'?

In the below code snippet , how is '\' behaving ?
printf("hii\"); // This line gives error : missing terminating " character
printf("hii\ n"); // This line prints hii n
I am unable to get how this escape sequence is behaving here ,Please explain .
An escape sequence isn't the single \ character; it's that followed by another character. For example, \" is an escape sequence, as is \n. Under some circumstances you can see more than a single character following the backslash all as the same escape code; this has to do with how the characters are represented internally (ASCII or Unicode value) and can be safely ignored for now.
An escape sequence is used to write a character that is inconvenient/impossible to put into the code directly. For example, \" is the escape sequence for a quotation mark. It is like putting a quote inside the string, which you couldn't otherwise do because it would instead close the string literal. Look at the syntax highlighting of your question to see what I mean; most of the first line is considered part of the string, because you never have an unescaped closing quote.
The most common escape sequence is perhaps \n. Unlike with \", it doesn't just produce a literal n in the string; you could do that without an escape. Instead it produces a newline. The code
printf("hii\nthere");
prints
hii
there
to the screen.
The second line of code in your question uses the escape sequence \ (backslash space). Thisis not a standard escape sequence; if you compile with warnings your compiler will probably report that it's ignoring it or something.
(If you want to actually print a backslash to the screen, you need to escape a backslash, using \\)

repl.bat : Using Regular Expressions in the Replace Parameter

I have a question with regards to using the 'repl' batch command, specifically it's replace parameter.
After taking the time to read the documentation... :-)... and some testing, it seems that regular expressions can't be used in the replace parameter.
"type file.txt | repl "Jacob is alive. He lives.\n" "Betty lives.\nGo Betty." M >file.txt.new"
This will do a literal replace using the characters '.' & '\n' rather than inserting a new line. Is it true regular expressions cannot be used in the [replace] parameter of repl.bat? If not, do you know of a way to achieve this behavior? Thanks ahead of time!
Extracted from the repl.bat /? information
M - Multi-line mode. The entire contents of stdin is read and
processed in one pass instead of line by line, thus enabling
search for \n. This also enables preservation of the original
line terminators. If the M option is not present, then every
printed line is termiated with carriage return and line feed.
The M option is incompatible with the A option unless the S
option is also present.
X - Enables extended substitution pattern syntax with support
for the following escape sequences within the Replace string:
\\ - Backslash
\b - Backspace
\f - Formfeed
\n - Newline
\q - Quote
\r - Carriage Return
\t - Horizontal Tab
\v - Vertical Tab
\xnn - Extended ASCII byte code expressed as 2 hex digits
\unnnn - Unicode character expressed as 4 hex digits
So, your repl command options should be MX instead of only M

Why aren't standard escape sequences like \a ,\v working?Why ' works without \,and is \? standard?

Why aren't \a (beep),\v(vertical tab) not working in my program even though they are standard according to the links below?And why is a single quotation mark working even without using it as \'? And finally,is \? an escape character at all as that Microsoft site says,because I use the ? symbol inside a printf() format string without \ and it works fine.
To put it clearly:
Why are \a and \v not working?
Why single quote works without the \ even though \' is an escape sequence?
Is \? an escape sequence?(The link says so but ? works without the \)
http://msdn.microsoft.com/en-us/library/h21280bw(v=vs.80).aspx
http://en.wikipedia.org/wiki/Escape_sequences_in_C
Why are \a and \v not working?
Because the console you’re using doesn’t support them. The compiler does, and produces the correct character code in the output, but the terminal emulator ignores them.
Why single quote works without the \ even though \' is an escape sequence?
Because it’s unnecessary to escape it in strings, you only need to escape it in a char literal. Same for \" for string literal:
"'" vs. '\''
'"' vs. "\""
Is \? an escape sequence? (The link says so but ? works without the \)
The link actually says something different:
Note that … \? specifies a literal question mark in cases where the character sequence would be misinterpreted as a trigraph
It’s only there to avoid ambiguity in cases where the following characters would form a valid trigraph, such as ??= (which is the trigraph for #). If you want to use this sequence in a string, you need to escape the first (or second) ?.
Some of the escape sequences are device specific. So they don't produce the desired on effect on every device. For example, the vertical tab (\v) and form feed (\f) escape sequences do not affect screen output. But they do perform the appropriate printer operations.

Resources