I want to show Double quotes and Parentheses in nodes.
Without using A[label="Rahul(A)"]. I want to show during Rahul(A) -> B
The only way I know to do that (without label) is by enclosing the node name between double quotes, and escaping double quotes which are part of the node name:
This works:
"Rahul(A)" -> "Rahul \"B\"";
Related
I want to create a function in zshrc for the following command -
node scripts/node_es6.js scripts/small_run_oneoff.js runMiaEventsStatsJob '{"targetDate": "02-01-2018"}'
I want to pass the targetDate as a command line argument. So, I wrote the following function in zshrc -
function mia-events-stats() {
node scripts/node_es6.js scripts/small_run_oneoff.js runMiaEventsStatsJob '{"targetDate": "$1"}'
}
This however does not work. When I execute mia-events-stats 02-01-2018, the targetDate passed to the actual running code is $1.
What am I missing here?
Characters of a string inside single quotes is quoted. Thus, your dollar sign is read as a normal character.
You should replace your single quotes by doubles quotes to let the magic happen, and escape inner double quotes like that:
"{\"targetDate\": \"$1\"}"
If you need your single quotes to be read, simply add them:
"'{\"targetDate\": \"$1\"}'"
Single quotes won't have any effect thanks to doubles quotes.
With this program (cs.exe):
class Program
{
static void Main(string[] args)
{
foreach (var item in args)
{
Console.WriteLine(item);
}
}
}
And run:
> cs.exe go\to\a_path
go\to\a_path
> cs.exe "go\to\a path"
go\to\a path
> cs.exe "go\to\a path\"
go\to\a path"
> cs.exe 'go\to\a path\'
'go\to\a
path\'
That means if your path has a space so you quote it, be very careful NOT to put a trailing \ at the end, otherwise your program
might just not be able to handle it as it incorrectly contains a " at the end. Single quote is even weirder!
PowerShell exhibits a similar behavior but without the difference between single and double quotes.
How do I understand this behavior? What's the underlying rule to evaluate backslash in cmd so this can be explained consistently?
As you are not calling an internal cmd command, but calling an executable file, this behaviour is not caused by cmd but the command line argument parser routines. In windows, programs don't receive a collection/array/set of arguments, but a string with all the arguments and each program tokenizes this string to obtain each element. This is usually done by routines included by the compiler that hides this operation and exposes to the code an easier way to handle arguments.
Documentation for the C Command-Line argument parser states that
Arguments are delimited by white space, which is either a space or a tab.
A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. A quoted
string can be embedded in an argument. Note that the caret (^) is not
recognized as an escape character or delimiter.
A double quotation mark preceded by a backslash, \", is interpreted as a literal double quotation mark (").
Backslashes are interpreted literally, unless they immediately precede a double quotation mark.
If an even number of backslashes is followed by a double quotation mark, then one backslash (\) is placed in the argv array for every
pair of backslashes (\\), and the double quotation mark (") is
interpreted as a string delimiter.
If an odd number of backslashes is followed by a double quotation mark, then one backslash (\) is placed in the argv array for every
pair of backslashes (\\) and the double quotation mark is interpreted
as an escape sequence by the remaining backslash, causing a literal
double quotation mark (") to be placed in argv.
There is also a set of undocumented/non official rules (How Command Line Parameters Are Parsed)
Outside a double quoted block a " starts a double quoted block.
Inside a double quoted block a " followed by a different character (not another ") ends the double quoted block.
Inside a double quoted block a " followed immediately by another " (i.e. "") causes a single " to be added to the output, and the
double quoted block continues.
The .Net argument parsing rules are just a derivation of those rules. If you need a different behaviour, then you should use the Environment.CommandLine property to retrieve the full command line string and write your own parsing code.
I tried the following regex, but it matches all the double quotes:
(?>(?<=(")|))"(?(1)(?!"))
Here is a sample of the text:
"[\"my cars last night\",
\"Burger\",\"Decaf\" shirt\",
\"Mocha\",\"marshmallows\",
\"Coffee Mission\"]"
The pattern I want to match is the double quote between the double quotes in line 2
As a general rule, I would say: no.
Given a string:
\"Burger\" \"Decaf\" shirt\"
How do you decide which \" is superfluous (non-matching)? Is this one after Burger, one after Decaf or one after shirt? Or one before any of these words? I believe the choice is arbitrary.
Although in your particular example it seems that you want all \" that are not adjacent to comma.
These can be found by following regexp:
(?<!^)(?<![,\[])\\"(?![,\]])
We start with \\" (backslash followed by double quote) in the center.
Then we use negative lookahead to discard all matches that are followed by comma or closing square bracket.
Then we use negative lookbehind to discard all matches that happen after comma or opening bracket.
Regexp engine that I have used can't cope with alternation inside lookaround statements. To work around it, I take advantage of the fact that lookarounds are zero-length matches and I prepend negative lookbehind that matches beginning of line at the beginning of expression.
Proof (in perl):
$ cat test
"[\"my cars last night\",
\"Burger\",\"Decaf\" shirt\",
\"Mocha\",\"marshmallows\",
\"Coffee Mission\"]"
$ perl -n -e '$_ =~ s/(?<!^)(?<![,\[])\\"(?![,\]])/|||/g; print $_' test
"[\"my cars last night\",
\"Burger\",\"Decaf||| shirt\",
\"Mocha\",\"marshmallows\",
\"Coffee Mission\"]"
Let's assume that the format of your string must be like this:
["item1", "item2", ... "itemN"]
The way to know if a double quote is a closing double quote is to check if it is followed by a comma or a closing square bracket.
To find a double quote enclosed by double quotes, you must match all well formatted items from the beginning until an unexpected quote.
Example to find the first enclosed quote (if it exists):
(?:"[^"]*",\s*)*+"[^"]*\K"
demo
But this works only for one enclosed quote in all the string and isn't useful if you want to find all of them.
to find all quotes:
(?:\G(?!\A)|(?:\A[^"]*|[^"]*",\s*)(?:"[^"]*",\s*)*+")[^"]*\K"(?!\s*[\],])
demo
I am trying to write a regex in Ruby to test a string such as:
"GET \"anything/here.txt\""
the point is, everything can be in the outer double quote, but all double quotes in the outer double quotes must be escaped by back slash(otherwise it doesnt match). So for example
"GET "anything/here.txt""
this will not be a proper line.
I tried many ways to write the regex but doest work. can anyone help me with this? thank you
You can use positive lookbehind:
/\A"((?<=\\)"|[^"])*"\z/
This does exactly what you asked for: "if a double quote appears inside the outer double quotes without a backslash prefixed, it doesn't match."
Some comments:
\A,\z: These match only at the beginning and end of the string. So the pattern has to match against the whole string, not a part of it.
(?<=): This is the syntax for positive lookbehind; it asserts that a pattern must match directly before the current position. So (?<=\\)" matches "a double quote which is preceded by a backslash".
[^"]: This matches "any character which is not a backslash".
One point about this regex, is that it will match an inner double quote which is preceded by two backslashes. If that is a problem, post a comment and I'll fix it.
If your version of Ruby doesn't have lookbehind, you could do something like:
/\A"(\\.|[^"\\])*"\z/
Note that unlike the first regexp, this one does not count a double backslash as escaping a quote (rather, the first backslash escapes the second one), so "\\"" will not match.
This works:
/"(?<method>[A-Z]*)\s*\\\"(?<file>[^\\"]*)\\""/
See it on Rubular.
Edit:
"(?<method>[A-Z]*)\s(?<content>(\\\"|[a-z\/\.]*)*)"
See it here.
Edit 2: without (? ...) sequence (for Ruby 1.8.6):
"([A-Z]*)\s((\\\"|[a-z\/\.]*)*)"
Rubular here.
Tested this on Rubular successfully:
\"GET \\\".*\\\"\"
Breakdown:
\" - Escape the " for the regex string, meaning the literal character "
GET - Assuming you just want GET than this is explicit
\\" - Escape \ and " to get the literal string \"
.* - 0 or more of any character other than \n
\\"\" - Escapes for the literal \""
I'm not sure a regex is really your best tool here, but if you insist on using one, I recommend thinking of the string as a sequence of tokens: a quote, then a series of things that are either \\, \" or anything that isn't a quote, then a closing quote at the end. So this:
^"(\\\\|\\"|[^"])*"$
I am trying to send a query to SQLite from the command line using bash. I need to escape both single quotes and double quotes, and escape them so that bash does not misinterpret them. Here is a typical query:
select * from contacts where source = "Nancy's notes";
How can I send this query from the command line? The basic syntax is something like this:
sqlite3.bin contacts.db 'select * from contacts where source = "Nancy's notes"'
But in this case, the shell misinterprets either the single or double quotes. I've tried escaping using both double and triple slashes but this doesn't work. I'm befuddled. Any suggestions?
The trouble with MarkusQ's solution is knowing which characters are special inside double quotes - there are quite a lot of them, including back-ticks, dollar-open parenthesis, dollar-variable, etc.
I would suggest it is better to enclose the string inside single quotes; then, each single quote inside the string needs to be replaced by the sequence quote, backslash, quote, quote:
sqlite3.bin contacts.db 'select * from contacts
where source = "Nancy'\''s notes"'
The first quote in the replacement terminates the current single-quoted string; the backslash-quote represents a literal single quote, and the final quote starts a new single-quoted string. Further, this works with Bourne, Korn, Bash and POSIX shells in general. (C Shell and derivatives have more complex rules needing backslashes to escape newlines, and so on.)
If bash is your only problem, enclose the whole thing in double quotes and then escape anything that's special within bash double quotes with a single backslash. E.g.:
sqlite3.bin contacts.db "select * from contacts where source = \"Nancy's notes on making \$\$\$\""
Here I use two single quotes that sqlite interprets as one.
sqlite3.bin contacts.db "select * from contacts where source = 'Nancy''s notes on making \$\$\$'"
foo_double_single_quote=`echo ${foo_with_single_quote} | sed "s/\'/\'\'/"g`
sqlite3 "INSERT INTO bar_table (baz_colname) VALUES ('${foo_double_single_quote}');"
if you have a variable foo_with_single_quote whose contents you want to insert into a DB, you can use sed like so to create a variable that has the ' character duplicated as necessary.