special character in JSON \n and double quote - java

I am having problem when convert string to json object.
\n gives out Unterminated string . If I want to use new line I use \n character, then it occurs the error. Example String: Hello \n this is testing message.
double quote " gives out Expected a ',' or '} Exception. Example String : This is a "TESTING" message.
I use to escape \ to escape those , but still facing the JSON Exception

The \n, intended as "newline", must be encoded as \n in json.
\" is for quoting.
You can open the python shell, write
>>> import json
>>> json.dumps('\n"')
and see the result yourself

I use the StringEscapeUtils.escapeJavaScript() function which make my life easy.
http://commons.apache.org/lang/api/org/apache/commons/lang3/StringEscapeUtils.html

Related

Groovy string parsing by ignoring "$" or any character(ps: no control over input data)

I am trying to replace a particular word say password to ******* from a string which has characters such as $ and \n in Groovy.
I cannot escape them by using \ because i have no control over data i receive and even in the final output i need as it was earlier with $.
i tried str.replaceAll("password","**")
gives:
illegal string body character after dollar sign;
solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" # line 2, column 8.
afdmas$
def str="""hello how
you$
password
doing"""
expected o/p :
hello how
you$
**
doing
It's not clear why you don't have access to the input data because you use string literal in the example and the error you get is from this example as well. In this case you can use Groovy single-quoted strings but they are without interpolation. Or if interpolation is necessary then you can use slashy or dollar slashy strings which additionally don't require backslash escaping:
def str='''"hello how
you$ password doing'''
def str=/"hello how
you$ password doing/
def str=$/"hello how
you$ password doing/$
*Formatting is the same as in OP

Replacing quotes in a Java String only on specific places

We have a String as below.
\config\test\[name="sample"]\identifier["2"]\age["3"]
I need to remove the quotes surrounding the numbers. For example, the above string after replacement should look like below.
\config\test\[name="sample"]\identifier[2]\age[3]
Currently I'm trying with the regex as below
String.replaceAll("\"\\\\d\"", "");
This is replacing the numbers also. Please help to find out a regex for this.
You can use replaceAll with this regex \"(\d+)\" so you can replace the matching of \"(\d+)\" with the capturing group (\d+) :
String str = "\\config\\test\\[name=\"sample\"]\\identifier[\"2\"]\\age[\"3\"]";
str = str.replaceAll("\"(\\d+)\"", "$1");
//----------------------^____^------^^
Output
\config\test\[name="sample"]\identifier[2]\age[3]
regex demo
Take a look about Capturing Groups
We can try doing a blanket replacement of the following pattern:
\["(\d+)"\]
And replacing it with this:
\[$1\]
Note that we specifically target quoted numbers only appearing in square brackets. This minimizes the risk of accidentally doing an unintended replacement.
Code:
String input = "\\config\\test\\[name=\"sample\"]\\identifier[\"2\"]\\age[\"3\"]";
input = input.replaceAll("\\[\"(\\d+)\"\\]", "[$1]");
System.out.println(input);
Output:
\config\test\[name="sample"]\identifier[2]\age[3]
Demo here:
Rextester
You can use:
(?:"(?=\d)|(?<=\d)")
and replace it with nothing == ( "" )
fast test:
echo '\config\test\[name="sample"]\identifier["2"]\age["3"]' | perl -lpe 's/(?:"(?=\d)|(?<=\d)")//g'
the output:
\config\test\[name="sample"]\identifier[2]\age[3]
test2:
echo 'identifier["123"]\age["456"]' | perl -lpe 's/(?:"(?=\d)|(?<=\d)")//g'
the output:
identifier[123]\age[456]
NOTE
if you have only a single double quote " it works fine; otherwise you should add quantifier + for both beginning and end "
test3:
echo '"""""1234234"""""' | perl -lpe 's/(?:"+(?=\d)|(?<=\d)"+)//g'
the output:
1234234

convert string to control characters

I have to replace a string literal with the delimiter coming form mysql resultset.
String str1 = value.replace(" - ", mysqlDelimiterValue);
Here i am passing delimiter into variable mysqlDelimiterValue.
If there is any escape character in the delimiter value then instead of generating that escape character as delimiter into data file it converting that delimiter value into string and writing into data file.
For Example:
My input file record is: "a - b - c - d"
My delimiter is: "\t" (tab delimiter)
Expected output: "a b c d"(delimited by tab)
Actual output: "a\tb\tc\td"
Here my delimiter is dynamic. So i want to make this one as generalized one which support any delimiter.
Please help me... thanks in advance...

Insert " in correct form in a String

I want to to input this link in to the string.
String url=www.test.com;
String link=<a href=url>contact info</a>
How can I write this ?
You will need to do:
String url = "www.test.com";
You can use \ character to indicate that we want to include a special character, and that the next character should be treated differently. \" indicates a double quote character and not the termination of the string.
String link = "contact info";
A character preceded by a backslash is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:
Java Escape Sequences:
For More information check this link
First, let's assume you have:
String url = "www.test.com";
(Note the quotes around the string.)
To create your link string, you'd do this:
String link = "contact info";
// Note ---------------^^-----------^^
To put a " inside a string literal, you put a backslash in front of it. This is called "escaping" the quote.
First have the url value within quotes ,then concat the value in the link string.
String url="www.test.com";
String link="contact info";

quotes in the java regular expressions

I get a String from the JSP, containing [", e.g.
["Bulgaria
I would like to replace all the [" occurrences for [', but I don't know exactly how to do it...
I just tried:
str = str.replaceAll("[\\\"", "['");
with the result
java.util.regex.PatternSyntaxException: Unclosed character class near index 2 [\"
and
html = html.replaceAll("[\"", "['");
with the result
java.util.regex.PatternSyntaxException: Unclosed character class near index 1 [" ^
any help will be appreciated
Try this:
str.replaceAll("\\[\"", "['");
You need \\ to escape in java regex and [ is a special character in java regex, thus the \\ in front of it. " is a special character in strings so you only need one \ to escape it.
"Test[\"".replaceAll("\\[\"", "['"); // Test['

Categories