How do I remove this backslash in Ruby - ruby-on-rails

How do I remove this backslash?
s = "\""
I have tried s.gsub("\\", "") and that doesn't remove it, it returns the same string.

there's actually no backslash character in your String. The Backslash in your example simply escapes the following double quote and prevent's that it would terminate the string and thereby resulting in a syntax error (unterminated double quote ).
So what you see when you print that string in IRB is actually not the backslash as is, but the backslash in combination with the following dobule quote as an indication that the double quote is escaped. Kind of hard to grasp when you encounter it the first time. Have a look at http://en.wikibooks.org/wiki/Ruby_Programming/Strings#Escape_sequences
long story short: there is no backslash in your string so you can't remove it :)

gsub takes a regular expression as the first parameter. I believe that if you pass it a string, it will first convert it into a regex. This means you need extra escaping:
s.gsub("\\\\", "")
If you use regex notation, you can stop it from doubling up:
s.gsub(/\\/, "")
This is because you don't have to escape twice: once because double-quoted strings need you to escape the \ character, and once because the regular expression requires you to as well.

that's actually an escape quote sign (do a print s to see it)

I'm not sure if this is a solution to YOUR problem, but seeing that this is one of the first SO questions I looked at when trying to solve my problem and have in fact, solved it, here is what I did to fix my problem.
So I had some CSV.read output with a load of \ (backslashes) and unwanted quotation marks.
arr_of_arrays = CSV.read("path/to/file.csv")
processed_csv = arr_of_arrs.map {|t| eval(t)}
the key here is the eval() method.

Related

Double \\ in regular expression iOS

Does anyone understand what this (([A-Za-z\\s])+)\\? means?
I wonder why it should be "\\s" and "\\" ?
If I entered "\s", Xcode just doesn't understand and if I entered "\?", it just doesn't match the "?".
I have googled a lot, but I did not find a solution. Anyone knows?
The actual regex is (([A-Za-z\s])+)\?. This matches one or more letters and whitespace characters followed by an question mark. The \ has two different meanings here. In the first instance \s has a fixed meaning and stands for any white space characters. In the second instance the \? means the literal question mark character. The escaping is necessary as the question mark means one or none of the previous otherwise.
You can't type your regex like this in a string literal in C code though. C also does some escaping using the backslash character. For example "\n" is translated to a string containing only a newline character. There are some other escape sequences with special meanings. If the character after the backslash doesn't have a special meaning the backslash is just removed. That means if you want to have a single backspace in your string you have to write two.
So if you wrote your regex string as you wanted you'd get different results as it would be interpreted as (([A-Za-zs])+)? which has a completely different meaning. So when you write a regex in an ObjC (or any other C-based language) string literal you must double all backslash characters.
not sure about ios but same thing happens in java. \ is escape character for java,and c also so when you type \s java reads \ as an escape character.
think of it as if you want to print a \ what will you have to do.
you will have to type \\. now first \ will work as escape character for java and second one will be printed.
I think it should be the same concept for ios too.
so if you want \s you type \s, if you want \ you type \\.
The \s metacharacter is used to find a whitespace character.
Refer this!

How can I enter quotes or apostrophes in neo4j

