How do I remove \" from a string?
Example:
'"\"asdasd"\"'.gsub('\"', '') # => "\"asdasd\""
Why is \" not removed?
It is removed. The \" in the output is not a backslash followed by a double quote, it's just a double quote character that's escaped because inspect prints strings in double quotes. If you try to print the string, it'll come out as:
"asdasd"
To expand on this a bit: '"\"asdasd"\"' (which can also be written using double quotes as "\"\\\"asdasd\"\\\"") is a string that contains a double quote, followed by a backslash, followed by a double quote, followed by asdasd, followed by a double quote, followed by a backslash, followed by a double quote.
Your call to gsub removes the two occurrences of backslashes followed by double quotes. The result is "\"asdasd\"", which could also be written as '"asdasd"' and is a string containing a double quote, followed by asdasd, followed by a double quote. So the backslash-double quotes were removed, but the simple double quotes weren't. I assume that's the intended behavior.
In ruby special symbol is preceding by backslash when convert into string.
i.e "\" to the \"\\\"
Check this you will understand
'"\"asdasd"\"' # => "\"\\\"asdasd\"\\\""
'"\"asdasd"\"'.gsub("\\", '') # => "\"\"asdasd\"\""
'"\"asdasd"\"'.gsub("\\", '').gsub("\"", '') # => "asdasd"
How is this ?
str = '"\"asdasd"\"'
p str[/\w+/] # => "asdasd"
It did remove them. Here's what the string looks like when inspected before the change:
'"\"asdasd"\"' # => "\"\\\"asdasd\"\\\""
And here's after:
'"\"asdasd"\"'.gsub('\"','') # => "\"asdasd\""
So it previously had some backslash-quotation mark sequences in it. Now it just has quotation marks.
Related
Is there a way to convert a hash, possibly nested:
{:event=>"subscribe", :channel=>"data_channel", :parameters=>{:api_key=>"XXX", :sign=>"YYY"}}
into a string in specified format as below?
"{'event':'subscribe', 'channel':'data_channel', 'parameters': {'api_key':'XXX', 'sign':'YYY'}}"
EDIT
The format reminds JSON, but practically is not due to single quotes.
Make JSON, then fix it up:
require 'json'
hash = {:event=>"subscribe", :channel=>"data_channel",
:parameters=>{:api_key=>"XXX", :sign=>%q{Miles "Chief" O'Brien}}}
puts hash.to_json.gsub(/"((?:\\[\"]|[^\"])*)"/) { |x|
%Q{'#{$1.gsub(/'|\\"/, ?' => %q{\'}, %q{\\"} => ?")}'}
}
# => {'event':'subscribe','channel':'data_channel',
# 'parameters':{'api_key':'XXX','sign':'Miles "Chief" O\'Brien'}}
EDIT: The first regex says: match a double quote, then a sequence of either escaped double quotes/backslashes, or non-double-quote/backslash characters, then a double quote again. This makes sure we only find strings, and not accidental half-strings like "Miles \". For each such string, we surround the bit that was inside the double quotes ($1) with single quotes, and run a sub-replacement on it that will find escaped double quotes and unescaped single quotes, unescape the former and escape the latter.
Also, sorry about wonky highlighting, seems StackOverflow syntax highlighter can't deal with alternate forms of Ruby quoting, but they're so convenient when you're working with quote characters...
Your desired output looks like a JSON. Try
require 'json'
JSON.dump(hash)
=> "{\"event\":\"subscribe\",\"channel\":\"data_channel\",\"parameters\":{\"api_key\":\"XXX\",\"sign\":\"YYY\"}}"
To have single quotes you can try something like:
JSON.dump(hash).gsub('"', '\'')
It returns:
{'event':'subscribe','channel':'data_channel','parameters':{'api_key':'XXX','sign':'YYY'}}
I have this string:
str = "no,\"contact_last_name\",\"token\""
=> "no,\"contact_last_name\",\"token\""
I want to remove the escaped double quoted string character \". I use gsub:
result = str.gsub('\\"','')
=> "no,\"contact_last_name\",\"token\""
It appears that the string has not substituted the double quote escape characters in the string.
Why am I trying to do this? I have this csv file:
no,"contact_last_name","token",company,urbanization,sec-"property_address","property_address",city-state-zip,ase,oel,presorttrayid,presortdate,imbno,encodedimbno,fca,"property_city","property_state","property_zip"
1,MARIE A JEANTY,1083123,,,,17 SW 6TH AVE,DANIA BEACH FL 33004-3260,Electronic Service Requested,,T00215,12/14/2016,00-314-901373799-105112-33004-3260-17,TATTTADTATTDDDTTFDDFATFTDDDTTFADTTDFAAADDATDAATTFDTDFTTAFFTTATFFF,017,DANIA BEACH,FL, 33004-3260
When I try to open it with CSV, I get the following error:
CSV.foreach(path, headers: true) do |row|
end
CSV::MalformedCSVError: Illegal quoting in line 1.
Once I removed those double quoted strings in the first row (the header), the error went away. So I am trying to remove those double quoted strings before I run it through CSV:
file = File.open "file.csv"
contents = file.read
"no,\"contact_last_name\",\"token\" ... "
contents.gsub!('\\"','')
So again my question is why is gsub not removing the specified characters? Note that this actuall does work:
contents.gsub /"/, ""
as if the string is ignoring the \ character.
There is no escaped double quote in this string:
"no,\"contact_last_name\",\"token\""
The interpreter recognizes the text above as a string because it is enclosed in double quotes. And because of the same reason, the double quotes embedded in the string must be escaped; otherwise they signal the end of the string.
The enclosing double quote characters are part of the language, not part of the string. The use of backslash (\) as an escape character is also the language's way to put inside a string characters that otherwise have special meaning (double quotes f.e.).
The actual string stored in the str variable is:
no,"contact_last_name","token"
You can check this for yourself if you tell the interpreter to put the string on screen (puts str).
To answer the issue from the question's title, all your efforts to substitute escaped characters string were in vain just because the string doesn't contain the character sequences you tried to find and replace.
And the actual problem is that the CSV file is malformed. The 6th value on the first row (sec-"property_address") doesn't follow the format of a correctly encoded CSV file.
It should read either sec-property_address or "sec-property_address"; i.e. the value should be either not enclosed in quotes at all or completely enclosed in quotes. Having it partially enclosed in quotes confuses the Ruby's CSV parser.
The string looks fine; You're not understanding what you're seeing. Meditate on this:
"no,\"contact_last_name\",\"token\"" # => "no,\"contact_last_name\",\"token\""
'no,"contact_last_name","token"' # => "no,\"contact_last_name\",\"token\""
%q[no,"contact_last_name","token"] # => "no,\"contact_last_name\",\"token\""
%Q#no,"contact_last_name","token"# # => "no,\"contact_last_name\",\"token\""
When looking at a string that is delimited by double-quotes, it's necessary to escape certain characters, such as embedded double-quotes. Ruby, along with many other languages, has multiple ways of defining a string to remove that need.
I need to escape the character " by replacing it in vbs
I write
str=8505usafromTo^1c0"ma
str = replace(str,chr(34),"""")
But it seems like " does not escape for the string str
Please, what's wrong and someone could help me fix that?
Thanks
String literals need double quotes:
str = "8505usafromTo^1c0 ma"
To escape a double quote in a string literal, use "" (double double quotes)
str = "8505usafromTo^1c0""ma"
It makes no sense to a replace double quotes (Chr(34)) in a string with double quotes ("""").
Update:
If you .ReadAll()/.ReadLine() a string from a file and want to change the " in that string, use
str = Replace(str, """", "that's what I want to see instead of each double quote")
In case you want "" (double double quotes) as replacements, you need """""" (2 delimiters and two times two double quotes).
Given a string in between quotations, as such "Hello"
The following regular expression will print out a match of the string without the double quotations:
/"([^"]+)"/
I don't understand how it is capturing the characters. I believe what this should be capturing is just the initial double quote. What this regular expression is saying is find an expression that starts and ends with double quotes and again has one or more double quotes at the beginning. And it captures that one or more double quotes at the beginning. How does it end up matching the string here with [^"]+ ?
The expression [^"]+ means literally to match all characters which are not the double quote ". So when placed inside (), all characters following the first " and up to the next " are captured. This is because the ^ inside a [] character class implies negation rather than the start of a string as it would mean outside the []. So [^"] literally means anything but a ".
The () itself is the capture group, and the regex will only capture the expression which exists inside (). Depending on the programming language you use, it may also record the entire string matched "Hello" by the entire expression /"([^"]+)"/ in a separate variable, but the purpose of () is to capture its contents.
Full breakdown of the expression:
" - first literal quote
( - begin capture
[^"]+ all subsequent characters up to but not including "
) - end capture group
" - final closing quote literal
I am trying to construct a regex to find a string in ruby
str = "foo"
I want to be able to stop trying to find the string after it finds the closing quotation mark. I also want to keep the quotation marks so I can output the string I found as:
puts "the string is:" + str
=> the string is: "foo"
I am pretty new to using regular expressions.
Here is a start:
/".*?"/
Explanation:
" Match a literal double quote.
.*? Match any characters, as few as possible (non-greedy)
" Match a second literal double quote.
Rubular
Note that this won't work if the string contains escaped quotes or is quoted with single quotes.