How to escape + character in java? - java

How to escape + character while using split function call in java?
split declaration
String[] split(String regularExpression)
thats what i did
services.split("+"); //says dongling metacharacter
services.split("\+"); //illegal escape character in string literal
But it allows to do something like this
String regExpr="+";

Since the + is a regex meta-character (denoting an occurrence of 1 or more times), you will have to escape it with \ (which also has to be escaped because it's a meta-character that's being used when describing the tab character, the new line character(s) \r\n and others), so you have to do:
services.split("\\+");

Java and Regex both have special escape sequences, and both of them begin with \.
Your issue lies in writing a string literal in Java. Java's escape sequences are resolved at compile time, long before the string is passed into your Regex engine for parsing.
The sequence "\+" would throw an error as this is not a valid Java string.
If you want to pass \+ into your Regex engine you have to explicitly let Java know you want to pass in a backslash character using "\\+".
All valid Java escape sequences are as follows:
\t Insert a tab in the text at this point.
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
\f Insert a formfeed in the text at this point.
\' Insert a single quote character in the text at this point.
\" Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.

should be like this :
services.split("\\+");

Related

How do I print \\ in java without one becoming an escape sequence character?

In IntelliJ, my code is something like this,
String m = "\\Hello";
System.out.println(m);
I want to print 2 backslashes but one always becomes an escape sequence. When I add another backslash (total 3 backslashes) it gives me an error "java: illegal escape character"
Escaping characters is accomplished using a special symbol: \. In Java, a backslash combined with a character to be "escaped" is called a control sequence.
List of escaped characters:
\t - tab.
\b - backspace (a step backward in the text or deletion of a single character).
\n - new line.
\r - carriage return. ()
\f - form feed.
\' single quote.
\" double quote.
\\ backslash.
So if you want a single backslash \, you need to escape it like \\. So two backlashes would look like \\\\.
What does your error mean?
When you have a string like: "\\\hello", breaking it down, it means \\, \h, e, l, l, o.
\h is an not a valid escaped character. Which is why your error says java: illegal escape character.
If you want one \ you have to type \\. If you want two \\ repeat it two times ;)
String m = "\\\\Hello";
System.out.println(m);
Try to store 4 backslashes in the string variable
String m = "\\\\Hello"; System.out.println(m);

In Java, why does System.out.println("\" \\"); gives " \ as output

