Java doesn't work with regex \s, says: invalid escape sequence
You need to escape the slash
start_from = start_from.replaceAll("\\s", "+");
Invalid escape sequence with my Regex
You have some errors in your expression and code.
First of all, you have to escape backslashes with another backslash. Additionally, you are using a character class [...]
, so you if you have [a e aaaa]
this will only match ae
. So, [ + ]
will only match a space or a plus.
You can change your code to this:
private String mathA = "(\\d) [+] (\\d)\\s=\\?";
// or escaping +
private String mathA = "(\\d) \\+ (\\d)\\s=\\?";
Btw, if you want match multiple digits, you can use:
private String mathA = "(\\d+) [+] (\\d+)\\s=\\?";
Why is `\.` not a valid escape sequence in java regex
This is one of the hassles of regular expressions in Java. That \\
is not an escaped backslash at the regex level, just at the string level.
This string:
"^[0-9A-Z\\.]{1,10}$"
Defines this regular expression:
^[0-9A-Z\.]{1,10}$
...because the escape is consumed by the string literal.
Invalid escape sequence for Regular Expression
Your backslashes need to be escaped --
Pattern numberPattern = Pattern.compile("^((\\+){0,1}91(\\s){0,1}(\\-){0,1}(\\s){0,1})?([0-9]{10})$");
this is because Java using the \
character as an escape character, to tell it you really mean \
and not an escape character, you have to write \\
.
Invalid escape sequence (valid ones are \b \t \n \f \r \ \' \ )
This should work ^(?:(?:([01]?\\d|2[0-3]):)?([0-5]?\\d):)?([0-5]?\\d)$
The reason is that the listed symbols in the error message have special meaning, but \d
is not one of those defined special symbols for using \
, this means you have to escape it (by adding an extra \
in front of the symbol).
Java Regex - Invalid Escape Sequence w/ one backslash, Delete These Tokens w/ two
You forgot to escape your double quotes, as such (one escape only): \"
.
Here is your escaped Pattern
(both code and Pattern
compile, but I'm not guaranteeing it does what you want).
Pattern signaturePattern = Pattern.compile("[A-Z0-9_]+[\" \"]+[A-Za-z0-9\\.]+[\" \"]+[A-Za-z0-9\\.]+[\" \"]+[A-Za-z0-9\\.]+[\" \"]+[A-Za-z0-9\\.]+[\" \"]+");
In java I how do I escape a * character
Escape the escape character:
"\\*"
Alternatively, just use replace
, which treats the arguments as literals, not regexes:
a.replace("*", " ")
Or, as Aniket Sahrawat points out, you can use the char
overload in this case:
a.replace('*', ' ')
Invalid escape sequence for regex to match URL in Java
Note that in Java strings you have to escape backslashes (i.e. \
would result in the string literal "\\"
). Thus the expression should look like this:
String expression = "(http|https):\\/\\/(\\w+:{0,1}\\w*@)?(\\S+)(:[0-9]+)?(\\/|\\/([\\w#!:.?+=&%@!\\-\\/]))?";
Invalid escape sequence \d
Java will treat \
inside a string as starting an escape sequence. Make sure you use \\
instead (so that you get an actual \
character in the string) and you should be ok.
Quick Update: As Etienne points out, if you actually want a \
in the RegEx itself, you'll need to use \\\\
, since that will produce \\
in the string, which will produce \
in the RegEx.
New Question Update: You mention that your RegEx doesn't work, and I'm pretty sure that's because it's wrong. If you just want to ensure one of each type of character class is present, you may just want to create one RegEx for each class, and then check the password against each one. Passwords are pretty much guaranteed to be short (and you can actually control that yourself) so the perf hit should be minimal.
Related Topics
Javafx - Border Radius <-> Background Color
Map.Clear() VS New Map:Which One Will Be Better
Change Position of Google Maps API's "My Location" Button
Android Studio Convert Iso String to "America/New_York" When Adding to Event to Calendar
Android Simpledateformat, How to Use It
Android:Change App Label Programmatically
Alertdialog with Custom View: Resize to Wrap the View's Content
Android Studio: Failed to Complete Gradle Execution, Cause Is Empty
How to Create Your Own Library for Android Development to Be Used in Every Program You Write
How to Solve the "Double-Checked Locking Is Broken" Declaration in Java
Jpanel & Components Change Position Automatically
Why Is Exception.Printstacktrace() Considered Bad Practice
How to Read Jpeg Image Using Imageio.Read(File File)
How to Use an Arraylist as a Prepared Statement Parameter
Jersey Rest Web Service with Activemq Middleware Integration