The following code fragment in Java:
"\\\\".replaceAll("\\\\", "\\");
throws the exception:
java.lang.StringIndexOutOfBoundsException: String index out of range: 1 (NO_SOURCE_FILE:0)
The javadoc on replaceAll does include a caveat on the use of backslashes and recommends using Matcher.replaceAll or Matcher.quoteReplacement. Does anybody have a snippet on how to replace all occurrences of two backslashes in a string with a single backslash ?
clarification
The actual literal shown above is only an example, the actually string can have many occurrences of two consecutive backslashes in different places.
You can simply do it with String#replace(): -
"\\\\".replace("\\\\", "\\")
String#replaceAll takes a regex as parameter. So, you would have to escape the backslash twice. Once for Java and then for Regex. So, the actual replacement using replaceAll would look like: -
"\\\\".replaceAll("\\\\\\\\", "\\\\")
But you don't really need a replaceAll here.
Try this instead:
"\\\\".replaceAll("\\{2}", "\\")
The first parameter to replaceAll() is a regular expression, and {2} indicates that exactly two occurrences of the char must be matched.
If you want to use Matcher.replaeAll() then you want something like this:
Pattern.compile("\\\\\\\\").matcher(input).replaceAll("\\\\");
If you have backslash in replacement string, it will be treated as escape character and the method will try to read the next character.That is why , it is throwing StringIndexOutOfBoundsException.
Related
I want to replace special characters with nothing. So i tried
this.name.replace("[^a-zA-Z]+", "").trim()
I wnat the below word to be 000 Vektor
OOO "Vektor"
The documentation of replace says:
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
It won't take regular expressions.
The documentation of replaceAll says:
Replaces each substring of this string that matches the given regular expression with the given replacement.
So you may use:
this.name.replaceAll("[^a-zA-Z]+", "").trim();
You may also use replaceFirst with regular expressions, though not here.
Also, in a comment you say that you have tried it. I suspect that it is because you just use :
this.name.replaceAll("[^a-zA-Z]+", "").trim();
But java Strings are immutable, and don't change by themselves.
Hence you should use:
this.name = this.name.replaceAll("[^a-zA-Z]+", "").trim();
String.replace takes a literal first argument. replaceAll uses a regex
name = name.replaceAll("[^a-zA-Z ]+", "");
I am trying to strip out certain "<" and ">" from HTML code that is being generated by a 3rd party (of morons)
I am doing a replaceAll for some certain left over conditions that are not being picked up by our ETL people.
I have this string: "<$200" and I need it to be XML compliant like "<$200"
string.replaceAll("<$200","<$200");
does not work. I assume it is some regEx funkyness. What is the correct way to do this?
String#replaceAll accepts a regex as an argument, and not a String. $ is a special character an won't be refereed as a String. Solutions:
Use String#replace instead - It accepts a String and not a regex:
string.replace("<$200","<$200");
Use Pattern#quote - It returns a string representation:
string.replaceAll(Pattern.quote("<$200"),"<$200");
Escape special characters by adding \\ before the special characters.
Use this
String demo ="<$200";
demo = demo.replaceAll("<","<");
System.out.println(demo);
I am having some problem with searching for a special character "(".
I got a java.util.regex.PatternSyntaxException exception has occurred.
It might have something to do with "(" being treated as special character.
I am not very good with pattern expression. Can someone help me properly search for the escape character?
// I need to split the string at the "("
String myString = "Room Temperature (C)";
String splitList[] = myString.split ("("); // i got an exception
// I tried this but got compile error
String splitList[] = myString.split ("\(");
Try one of these:
string.split("\\(");
string.split(Pattern.quote("("));
Since a string split takes a regular expression as an argument, you need to escape characters properly. See Jon Skeet's answer on this here:
The reason you got an exception the first time is because split() takes a regular expression as argument, and ( has a special meaning there, as you suggest. To avoid this, you need to escape it using a \, like you tried.
What you missed, is that you also need to escape your backslashes with an extra \ in Java, meaning you need a total of two:
String splitList[] = myString.split ("\\(");
You need to escape the character via backslashes: string.split("\\(");
( is one of regex special characters. To escape it you can use e.g.
split(Pattern.quote("(")),
split("\\Q(\\E"),
split("\\("),
split("[(]").
I have a list of strings like
xxx_2pathway
xxx_6pathway
xxx_pathway
So I have a string followed by an underscore and "pathway". There may be a digit between the underscore and "pathway". How can I match and replace everything except xxx with a regular expression in Java?
This does not work:
pathnameRaw = pathnameRaw.replace("_\\dpathway","");
Your regex is almost fine. Since the digit is optional, add a ? at the end of \\d.
Also the replace method does not use regex. Use replaceAll instead.
See it
"_[0-9]?pathway"
I have following string
String str = "replace :) :) with some other string";
And I want to replace first occurance of :) with some other string
And I used str.replaceFirst(":)","hi");
it gives following exception
"Unmatched closing ')'"
I tried using replace function but it replaced all occurance of :).
The replaceFirst method takes a regular expression as its first parameter. Since ) is a special character in regular expressions, you must quote it. Try:
str.replaceFirst(":\\)", "hi");
The double backslashes are needed because the double-quoted string also uses backslash as a quote character.
The first argument to replaceFirst() is a regular expression, not just a character sequence. In regular expressions, the parantheses have special significance. You should escape the paranthesis like this:
str = str.replaceFirst(":\\)", "hi");
Apache Jakarta Commons are often the solution for this class of problems. In this case, I would have a look at commons-lang, espacially StringUtils.replaceOnce().