Does anyone know a way with cgvg to escape quotes? Normally I just use the opposite quote type (single vs double) when I need a quote in the string. But sometimes I need both. Like this:
cg '["'].*something.*["']'
Escaping the enclosed single quote doesn't help:
cg '["\'].*something.*["\']'
nor does a second or third layer of escaping:
cg '["\\'].*something.*["\\']'
cg '["\\\'].*something.*["\\\']'
Thoughts?
Thank you!
Related
I just have a general question.
Is there any difference using single and double quote marks within Lua?
Example:
require('test.lua')
require("test.lua")
When I programmed in PAWN, a language similar to C, single quote marks could be used for characters, but not strings of text, you had to use double quote marks for them.
And if there is no difference, which one is recommended to be used?
This question has most likely been answered already, however I failed to find a topic already answered.
Thank you.
Nope. No difference, except that you can enclose the other inside the ones that you are using.
-- No difference between these
myStr = "Hi!!"
myStr = 'Hi!!'
myStr = [[Hi!!]] -- The 'weird' way to make a string literal IMO...
-- Double quotes enclosed in single quotes
myStr = 'My friend said: "Hi!!"'
-- Single quotes enclosed in double quotes
myStr = "My friend said: 'Hi!!'"
-- Single and double quotes enclosed in double brackets
myStr = [[My friend said: "What's up?"]]
See: Strings in Lua for more information.
There is no difference between the two in Lua, so you can use whichever you like. I tend to differentiate between them semantically:
I use "double quotes" for user-visible strings and text, basically whatever appears on the output or in files
I use 'single quotes' for option-like strings for passing to methods and generally in code, like io.stdout:setvbuf('no')
You can find out about Lua strings easily enough by searching the internet for "lua strings".
Here is one result: http://www.lua.org/pil/2.4.html
You are free to choose your own quote style, as it makes no difference, but you should be consistent so that your code is nicer to read.
As a matter of preference, I would normally use double-quotes because Lua uses C-style escape-codes. Many script languages (Perl, for example) and command shells do make the distinction between single- and double-quoted strings, and it would be nice to be a little consistent with these quasi-standard practices.
I mean that when I see a single-quoted string I have to actually think about what language I'm reading, and whether or not escape-codes are substituted in that string. With double-quoted strings I don't have to think about it, because as a general rule any language that substitutes C-style escape-codes in a string will do that in a double-quoted string.
I'm internationalizing an application and cannot figure out how to declare a translation string that contains both single and double quotes. Here's an example of the en.yml string that I am trying to
en:
my_string: When you're using double quotes, they look like "this"
With the above string, I get the following error:
can not load translations from /vagrant/config/locales/en.yml,
expected it to return a hash, but does not
If there were just double quotes, I would wrap it in single quotes and vise-versa. How do I handle both double and single quotes though?
escaping should be done like this
"When you're using double quotes, they look like \"this\""
Actually I can’t figure out why do you need obsolete typewriter quotes in translation strings. There is 2013 around and we are not stuck to ASCII-7 anymore. The typography rules dictate their demands to use unicode quotation marks.
That’s the best practice ever: map those within 3rd keyboard level (or, eventually, sed your yml):
"When you’re using double quotes, they look like “this”"
With such an approach you’ll never run into troubles with escaping and your clients will definitely say “oh, neat.”
Sorry, if this seems a little bit off-topic, but since the question was about translation strings, I still consider it to be the best solution.
See if this works for you, it works perfectly for me in my spring boot applications where I needed to pass JSON values in:
Using YAML pipe style:
app.json:
values: |
{"key": "value"}
Your case would be:
en:
my_string: |
When you're using double quotes, they look like "this"
Folded style might work too but I haven't tried.
See more here: http://symfony.com/doc/current/components/yaml/yaml_format.html#strings
In ruby using a single quote is faster than double quotes as we can see in the message : Is there a performance gain in using single quotes vs double quotes in ruby?.
So why does everyone use double quotes (or both)???
Some examples :
https://github.com/rails/rails/blob/master/activerecord/test/models/company.rb
https://github.com/sinatra/sinatra/blob/master/test/routing_test.rb
https://github.com/jnicklas/capybara/blob/master/spec/rack_test_spec.rb
(I know, it's not everyone but the majority.)
EDIT 1 : Interpolation is not a reason!
Double quotes are not always with interpolation or anything special like this, in Sinatra :
it "defines HEAD request handlers with HEAD" do
Double quotes allows you to do interpolation : "Number of users : #{#count_user}"
Plus taking a look at the benchmarks, I'd say that at best it doesn't matter, the overhead is very small, and some benchmarks are actually faster with double quotes ...
Double quotes allow for string interpolation, single quotes will not evaluate anything within them while the double quotes will. For example, the double quotes will allow you to have escape characters and single quotes won't!
"he said \n hello"
will result in 2 lines while
'he said \n hello'
will result in a string with '\n' in the middle of it
One reason is that string interpolation is supported in double quotes but not single quotes.
For example:
some_var = 15
"I have #{some_var} cats"
# => "I have 15 cats"
'I have #{some_var} cats"
# => "I have \#{some_var} cats"
I personally prefer single quotes when possible because I think they look cleaner. But string interpolation is one very important reason to use double quotes.
With respect to the performance issue, it's so negligible that it's really a matter of preference rather than performance.
I don't know if you actually read that thread properly but #zetetic actually found that using double quotes is slightly faster. Nonetheless this test was done over 1 million assignments and thus in that regard they'r both basically the same speed.
It all comes down to preference. A lot of people come from languages where single quotation are just not possible and so they are used to double quotations. Its all a matter of preference.
It's been answered and accepted, and some of these are repeating others, but my 2¢ worth.
Using double quotes means that if you ever do want to add something that needs interpolation into a string later, you can just do so without having to change the quotes.
Consistency between strings that do require interpolation and those that don't.
Being able to include all English in a string, eg. "don't", "Jim's" without having to escape it, or change the quotes.
Embedding \ escape chars in strings like \n and \t
I understand the functional difference between single and double quotes in Ruby, but I'm wondering what concrete reasons people have for varying between the two. In my mind it seems like you should just always use a double quote, and not think about it.
A couple rationales that I've read in researching the topic...
Use a single quote unless a double quote is required.
There's a very, very minor performance advantage to a single quotes.
Any other interesting thoughts out there? (Or maybe this is a case of the freedom or Ruby leaving the door open for no One Right Way to do something...)
I usually follow the following rule:
never use double quotes (or %Q or %W) if you don't interpolate
The reason for this is that if you're trying to track down an error or a security bug, you immediately know when looking at the beginning of the string that there cannot possibly any code inside it, therefore the bug cannot be in there.
However, I also follow the following exception to the rule:
use double quotes if they make the code more readable
I.e. I prefer
"It's time"
over
'It\'s time'
%q{It's time}
It is technically true that single quoted strings are infinitesimally faster to parse than double quoted strings, but that's irrelevant because
the program only gets parsed once, during startup, there is no difference in runtime performance
the performance difference really is extremely small
the time taken to parse strings is irrelevant compared to the time taken to parse some of Ruby's crazier syntax
So, the answer to your question is: Yes, there is an advantage, namely that you can spot right away whether or not a string may contain code.
I can think of three reasons to use single quoted strings:
They look cleaner (the reason I use them)
They make it easier to create a string you'd otherwise have to escape ('he said "yes"' vs "he said \"yes\"")
They are slightly more performant.
I would assume using a single-quoted string is faster, since double quotes allow string interpolation, and single-quoted strings do not.
That's the only difference I know of. For that reason, it's probably best to only use a single-quoted string unless you need string interpolation:
num = 59
"I ate #{num} pineapples"`
Well, there are a lot of fuzz about the "performance gain" of single quoted strings vs double quoted strings.
The fact is that it doesn't really matter if you don't interpolate. There are a lot of benchmarks around the web that corroborate that assertion. (Some here at stackoverflow)
Personally, I use double for strings that have interpolation just for the sake of readability. I prefer to see the double quotes when I need them. But in fact there are methods in ruby for interpolating strings other than "double quoting" them:
%q{#{this} doesn't get interpolated}
%Q{#{this} is interpolated}
1.9.2-p290 :004 > x = 3
=> 3
1.9.2-p290 :005 > "#{x}"
=> "3"
1.9.2-p290 :006 > '#{x}'
=> "\#{x}"
In any other case, i prefer single quotes, because it's easier to type and just makes the code less overbloated to my eyes.
Since asking this question I've discovered this unofficial Ruby Style Guide that addresses this, and many many more styling questions I've had floating around in my head. I'd highly recommend checking it out.
I found that when putting variables in a string using #{} did not work in single quotes, but did work in double quotes as below.
comp_filnam and num (integer) are the variables I used to create the file name in the file path:
file_path_1 = "C:/CompanyData/Components/#{comp_filnam}#{num.to_s}.skp"
My regular extression (regex) is still work in progress, and I'm having the following issue with trying to extract some anchor text from a hash of where the element is stored.
My hash looks like:
hash["example"]
=> " Project, Area 1"
My ruby of which is trying to do the extraction of "Project" and "Area 1":
hash["ITA Area"].scan(/<a href=\"(.*)\">(.*)<\/a>/)
Any help would be much appreciated as always.
Your groups are using greedy matching, so it's going to grab as much as it can before, say, a < for the second group. Change the (.*) parts to (.*?) to use possessive matching.
There are loads of posts here on why you should not be using regex to parse html. There are many reasons why... such as, what if there is more than one space between the a and href, etc. It would be ideal to use a tool designed for parsing html.
You will have to exape the backslashes for the backslashes. so something like... \\\\ instead of just \\. It sounds stupid, but I had a similar problem with it.
I'm not entirely sure what your issue is, but the regexp should match. Double quotes " need not be escaped. As mentioned in Dan Breen's answer, you need to use non-greedy matchers if the string is expected to contain more than one possible match.
The canonical SO reason to use a real HTML parser is calmly explained right here.
However, regexen can parse simple snippets without too much trouble.
Update: Aha, the anchor text. That's actually pretty easy:
> s.scan /([^<>]*)<\/a>/
=> [["Project"], ["Area 1"]]