In Java when i run System.out.println("\" \\");
I get output as :
" \
Can you please explain in detail, why this is happening?
Because you escape double quotes ("") with a backslash (\) and also a backslash with a backslash.
backslash is a special character in JAVA and many other programming languages, one of its use is to escape characters in certain situation.
For example:
If you want to print a string containing double quotes like: How are you "Bob" ?
Printing this using System.out.println("How are you "Bob" ?"); will not work because you are closing the quotes just before the word Bob. Therefore, a character was used to deal with such situation so one can print double quotes inside a string:
System.out.println("How are you \"Bob\" ?");
Moreover, since we've agreed above that \ escapes the double quotes, if you want to print a single backslash inside a string, doing this System.out.println("\"); will open the string but will escape the second double quotes which will result in an error because the string was not closed. To fix this, you need to escape the backslash like this: System.out.println("\");
Other interesting uses of \:
\n character to return to a new line
\t character to insert a tab
More about escape character can be found on Wikipedia
System.out.println("\" \");
System.out.println(" --> String Open
\" --> Double Quote character escaped using backslash
\\ --> Backslash itself as a character escaped using backslash
"); --> String Close
will give you output as "\
For the list of escaped characters, You can find that here.
\t Insert a tab in the text at this point.
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
\f Insert a formfeed in the text at this point.
\' Insert a single quote character in the text at this point.
\" Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.
Yes. You are escaping two characters,
String s = "\" \\";
uses the single back-slash to escape first the double quote and then a backslash. So you get,
" \
You might also try
System.out.println(s.length());
Which would tell you "3". Because you have a String of '"', ' ' and '\'
Escape Sequences are explained in The Java Tutorial: Characters, which also allows Unicode characters,
System.out.println("\u03A9");
Will output a one character String that equals
Ω
In addition to comments of my precursors, you can check it in Oracle's Java Tutorial, list of escape sequences.
http://docs.oracle.com/javase/tutorial/java/data/characters.html
This is because when you put a \ before a special character in java, \ tells the JVM that it is not a special character, rather it is a part of a String.
So in your case, when you put a \ before " , it prints a double quote(") and when you again put \\ , it prints a slash (\).
If you want to know more about this, you can go through the inside of Java and how the special characters are handled in java.
Hope it helps.
These are called escape sequences. All the escape sequences start with \ (backward slash) character (e.g., \n, \t etc.,). Here \n, \t has special meaning to Java like line break and tab space respectively. Similarly " (double quote) has a special meaning saying that termination of string literals in Java. Instead of making " as a string literal terminator, we need to tell java compiler to treat it as a special sequence. Hence we use these escape sequences like \\ (for backward slash), \' (single quote), \r (carriage return) etc.,
Thanks,
JK
The standard definition according to oracle is as follows:
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
EscapeSequence Description
\" Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.
By following the above description in our case for System.out.println("\" \\"),
\" would be replaced with a double " quote character and
\\ would be replaced with a (single backslash) \ character.
Hence output printed will be " \
Hope this helps.

How to write regular expressions in eclipse without compile time errors

Eclipse keeps on indicating there is an error in my code when I write a regular expression.
For example,
String regex = "/\((.+)\)/";
This causes eclipse to warn with a red flag:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \'
\ )
How do I change this?
You must escape backslashes
String regex = "/\\((.+)\\)/";
if you want to put backslash within quotes you must use the escape sequence, \\, on the interior quotes to convey that it is part of the String literal and doesn't have any other special meaning
You need to escape all backslashes, so special characters appear "double escaped" - once for the String, once for the regular expression.

JDBC connection string SQL Server with JSP causes "Illegal escape character" error

What would my connection string be if the name of my SQL Server 2012 is JOSEAN\REGULUS ?
"jdbc:sqlserver://JOSEAN\REGULUS;databasename = BD12272"
However, it caused a compilation error:
Illegal escape character
How is this caused and how can I solve it?
Try this instead:
"jdbc:sqlserver://JOSEAN;instanceName=REGULUS;databaseName=BD12272"
The java documentation says:
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:
\t Insert a tab in the text at this point.
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
\f Insert a formfeed in the text at this point.
\' Insert a single quote character in the text at this point.
\" Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.
Your connection string has a backslash, so java expects a escape sequence and \R is not (Illegal escape character).
So, you must escape the backslash and your connection string should look like this:
"jdbc:sqlserver://JOSEAN\\REGULUS;databasename = BD12272"

java regex expression escape characters

Hi i'm trying to split a string separated by vertical bars. for example:
String str = "a=1|b=2";
In java, we should do like this:
str.split("\\|");
If I use a single slash:
str.split("\|");
compiler gives errors:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )
Can anyone explain me why this happens? thanks!
Backslash \ is a special character. In the Java world it is used to escape a character.
The pipe | is a special character in the Regex world, which means "OR".
To use the pipe as a separator you need to escape it(so it can be recognized during the regex parsing), so you need to get this in your regex: \|.
But as backshlash is a special character in Java and that you are using a String object, you have to escape the backslash so it can be interpreted as the final expected final result: \|
To do so, you simply escape backslash with another backslash: \\|
The first backslash escapes the second backslash (java requirement) which escapes the pipe (regex requirement).
In Java strings, a backslash needs to be escaped with another backslash. So, while the "canonical" form of the regex is indeed \|, as a Java string, this must be written "\\|".

Categories

Resources