Java Doesn't Work with Regex \S, Says: Invalid Escape Sequence

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=\\?";

Regular expression visualization

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



Leave a reply



Submit