Trying to enter the following text fails:
MERGE (people:People {Person:'Abe N. O'Sullivan'})
Replacing the apostrophe with a ’ works, but I wonder if there is a more ellegant solution.
Use double quotes, and it will work fine:
MERGE (people:People {Person:"Abe N. O'Sullivan"})
Note if this were the name of a property, you can use backticks to escape the name of a property that has spaces or special characters in it. For text literals, you can surround them with either single or double quotes. If you want to put a quote inside of a text literal quote, you either need to use the other kind of quote to surround the string, or you need to escape it with backslash, I believe.

How to define a ruby array that contains a backslash("\") character?

I want to define an array in ruby in following manner
A = ["\"]
I am stuck here for hours now. Tried several possible combinations of single and double quotes, forward and backward slashes. Alas !!
I have seen this link as well : here
But couldn't understand how to resolve my problem.
Apart from this what I need to do is -
1. Read a file character by character (which I managed to do !)
2. This file contains a "\" character
3. I want to do something if my array A includes this backslash
A.includes?("\")
Any help appreciated !
There are some characters which are special and need to be escaped.
Like when you define a string
str = " this is test string \
and this contains multiline data \
do you understand the backslash meaning here \
it is being used to denote the continuation of line"
In a string defined in a double quotes "", if you need to have a double quote how would you doo that? "\"", this is why when you put a backslash in a string you are telling interpretor you are going to use some special characters and which are escaped by backslash. So when you read a "\" from a file it will be read as "\" this into a ruby string.
char = "\\"
char.length # => 1
I hope this helps ;)
Your issue is not with Array, your question really involves escape sequences for special characters in strings. As the \ character is special, you need to first prepend it (escape it) with a leading backslash, like so.
"\\"
You should also re-read your link and the section on escape sequences.
You can escape backslash with a backslash in double quotes like:
["\\"].include?("\\")

Ruby Regex - Need to replace every occurrence of a character inside a regex match

Here's my string:
mystring = %Q{object1="this is, a testyay', asdkf'asfkd", object2="yo ho', ho"}
I am going to split mystring on commas, therefore I want to (temporarily) sub out the commas that lie in between the escaped quotes.
So, I need to match escaped quote + some characters + one or more commas + escaped quote and then gsub the commas in the matched string.
The regex for gsub I came up with is /(".*?),(.*?")/, and I used it like so:
newstring = mystring.gsub(/(".*?),(.*?")/ , "\\1|TEMPSUBSTITUTESTRING|\\2"), but this only replaces the first comma it finds between the escaped quotes.
How can I make it replace all the commas?
Thanks.
I believe this is one way to achieve the results you are wanting.
newstring = mystring.gsub(/".*?,.*?"/) {|s| s.gsub( ",", "|TEMPSUBSTITUTESTRING|" ) }
It passes the matched string (the quoted part) to the code block which then replaces all of the occurrences of the comma. The initial regex could probably be /".*?"/, but it would likely be less efficient since the code block would be invoked for each quoted string even if it did not have a comma.
Don't bother with all that, just split mystring on this regex:
,(?=(?:[^"]*"[^"]*")*[^"]*$)
The lookahead asserts that the comma is followed by an even number of quotes, meaning it's not inside a quoted value.

Regular expression in Ruby

Could anybody help me make a proper regular expression from a bunch of text in Ruby. I tried a lot but I don't know how to handle variable length titles.
The string will be of format <sometext>title:"<actual_title>"<sometext>. I want to extract actual_title from this string.
I tried /title:"."/ but it doesnt find any matches as it expects a closing quotation after one variable from opening quotation. I couldn't figure how to make it check for variable length of string. Any help is appreciated. Thanks.
. matches any single character. Putting + after a character will match one or more of those characters. So .+ will match one or more characters of any sort. Also, you should put a question mark after it so that it matches the first closing-quotation mark it comes across. So:
/title:"(.+?)"/
The parentheses are necessary if you want to extract the title text that it matched out of there.
/title:"([^"]*)"/
The parentheses create a capturing group. Inside is first a character class. The ^ means it's negated, so it matches any character that's not a ". The * means 0 or more. You can change it to one or more by using + instead of *.
I like /title:"(.+?)"/ because of it's use of lazy matching to stop the .+ consuming all text until the last " on the line is found.
It won't work if the string wraps lines or includes escaped quotes.
In programming languages where you want to be able to include the string deliminator inside a string you usually provide an 'escape' character or sequence.
If your escape character was \ then you could write something like this...
/title:"((?:\\"|[^"])+)"/
This is a railroad diagram. Railroad diagrams show you what order things are parsed... imagine you are a train starting at the left. You consume title:" then \" if you can.. if you can't then you consume not a ". The > means this path is preferred... so you try to loop... if you can't you have to consume a '"' to finish.
I made this with https://regexper.com/#%2Ftitle%3A%22((%3F%3A%5C%5C%22%7C%5B%5E%22%5D)%2B)%22%2F
but there is now a plugin for Atom text editor too that does this.

Resources