In Java, \' denotes a single quotation mark (single quote) character, and \" denotes a double quotation mark (double quote) character.
So, String s = "I\'m a human."; works well.
However, String s = "I'm a human." does not make any compile errors, either.
Likewise, char c = '\"'; works, but char c = '"'; also works.
But I need to detect whether the string contains backslash or not:
"abcd'" does not contain backslash
"abcd\'" contains backslash.
I need to distinguish whether the string contains backslash or not.
You can't. The're called escape sequences for a reason. For example, \n once put in a String, cannot match a literal \ against itself. It's gone. All that's left, is a new-line.
Remember \ is used to escape a character. It itself doesn't remain a part of the String.
However, you can check for a literal \ by doing a simple contains like
String s = "abcd\\";
System.out.println(s.contains("\\"));
"abcd\" is not a valid string in java.
Here java treated \" as an escape sequence character("). So, if you want to put a backslash in a string then you need to use \ with escape sequence character.
String "abcd\'" has not contained backslash character. It has an escape sequence character \'.
Escape characters (also called escape sequences or escape codes) in
general are used to signal an alternative interpretation of a series
of characters. In Java, a character preceded by a backslash (\) is an
escape sequence and has special meaning to the java compiler.
When an escape sequence is encountered in a print statement, the
compiler interprets it accordingly. For example, if you want to put
quotes within quotes you must use the escape sequence, \", on the
interior quotes. To print the sentence: She said "Hello!" to me. you
should write:
System.out.println("She said \"Hello!\" to me.");
// Java program to illustrate to find a character
// in the string.
import java.io.*;
public static void main (String[] args)
{
// This is a string in which a character
// to be searched.
String str = "gee\\k";
// Returns index of first occurrence of character.
int firstIndex = str.indexOf('\\');
System.out.println("First occurrence of char '\\'" +
" is found at : " + firstIndex);
}
if(string.contains("\\")){
//TODO do your code here
}
\ is used as for escape sequence in Java.
If you want to print backslash in the string you just have to print "abcd\\".
For your example it would be:
boolean containsBs = "abcd\\".contains("\\");
When you are using Strings you do not need to use the escape character(backslash) for single quotation marks. Likewise when using char you do not need to escape the double quotation mark.
String use double quotation mark while chars use single quotation mark. You need to use the escape character for double quote in Strings and for simple quote in chars.
String ex="I'm an example";
String ex2="My name is \"example\"";
char c='"';
char c2='\'';
If you want to find out if a String contains backslash
String ex="abcd";
String ex2="abcd\\";
ex.contains("\\"); //false
ex.contains("\\"); //true
The first backslash is for escaping and the second is the character.
Related
I want to understand the concept of regular expression in below code:
private static final String SQL_INSERT = "INSERT INTO ${table}(${keys})
VALUES(${values})";
private static final String TABLE_REGEX = "\\$\\{table\\}";
.
.
.
String query = SQL_INSERT.replaceFirst(TABLE_REGEX, "tableName");
The above code is working fine but i would like to understand how. As per my knowledge $ and { symbols should be escaped in java string using backslash but in above string there is no backslash and if I try to add it, it shows error: invalid escape sequence.
Also why the TABLE_REGEX = "\\$\\{table\\}"; contains double backslash?
The $ and { don't need to be escaped in Java string literals in general but in regular expressions they need to be escaped as they have special meaning in regular expressions. The $ matches the end of a line and { is used for matching characters a certain amount of times. To match any of the regular expression special characters themselves these characters need to be escaped. For example A{5} matches AAAAA but A\{5 matches A{5.
To escape something in a regular expression string you use the \. But the backslash in string literals itself needs escaping which is done by another \. That is the String literal "\\{" actually corresponds to the string "\{".
This is why in regular expression string literals you will often encounter multiple backslashes. You might also want to take a look at Pattern.quote(String s) which takes a string and properly escapes all special characters (wrt. Java regular expressions).
Essentially instead of
private static final String TABLE_REGEX = "\\$\\{table\\}";
you could write
private static final String TABLE_REGEX = Pattern.quote("${table}");
In your example SQL_INSERT.replaceFirst(TABLE_REGEX, "tableName"); matches the first occurrence of ${table} in SQL_INSERT and replaces this occurrence with tableName:
String sql = "INSERT INTO ${table}(${keys}) VALUES(${values})".replaceFirst("\\$\\{table\\}", "tableName");
boolean test = sql.equals("INSERT INTO tableName(${keys}) VALUES(${values})");
System.out.println(test); // will print 'true'
$ or "$" is the dollar sign / a string containg it.
\$ is an escaped dollar sign, normally found in raw regex if you want to match the char $ instead of the end of the line
"\\$" is a String containing an escaped \ followed by a normale $. Since you are not writing a raw regex, but the regex is inside a Java String you need to escape the \ so that when the regex interpreter comes along it just sees a normal \ which it then treats as escaping the following $.
"\$" is not valid because from a normal String point of view a $ is nothing special and does not need to / must not be escaped.
i would like to understand how.
It is replacing the first match of the regex "\\$\\{table\\}" in the original string "INSERT INTO ${table}(${keys}) VALUES(${values})" with "tableName".
$ and { symbols should be escaped in java string
using backslash but in above string there is no backslash and if I try
to add it, it shows error: invalid escape sequence.
No, ${} are not escaped in a Java string, why would they?
Also why the TABLE_REGEX = "\$\{table\}"; contains double
backslash?
In Java escaping is done by double backslash because single backslash indicates special character (e.g. \n, \t). It is escaping ${} symbols because these symbols have a special meaning in a regex, so escaping them tells the Java regex engine to treat them literally as those symbols and not their special meaning.
How to validate the given string over the regular expression (XSD Pattern):
xsd pattern:'([a-zA-Z0-9.,;:'+-/()?*[]{}\`´~
]|[!"#%&<>÷=#_$£]|[àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ])*'
I need to validate the string with above pattern whether it matches or not.
I have tried the below code but getting unsupported escape characters error while compiling
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternMatching {
private static Pattern usrNamePtrn = Pattern.compile("([a-zA-Z0-9\.,;:'\+\-/\(\)?\*\[\]\{\}\\`´~ ]|[!"#%&<>÷=#_$£]|[àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ])*");
public static boolean validateUserName(String userName){
Matcher mtch = usrNamePtrn.matcher(userName);
if(mtch.matches()){
return true;
}
return false;
}
public static void main(String a[]){
System.out.println("Is a valid username?"+validateUserName("stephen & john"));
}
}
how to do the above task, in addition to that if the doesn't match with the pattern then that characters need to be displayed.and I am using java 1.6 any suggestions is appreciated
First, the regular expression itself has three mistakes.
Mistake 1:
A backslash is a special character which is used to escape whatever character follows it. Therefore, the sequence
\`
is either identical to a single back-quote, or, depending on the regular expression engine, is an illegal escape sequence. Either way, if the intent was to match a backslash along with all the other characters, it should be written as:
\\`
Mistake 2:
Inside the […] character grouping, a ] must be escaped so it doesn’t signify the end of the grouping. So, [] needs to be written as [\].
Mistake 3:
Inside the […] character grouping, a - indicates a character range, like a-z. The regular expression [+-/] does not mean “plus or hyphen or slash”; it means “any of the characters between plus and slash, inclusive.” Technically, this mistake doesn’t affect the outcome in this particular case, because +-/ is equivalent to those three literal characters plus the comma and period, which both happen to occur earlier in the character grouping anyway. But, in the interest of saying what you mean, the - should be escaped:
+\-/
Second is the matter of turning the regular expression into a Java string.
The backslash and the double-quote are special characters in Java. Obviously, " denotes the start and end of a String literal, so if you want a " inside a String, you must escape it:
\"
This is not related to regular expressions; this just tells the compiler that the String contains a double-quote character. It will be compiled into a single " and that is what the regular expression engine will see.
Finally, there is the matter of backslashes. It just so happens that, while regular expressions use a backslash to escape characters as described above, Java also uses backslashes to escape characters in strings. This means that if you want a literal backslash in a Java String, it must be written in the code as two backslashes:
String s = "\\"; // a String of length 1
Recall from above that we need a regular expression with consecutive backslash characters:
\\`
A Java string containing those three characters would look like this:
String s = "\\\\`"; // a String of length 3
A regular expression allows a backslash almost anywhere; for instance, \% is the same as %. However, Java only allows specific characters to be preceded by a single backslash. \+ is not one of those permitted sequences.
+, (, ), {, and } are not special characters inside a […] grouping, so there is no need to escape them anyway.
So, your code needs to be changed from this:
private static Pattern usrNamePtrn = Pattern.compile("([a-zA-Z0-9\.,;:'\+\-/\(\)?\*\[\]\{\}\\`´~ ]|[!"#%&<>÷=#_$£]|[àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ])*");
to this:
private static Pattern usrNamePtrn = Pattern.compile("([a-zA-Z0-9.,;:'+\\-/()?*\\[\\]{}\\\\`´~ ]|[!\"#%&<>÷=#_$£]|[àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ])*");
This is because " is a special character in Java.
You'll have to substitute " with an escape character i.e. \" and \ with \\ as follows:
private static Pattern usrNamePtrn = Pattern.compile("([a-zA-Z0-9.,;:'+-/()?*[]{}\\`´~ ]|[!\"#%&<>÷=#_$£]|[àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ])*");
Note the change in the pattern below where " and \ have been replaced by \" and \\:
Also, note that this will only fix the Compile Issues. You need to re-check your Regex to see if it works fine.
if I print ' (single quote) in the System.out.println() I can get exact output.
Like :
System.out.println("test'test");
output: test'test
What is the purpose of using \' escape sequence in java?
This also gives me the same output.
System.out.println("test\'test");
output: test'test
pls explain what is the main purpose of \' escape sequence in java
It's for use in character literals:
char c = '\'';
Without that, it would be painful to get a single apostrophe as a char.
This is useful for character literals stored in char.
Imagine you want a character constant to just hold a '. You could do:
public static final char SINGLE_QUOTE = '';
This won't work as it only is an empty character, but we want a single quote. Hence the escape character \'.
public static final char SINGLE_QUOTE = '\'';
If you print it on System.out.println, you'll see the difference.
For an exercise, try and take your exact example and see if you can print a double quote literal " without escaping it. You'll see it's not possible. You will have to escape it with \".
Reference: String literals and Escape Sequences for Character and String Literals.
I am converting unicode characters stored a String into unicode text.
For example, here is a String -
String unicode = "\u0041\u006e\u0064\u0072\u006f\u0069\u0064";
Now from this string, I want to get separate unicode character -
u0041 u006e u0064 u0072 u006f u0069 u0064
So for that, I use the following code -
String[] parts = "\u0041\u006e\u0064\u0072\u006f\u0069\u0064".split("\");
But now since the " after \ is ignored in split("\"), I am getting a error.
How to not ignore a character after \?
The \ character is an escape character. You are getting a syntax error because \" is the escape sequence for placing a " character in a String literal. To place a \ inside a String literal, you need to use \\ (the first \ escapes the special meaning of the second \). So a syntactically correct statement would be:
String[] parts = "\u0041\u006e\u0064\u0072\u006f\u0069\u0064".split("\\");
But that is not going to give you what you want, because the first argument does not contain any \ characters. (Also, the split() method expects a regular expression and \ is not a valid regular expression.) Instead, it contains seven characters with code points U+0041, etc. Perhaps you want:
String[] parts = "\\u0041\\u006e\\u0064\\u0072\\u006f\\u0069\\u0064".split("\\\\");
or perhaps you want
char[] parts = "\u0041\u006e\u0064\u0072\u006f\u0069\u0064".toCharArray();
and you can then convert each element of parts to a Unicode code point string.
You need to escape the backslash. You also need to escape the backslash again because split() treats the string as a regular expression. Use .split("\\\\");
With regular expressions in Java, why I should write "\n" to define a new line character and "\\s" to define whitespace character?
Why does the quantity of backslashes differs?
Java does its own string parsing, converting it from your code to an internal string in memory and before it sends the string to the regex parser.
Java converts the 2 characters \n to a linefeed (ASCII code 0x0A) and the first 2 (!) characters in \\s to a single backslash: \s. Now this string is sent to the regex parser, and since regular expressions recognize their own special escaped characters, it treats the \s as "any whitespace".
At this point, the code \n is already stored as a single character "linefeed", and the regular expression does not process it again.
Since regular expressions also recognize the set \n as "a linefeed", you can also use \\n in your Java string -- Java converts the escaped \\ to a single \, and the regular expression module then finds \n, which (again) gets translated into a linefeed.
A Java string has a certain set of allowed escape sequences, of which "\n" is one, but "\s" is not. A string doesn't understand the regexp shorthand for whitespace. You're probably passing a Java string to the RegExp constructor, so in order to pass "\s" as a string, you have to escape the "\" by doubling it.
\ is special character in many languages (in Java it is special in String or char) or tools like regex.
In String or char it is used to create other special characters which you normally couldn't write. By using \x where x is representation of that special character you are able to create
\t tab
\b backspace
\n newline
\r carriage return
\f formfeed
or to escape other special characters
\' single quote (' is special in char because it represents where char starts and ends, so to actually write ' character you need to escape it and write it as
here we start creating character
| here we end creating character
↓ ↓
'\''
↑↑
here we created literal of '
\" double quote - similarly to \' in char, in String " represents where it starts and ends, so to put " literal into string (to actually be able to write it) you need to escape it
here we start creating String
| here we end creating String
↓ ↓
"\""
↑↑
here we created literal of "
\\ backslash - since \ is special character used to create others special character there has to be a way to un-special it so we could actually print \ as simple literal.
Problem: how to write string representing day\night? If you write it such string in a way "day\night" it will be interpreted asday[newline]ight`.
So in many languages to represent \ literal another \ is added before it to escape it. So String which represent day\night needs to be written as "day\\night" (now \ in \n is escaped so it no longer represents \n - newline - but concatenation of \ and n characters)
In case of regex to represent character class which will accept any whitespace you need to actually pass \s.
But string which will represent \s needs to be written as "\\s" because as mentioned earlier in String \ is special and needs escaping.
If you would write \s as "\s" you would get