I am using java replaceAll() method to escape new line characters
String comment = "ddnfa \n \r \tdnfadsf ' \r t ";
comment = comment.replaceAll("(\\n|\\r|\\t)","\\\\$1");
System.out.println(comment);
But the above code is still inserting new line.
Is there a way to output the comment exactly the same (i.e. with \n and \r instead of inserting new line)?
UPDATE:
I ended up using:
comment = comment.replaceAll("\\n","\\\\n")
.replaceAll("\\r","\\\\r")
.replaceAll("\\t","\\\\t");
You'll have to go one-by-one, since the new-line character U+000A has nothing to do with the two-character escape sequence \n:
comment = comment.replaceAll("\n","\\\\n");
comment = comment.replaceAll("\r","\\\\r");
comment = comment.replaceAll("\t","\\\\t");
you will have to do it character by character:
comment = comment.replaceAll("\n","\\\\n");
comment = comment.replaceAll("\r","\\\\r");
comment = comment.replaceAll("\t","\\\\t");
another solution is to escape the String as a Java String using this function:
comment = org.apache.commons.lang.StringEscapeUtils.escapeJava(comment);
This will make the String look exactly like the String in the Java Code, but it will also show other escape sequences (like \\, \" etc).
But maybe thats exactly what you want
Hard way: using Matcher
String comment = "ddnfa \n \r \tdnfadsf ' \r t ";
Map<String,String> sub = new HashMap<String,String>();
sub.put("\n", "\\\\n");
sub.put("\r", "\\\\r");
sub.put("\t", "\\\\t");
StringBuffer result = new StringBuffer();
Pattern regex = Pattern.compile("\\n|\\r|\\t");
Matcher matcher = regex.matcher(comment);
while (matcher.find()) {
matcher.appendReplacement(result, sub.get(matcher.group()));
}
matcher.appendTail(result);
System.out.println(result.toString());
prints
ddnfa \n \r \tdnfadsf ' \r
Why you dont use Matcher.quoteReplacement(stringToBeReplaced);?
It is a \ problem, simplify like this :
comment = comment.replaceAll("(\n|\r|\t)", "");
output :
ddnfa dnfadsf ' t
Try this..
comment.replaceAll("(\n)|(\r)|(\t)", "\n");
Related
I am new to "REGEX".And this question is training and educational for regex in java.
I try to remove leading new line chars from a string.(I know we can do this by 'trim()',but I do not want use it).I use '^[\r\n]+' as regex,like this:
str = "\r\n\r\ntest";
str.replaceAll("^[\r\n]+", "");
System.out.print(str);
I guess result will be
test
But the result is:
CRLF
CRLF
test
As you can see,leading new line chars do not removed.Also I try '^\s' or '^\s+' as regex,but result was same.Do you know why those regexes do not match leading new line chars?
A String cannot be modified: replaceAll returns the changed string.
str = str.replaceAll(...);
I have a string value which received from input field.
String searchingText = getText();
After i receive a string i search this string. But if string contains \ symbol my search is failed.
I know about special characters and try to replace :
searchingText = searchingText.replaceAll("\\","\\\\");
But it give me error and app was shutdown.
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
After research i founded a regex and try to replace with matcher :
Map<String,String> sub = new HashMap<String,String>();
sub.put("\n", "\\\\n");
sub.put("\r", "\\\\r");
sub.put("\t", "\\\\t");
StringBuffer result = new StringBuffer();
Pattern regex = Pattern.compile("\\n|\\r|\\t");
Matcher matcher = regex.matcher(bodySearchText);
In the end i will want to get a string - searchingText = \\ instead of searchingText = \
Please any solutions.
You should do:
string = string.replaceAll("\\\\", "\\\\\\\\");
Note that in Java, \ is written as \\. So replaceAll will see \\ as \, which is not what you want.
Instead, you can use replace that accepts String and not a regex.
change this
searchingText = searchingText.replaceAll("\\","\\\\");
to
searchingText = searchingText.replaceAll("\\\\","\\\\\\");
the replaceAll() will take \\ as \ .
for more details read here
What I have is a string array that I am creating from a .csv file I am reading. I then want to parse the values I'm going to use for the ' character and replace it with a \' because I am outputting this to a javascript file.
Here's the code I'm using for that:
while ((thisLine = myInput.readLine()) != null) {
String[] line = thisLine.split("\t");
if(line[4].indexOf("'") > -1){
System.out.println(line[4]);
line[4] = line[4].replace("'", "\'");
System.out.println(line[4]);
}
brand.add(line[4]);
}
However this is not working. I am getting the same string back after I do the replace.
Is this because of some issue with the string array?
I appreciate any assistance in this matter.
Try like this:
line[4] = line[4].replace("'", "\\'");
The backslash must be "escaped".
In case of line[4] = line[4].replace("'", "\'"); the part \' is converted to just '
You're falling foul of the fact that "'" is the same as "\'". They're the same string (a single character, just an apostrophe) - the escaping is there to allow a character literal of '\''.
You want:
line[4] = line[4].replace("'", "\\'");
So now you're escaping the backslash, instead of the apostrophe. So you're replacing apostrophe with backslash-then-apostrophe, which is what you wanted.
See JLS section 3.10.6 for details of escaping in character and string literals.
you should add back slash \ something like this
line[4] = line[4].replace("'", "\\'");
because one left slash \ is escape character
Your issue looks like it is an escape issue. Try \\ to replace a single back slash.
I have a code like,
String str = " " ;
while( cond ) {
str = str + "\n" ;
}
Now, I don't know why at the time of printing, the output string is not printing the newline character. However, when I add any other character like ( str = str + "c"), it is printing properly. Can anybody help me, how to solve this problem and why this happening ?
The newline character is considered a control character, which doesn't print a special character to the screen by default.
As an example, try this:
String str = "Hi";
while (cond) {
str += "\n"; // Syntactically equivalent to your code
}
str += "Bye";
System.out.println(str);
Looks like you are trying to run the above code on Windows. Well the line separator or new line is different on Windows ( '\r\n' ) and Unix flavors ('\n').
So, instead of hard coding and using '\n' as new line. Try getting new line from the system like:
String newLine = System.getProperty("line.separator");
String str = " " ;
while( cond ) {
str = str + newLine ;
}
If you really want \n, to get printed, do it like this.
String first = "C:/Mine/Java" + "\\n";
System.out.println(first);
OUTPUT is as follows :
For a good reference as to why is this happening, visit JAVA Tutorials
As referred in that TUTORIAL : A character preceded by a backslash is an escape sequence, and has a special meaning to the compiler. When an escape sequence is encountered in a print statement, the compiler interprets it accordingly
Hope this might help.
Regards
Based on your sample, the only reason it would not show a new line character is that cond is never true and thus the while loop never runs...
I'm looking for a built-in Java functions which for example can convert "\\n" into "\n".
Something like this:
assert parseFunc("\\n") = "\n"
Or do I have to manually search-and-replace all the escaped characters?
You can use StringEscapeUtils.unescapeJava(s) from Apache Commons Lang. It works for all escape sequences, including Unicode characters (i.e. \u1234).
https://commons.apache.org/lang/apidocs/org/apache/commons/lang3/StringEscapeUtils.html#unescapeJava-java.lang.String-
Anthony is 99% right -- since backslash is also a reserved character in regular expressions, it needs to be escaped a second time:
result = myString.replaceAll("\\\\n", "\n");
Just use the strings own replaceAll method.
result = myString.replaceAll("\\n", "\n");
However if you want match all escape sequences then you could use a Matcher. See http://www.regular-expressions.info/java.html for a very basic example of using Matcher.
Pattern p = Pattern.compile("\\(.)");
Matcher m = p.matcher("This is tab \\t and \\n this is on a new line");
StringBuffer sb = new StringBuffer();
while (m.find()) {
String s = m.group(1);
if (s == "n") {s = "\n"; }
else if (s == "t") {s = "\t"; }
m.appendReplacement(sb, s);
}
m.appendTail(sb);
System.out.println(sb.toString());
You just need to make the assignment to s more sophisticated depending on the number and type of escapes you want to handle. (Warning this is air code, I'm not Java developer)
If you don't want to list all possible escaped characters you can delegate this to Properties behaviour
String escapedText="This is tab \\t and \\rthis is on a new line";
Properties prop = new Properties();
prop.load(new StringReader("x=" + escapedText + "\n"));
String decoded = prop.getProperty("x");
System.out.println(decoded);
This handle all possible characters