Could not able to replace \" with " in html tags using C# - c#-4.0

I want to replace a as using C#. I could not able to achive this using Regex.Replace functions as follos
Regex.Replace(html, "\\"", "\"");
execution this command again produces the original output
Anyone have already faced issue like this,Any help would be of greatly appreciated.
Regards,
Ganesan

first of all "\\""produces a compiler error, since you are just escaping one backslash but not the quote.
you are working with 2 escape mechanisms here, one is from the c# compiler, and another is from the regex interpreter.
Which means:
when you give this C# string as a regex: "\\\"" then after compilation there is a string looking like that \", which is then interpreted by the regex engine, which also uses \ as the escape character. therefor regex will escape ", so your code will replace " with "
so if you now use "\\\\\"", first the c# compiler will make \\" out of that, then the regex engine will make \" out of that (both are using \ as escape character)
now c# has a nice little feature to make such strings easier to write.
if you add an # before your string, \ will no long be the escape character, but now you have to escape " with ""
that means "\\\"" == #"\""" and "\\\\\"" == #"\\"""
so you could write Regex.Replace(html,#"\\""","\"")
which is easier to read then Regex.Replace(html,"\\\\\","\"")
i think i got it right this time :D

Related

String literal in ANTLR4

I'm using antlr4 C++ runtime and I'd like to create a string literal in my lexer definition file. How can I do this?
What I have so far:
V_STRING : '"' ~('\\' | '"')* '"';
I doesn't work with
printf("string literal\n");
but works with
printf("string literal\\n");
I don't want to explicitly escape the new line character.
my assumptions are that antlr interprets the new line character as a regular new line (when reading a file, for example).
Thanks in advance.
It's always a good idea to list out your token stream to see if your Lexer rules really do what you expect. (Look into the tokens option of the TestRig; also, some plugins will show you your tokens)
In your case your rule essentially says that a String is " a " followed by 0 or more characters that are not a \ or a " and then a "".
So, when the Lexer encounters your \, matches the ~('\\\\'|'")* part of the rule and then looks for a " (which it does not find, since the \ is followed by a n), so It won't recognize "string literal\n" as a V_STRING token (it also fails to match "string literal\\n" as well, here, so I'm not quite sure what's going on with the example that "works").
try:
V_STRING: '"' ~["]* '"';
Note: this is a very simple String rule, but it accepts your input. You probably want to examine grammars for other languages to see how you might want to handle strings in your language; there are several approaches (and many of them involve using Lexer modes). You can find examples here)
If you want the "\n" to be treated as a newline, just understand that the parser won't do that for you, you'll just see the characters "" and "n". It'll be up to you to handle encoding the escaped characters (and it's once you try to handle " that it'll get more complicated and you'll need to look into Lexer modes)

How do you add a single backslash to a string in Golfscript?

I'm having a bit of an issue trying to do an ascii art challenge in GS, since it requires you finishing a line with the \ symbol.
The problem is that "\"p breaks the program since it thinks you escaped a quote, and "\\"p prints two backslashes. I've tried string concatenation, removing one character at a time, printing substrings, etcetc - Nothing seems to work!
I need this string to be printed out, so how would this be done?
It seems that the behavior with p is buggy. I'll look for a place to report it.
However, "\\" by itself does not print two backslashes; it prints one.
Here's a test link to prove it.
Output:
\
"\\" creates a string with 1 backslash because strings are escaped. This is the same as languages like Ruby.
p escapes strings, so a string with one backslash will be displayed as two. This is also the same as languages like Ruby.
So if you want to print a single backslash, or print things without the quotes, you need to print unescaped strings. The best way to do this is with implicit IO (anything on the stack that is left over is printed unescaped).
The program
"\\"
Should print
\
You could also use print or puts if you don't want to use implicit IO.

Replace . with / in groovy

I know this is a very specific question by I didn't manage to do the replacement myself, I need to replace this string in groovy:
com.pantest in com/pantest.
I tried this:
groupId =com.pantest
def mavenGroupID = groupId.replaceAll('.','/')
And this is what I get in the output:
echo mavenGroupID is //////////
mavenGroupID is //////////
Is the dot (.) is some kind of special character? I tried to escape it using **** but it also didn't work.
As mentioned in the comments, String.replaceAll is taking regex as the input, so it means you need to escape dot at least, but actually, you have also to escape escaping char- backslash \
(more clues at Regular Expression to match a dot)
so, you can do it like follows:
def test = "aaa.bbb.ccc"
//want to replace ., need to use escape char \, but it needs to be escaped as well , so \\
println test.replaceAll('\\.','/')
Output is as requested aaa/bbb/ccc
replaceAll('\\.','/') is the key

Using multiple delimiters with scanner - Java

I'm trying to use both tabs and newlines as delimiters to read from a .txt file. What I have at the moment is:
Scanner fileScanner = new Scanner(new FileReader("propertys.txt"));
fileScanner.useDelimiter("[\\t\\n]");
I've tried:
fileScanner.useDelimiter("\\t|\\n");
and
fileScanner.useDelimiter("[\\t|\\n]");
I've got no idea what's going wrong, I've searched around a lot and it looks like one of those should be working. Clearly I'm doing something wrong.
fileScanner.useDelimiter("\t|\n");
should work.
If you have two slashes "\n" the first acts as an escape and it won't work right.
For the regular expression used as a parameter in useDelimiter method, you should use newline as \n instead of \\n and tab as \t instead of \\t. From Java Pattern class: http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html.
A part from that, I think you should define you regular expression like, for example, this:
fileScanner.useDelimiter("\\s*[\t\n]\\s*");
to limit strings (\\s) between newline or tab characters.

linux bash replace placeholder with unknown text which can contain any characters

If I want to replace for example the placeholder {{VALUE}} with another string which can contain any characters, what's the best way to do it?
Using sed s/{{VALUE}}/$(value)/g might fail if $(value) contains a slash...
oldValue='{{VALUE}}'
newValue='new/value'
echo "${var//$oldValue/$newValue}"
but oldValue is not a regexp but works like a glob pattern, otherwise :
echo "$var" | sed 's/{{VALUE}}/'"${newValue//\//\/}"'/g'
Sed also works like 's|something|someotherthing|g' (or with other delimiters for that matter), but if you can't control the input string, you'll have to use some function to escape it before passing it to sed..
The question asked basically duplicates How can I escape forward slashes in a user input variable in bash?, Escape a string for sed search pattern, Using sed in a makefile; how to escape variables?, Use slashes in sed replace, and many other questions. “Use a different delimiter” is the usual answer. Pianosaurus's answer and Ben Blank's answer list characters (backslash and ampersand) that need to be escaped in the shell, besides whatever character is used as an alternate delimiter. However, they don't address the quoting-a-quote problem that will occur if your “string which can contain any characters” contains a double quote. The same kind of problem can affect the ${parameter/pattern/string} shell variable expansion mentioned in a previous answer.
Some other questions besides the few mentioned above suggest using awk, and that is usually a good approach to changes that are more complicated than are easy to do with sed. Also consider perl and python. Besides single- and double-quoted strings, python has u'...' unicode quoting, r'...' raw quoting,ur'...' quoting, and triple quoting with ''' or """ delimiters. The question as stated doesn't provide enough context for specific awk/perl/python solutions.

Resources