This question already has answers here:
String replace method is not replacing characters
(5 answers)
Closed 6 years ago.
Assuming I have a String string like this:
"abcd=0; efgh=1"
and I want to replace "abcd" by "dddd". I have tried to do such thing:
string.replaceAll("abcd","dddd");
It does not work. Any suggestions?
EDIT:
To be more specific, I am working in Java and I am trying to parse the HTML document, concretely the content between <script> tags. I have already found a way how to parse this content into a string:
if(tag instanceof ScriptTag){
if(((ScriptTag) tag).getStringText().contains("DataVideo")){
String tagText = ((ScriptTag)tag).getStringText();
}
}
Now I have to find a way how to replace one substring by another one.
You need to use return value of replaceAll() method. replaceAll() does not replace the characters in the current string, it returns a new string with replacement.
String objects are immutable, their values cannot be changed after they are created.
You may use replace() instead of replaceAll() if you don't need regex.
String str = "abcd=0; efgh=1";
String replacedStr = str.replaceAll("abcd", "dddd");
System.out.println(str);
System.out.println(replacedStr);
outputs
abcd=0; efgh=1
dddd=0; efgh=1
2 things you should note:
Strings in Java are immutable to so you need to store return value of thereplace method call in another String.
You don't really need a regex here, just a simple call to String#replace(String) will do the job.
So just use this code:
String replaced = string.replace("abcd", "dddd");
You need to create the variable to assign the new value to, like this:
String str = string.replaceAll("abcd","dddd");
By regex i think this is java, the method replaceAll() returns a new String with the substrings replaced, so try this:
String teste = "abcd=0; efgh=1";
String teste2 = teste.replaceAll("abcd", "dddd");
System.out.println(teste2);
Output:
dddd=0; efgh=1
Note that backslashes (\) and dollar signs ($) in the replacement
string may cause the results to be different than if it were being
treated as a literal replacement string; see
Matcher.replaceAll.
Use
Matcher.quoteReplacement(java.lang.String)
to suppress the special meaning of these characters, if desired.
from javadoc.
You are probably not assigning it after doing the replacement or replacing the wrong thing.
Try :
String haystack = "abcd=0; efgh=1";
String result = haystack.replaceAll("abcd","dddd");
Related
I am trying to replace special character }} in a string with "" using regexp in Java, I tired the below two methods and it doesn't work. Please let me know what is wrong with these statements.
Note the string would also contain } which I would like to retain. Goal is to replace only }}.
Method 1:
String buffer = obj.toJSONString() + ",";
String result = buffer.replaceAll(Pattern.quote("(?<![\\w\\d])}}(?![\\w\\d])"), "");
Method 2:
Pattern.compile("(?<![\\w\\d])}}(?![\\w\\d])").matcher(buffer).replaceAll("");
The quote in the following:
String result = buffer.replaceAll(Pattern.quote("(?<![\\w\\d])}}(?![\\w\\d])"), "");
says to treat the regex as a literal string. That's wrong.
If you simply want to remove all }} irrespective of context:
String result = buffer.replaceAll(Pattern.quote("}}"), "");
If you do need to respect the context, don't Pattern.quote(...) the regex!
The other problem is in the way that you attempt to specify the character classes. Since \d is a subset of \w, it is unnecessary to combine them. Just do this instead:
String result = buffer.replaceAll("(?<!\\w)\\}\\}(?!\\w)"), "");
I'm not sure if it is strictly necessary to quote the } characters, but it is harmless if it is not necessary.
Dont' use Pattern.quote, use a literal regex pattern, and escape the brackets:
Stringbuffer = obj.toJSONString() + ",";
String result = buffer.replaceAll("(?<![\\w\\d])\\}\\}(?![\\w\\d])", "");
Using Pattern.quote tells the regex engine to treat the string as literal. This does mean the brackets would not have to be escaped, but it would also render your lookarounds as literal text, probably not what you have in mind.
The method 2 still needs to escape special characters }
Pattern.compile("(?<![\\w\\d])\\}\\}(?![\\w\\d])").matcher(buffer).replaceAll("");
Can you please try same with Apache StringUtils. It’s faster and should work in your case. Kindly find following links for reference.
apache-stringutils-vs-java-implementation-of-replace
Apache StringUtils 3.6
This question already has answers here:
String.split() *not* on regular expression?
(8 answers)
Closed 2 years ago.
In my Java application I need to find indices and split strings using the same "target" for both occasions. The target is simply a dot.
Finding indices (by indexOf and lastIndexOf) does not use regex, so
String target = ".";
String someString = "123.456";
int index = someString.indexOf(target); // index == 3
gives me the index I need.
However, I also want to use this "target" to split some strings. But now the target string is interpreted as a regex string. So I can't use the same target string as before when I want to split a string...
String target = ".";
String someString = "123.456";
String[] someStringSplit = someString.split(target); // someStringSplit is an empty array
So I need either of the following:
A way to split into an array by a non-regex target
A way to "convert" a non-regex target string into a regex string
Can someone help? Would you agree that it seems a bit odd of the standard java platform to use regex for "split" while not using regex for "indexOf"?
You need to escape your "target" in order to use it as a regex.
Try
String[] someStringSplit = someString.split(Pattern.quote(target));
and let me know if that helps.
String::split do split without regex if the regex is:
a one-char String and this character is not one of the RegEx's meta characters .$|()[{^?*+\\
two-char String and the first char is the backslash and the second is
not the ascii digit or ascii letter.
Please see String::split() source code for details.
For escaped '.' target it is going to be split without regex.
You can try this one.
String target = ".";
String someString = "123.456";
StringTokenizer tokenValue = new StringTokenizer(someString, target);
while (tokenValue.hasMoreTokens()) {
System.out.println(tokenValue.nextToken());
}
Is it possible to split a string using a string as delimiter? If yes, how?
Example :
String myString = "hello_world<;>goodbye<;>foo";
myString.split("<;>");
//returns "hello_world", "goodbye" and "foo"
The example in your question works exactly, however this may be coincidental. Keep in mind that the String.split(...) method accepts a RegEx parameter, not a String delimiter.
Check out the RegEx documentation here: http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#sum
yes, exactly as you have in your code.
This question already has answers here:
Replace a question mark (?) with (\\?)
(2 answers)
replace String with another in java
(7 answers)
Closed 9 years ago.
is it possible to replace all the questionmarks ("?") with "\?" ?
Lets say I have a String, and I want to delete some parts of that String, one part with an URL in it. Like this:
String longstring = "..."; //This is the long String
String replacestring = "http://test.com/awesomeness.asp?page=27";
longstring.replaceAll(replacestring, "");
But! As I understand it you can't use the replaceAll() method with a String that contains one single questionmark, you have to make them like this "\?" first.
So the question is; Is there some way to replace questionmarks with "\?" in a String? And no, I'm able to just change the String.
Thanks in advance, hope someone understands me! (Sorry for bad English...)
Don't use replaceAll(), use replace()!
It is a common misconception that replaceAll() replaces all occurrences and replace() just replaces one or something. This is totally incorrect.
replaceAll() is poorly named - it actually replaces a regex.
replace() replaces simple Strings, which is what you want.
Both methods replace all occurrences of the target.
Just do this:
longstring = longstring.replace(replacestring, "");
And it will all work.
Escape the \ too, using \\\\?.
String longstring = "..."; //This is the long String
String replacestring = "http://test.com/awesomeness.asp?page=27";
longstring=longstring.replaceAll(replacestring, "\\\\?");
But as other answer have mentioned, replaceAll is a bit overkill, just a replace should work.
replaceAll takes a regular expression, and ? has a special meaning in the regex world.
You should use replace in this case, since you don't need a regex.
String longstring = "..."; //This is the long String
String replacestring = "http://test.com/awesomeness.asp?page=27";
longstring = longstring.replace(replacestring, "");
Oh, and strings are immutable!! longstring = longstring.replace(..), notice the assignment.
Use String.replace() instead of String.replaceAll():
longstring = longstring.replace("?", "\\?");
String.replaceAll() uses Regular Expression, while String.replace() uses plain text.
Currently, the replaceAll method of the String class, along with Matcher.replaceAll methods evaluate their arguments as regular expressions.
The problem I am having is that the replacement string I am passing to either of these methods contains a dollar sign (which of course has special meaning in a regular expression). An easy work-around to this would be to pass my replacement string to 'Matcher.quoteReplacement' as this produces a string with literal characters, and then pass this sanitized string to replaceAll.
Unfortunately, I can't do the above as I need to preserve the special characters as the resultant string is later used in operations where a reg ex is expected, and if I have escaped all the special characters this will break that contract.
Can someone please suggest a way I might achieve what I want to do? Many thanks.
EDIT: For clearer explanation, please find code example below:
String key = "USD";
String value = "$";
String content = "The figure is in USD";
String contentAfterReplacement;
contentAfterReplacement = content.replaceAll(key, value); //will throw an exception as it will evaluate the $ in 'value' variable as special regex character
contentAfterReplacement = content.replaceAll(key, Matcher.quoteReplacement(value)); //Can't do this as contentAfterReplacement is passed on and later parsed as a regex (Ie, it can't have special characters escaped).
Why not use String#replace method instead of replaceAll. replaceAll uses regex but replace doesn't use regex in replacement string.