Invalid Escape sequence java [duplicate] - java

This question already has answers here:
Java doesn't work with regex \s, says: invalid escape sequence
(3 answers)
Closed 5 years ago.
I am trying do that but it keeps give me error invalid escape format. I was trying to remove some backslash but didn't work
private string chars = "(lP+" (\\w+)/\\d{3} \\d+ \\w+ \\S+\\s?\"?($|-|[\\w\\\\\.#]+)\"?")";

String should have a capital 'S'
You've missed esacaping a couple of the quotation marks.
You have a run of five backslashes, which should either be 4 or 6.
You probably want something like this instead...
private String chars = "(lP+\" (\\w+)/\\d{3} \\d+ \\w+ \\S+\\s?\"?($|-|[\\w\\\\\\.#]+)\"?\")";

private String chars = "(lP+\" (\\w+)/\\d{3} \\d+ \\w+ \\S+\\s?\"?(\\$|-|[\\w\\\\.#]+)\"?\")";

Related

how to trim specific characters off of Strings on Java [duplicate]

This question already has answers here:
Trim leading or trailing characters from a string?
(6 answers)
Closed 2 years ago.
I'd like to trim any single trailing . or -. I tried doing this by doing something like "f-o.o.".replaceFirst("^(\\.+)[-|.]$", "$0"). The expected string is f-o.o but I'm getting f-o.o.. Thank you.
Your expression has two mistakes:
you put a slash in front of a dot, making it match a literal dot, not just any character
you put | into a character class, so your expression would remove not just . or - at the end of the string, but also |.
Use "f-o.o.".replaceFirst("[-.]$", "")

Using Regex for splitting a string in java [duplicate]

This question already has answers here:
Why does this Java regex cause "illegal escape character" errors?
(7 answers)
Closed 3 years ago.
I want to split a string in java with white spaces. I know that the below line of code does it.
String s[] = str.split("\\\s+");
Here split function takes the regex by which the string must be split. So when I want to split string str through one or more spaces, I should pass \s+ as regex, then why is \\\s+ used?
This will do the split
String s[] = n.split("\\s+");
You don't need a third slash'\' - you get Compile Error.
And first '\' is for escaping the second '\'. Used as an escape character for '\s'.
Like Ismail said, you don't need the third backslash.
In your regex you want to use \s so in Java you also need to escape your backslashes for your tags.
Solution:
Why does this Java regex cause "illegal escape character" errors?

Replace dashes with underscores within Any type of bracket [duplicate]

This question already has answers here:
How to replace dashes with underscores within the square brackets using regex Java
(5 answers)
Closed 7 years ago.
I want to replace dashes with underscores within any bracket in a string.
Example String:
[a]-[a-gamma]+(a-alpha)*{a}-{b-gamma}+[a]
replaceAll=?
output
[a]-[a_gamma]+(a_alpha)*{a}-{b_gamma}+[a]
Try to do this one using lookbehind mechanism in regexp
String input = "[a]-[a-gamma]+(a-alpha)*{a}-{b-gamma}+[a]";
String result = input.replaceAll("-(?![\\[\\{\\(])","_");

How to split by escape character in java? [duplicate]

This question already has answers here:
How to split a java string at backslash
(8 answers)
Closed 7 years ago.
I have the following string:
c:\Users\moises\file
and I'm trying to use:
String fileName = path.split("\\")[3];
but I get this error:
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
How can I split the string with the "\" character
path.split("\\\\");
You need to escape your escapes
You'll need four backslashes in a row.
The regular expression with then consist of two backslashes: that means a literal backslash.
I think, it is because you need to escape four times, try this:
path.split("\\\\");

Java regex ignore '.' symbol [duplicate]

This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 9 years ago.
I'm trying to split a string at every '.' (period), but periods are a symbol used by java regexes. Example code,
String outstr = "Apis dubli hre. Agro duli demmos,".split(".");
I can't escape the period character, so how else do I get Java to ignore it?
Use "\\." instead. Just using . means 'any character'.
I can't escape the period character, so how else do I get Java to ignore it?
You can escape the period character, but you must first consider how the string is interpreted.
In a Java string (that is fed to Pattern.compile(s))...
"." is a regex meaning any character.
"\." is an illegally-escaped string. This won't compile. As a regex in a text editor, however, this is perfectly legitimate, and means a literal dot.
"\\." is a Java string that, once interpreted, becomes the regular expression \., which is again the escaped (literal) dot.
What you want is
String outstr = "Apis dubli hre. Agro duli demmos,".split("\\.");

Categories

Resources