Reg Ex shows invalid Escape sequence - java

The following Reg Ex :-
^((((\(\d{3}\))|(\d{3}-))\d{3}-\d{4})|(\+?\d{2}((-| )\d{1,8}){1,5}))(( x| ext)\d{1,5}){0,1}$
when tried to be implemented shows
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
Can anyone help me sort out the issue?

I'm not going to try and parse this monstrous regex, but the error points to a problem with string escaping. You need to double all the backslashes.

Try the following:
^((((\\(\\d{3}\\))|(\\d{3}-))\\d{3}-\\d{4})|(\\+?\\d{2}((-| )\\d{1,8}){1,5}))(( x| ext)\\d{1,5}){0,1}$
In Java strings the \ backslash character starts an escape sequence for a 'special' character. Like \n for newline.
You can escape your strings with tools like this one here if you don't want to do it by hand.

Related

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.

System.setProperty: Invalid Escape Sequence

Im trying to make a reference to a bin.
System.setProperty("mbrola.base", "C:\Users\Name\Desktop\FreeTTS\MBrola Project");
But Im getting this error:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
You want actual backslashes, which are usually part of escape sequences. You must escape the backslashes themselves, with another backslash.
System.setProperty("mbrola.base", "C:\\Users\\Name\\Desktop\\FreeTTS\\MBrola Project");
Yes, because this isn't a valid string literal:
"C:\Users\Name\Desktop\FreeTTS\MBrola Project"
You need to escape the backslashes:
"C:\\Users\\Name\\Desktop\\FreeTTS\\MBrola Project"
The string itself will only have the single backslashes though - you're just escaping it in source code.

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 "\\|".

regular-expression in java

I am using this regular expression in a java file to validate the password.
"^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$"
It's showing the error :
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
Can anybody tell me what mistake I am doing in this?(I don't know anything about regular expressions. I copied it from google.)
In Java string literals you need to escape the backslashes.
"^\\w*(?=\\w*\\d)(?=\\w*[a-z])(?=\\w*[A-Z])\\w*$"
You can also simplify your regular expression by removing the first \\w* as it is not needed.

Java doesn't work with regex \s, says: invalid escape sequence

I want to replace all whitespace characters in a string with a "+" and all "ß" with "ss"... it works well for "ß", but somehow eclipse won't let me use \s for a whitespace.. I tried "\t" instead, but it doesn't work either.. I get the following error:
Invalid escape sequence (valid ones
are \b \t \n \f \r \" \' \ )
this is my code:
try {
String temp1 = from.getText().toString();
start_from = temp1.replaceAll("ß", "ss");
start_from = start_from.replaceAll("\s", "+");
}
why doesn't it work? is it a problem with android, eclipse or what?
thanks in advance!
You need to escape the slash
start_from = start_from.replaceAll("\\s", "+");
The problem is that \ is an escape character in java as well as regex patterns. If you want to match the regex pattern \n, say, and you'd go ahead and write
replaceAll("\n", "+");
The regex pattern would not end up being \n: it would en up being an actual newline, since that's what "\n" means in Java. If you want the pattern to contain a backslash, you'll need to make sure you escape that backslash, so that it is not treated as a special character within the string.
replaceAll("\\s", "+");
You can use the java.util.regex.Pattern class and use something like p = Pattern.compile("\s"); in combination with p.matcher(start_from).replaceAll("+"). Alternatively, just escape your "\s" metacharacter as "\\s".

Categories

Resources