How to replace all regex string in a String object? - java

For a string str_in = "instance (\\w+\\s+){0,8}deleted"; how can I extract instance and deleted by using the replaceAll function?
I tried str_in = str_in.replaceAll("(\\w+\\s+){0,8}", ""); but it didn't work.

If you, as your question states, really want to use replaceAll() instead of the (in my opinion more suitable) replace(), you can use the \Q and \E markers to match the string literally:
String str_in = "instance (\\w+\\s+){0,8}deleted";
System.out.println(str_in.replaceAll("\\Q(\\w+\\s+){0,8}\\E", ""));
prints
instance deleted

You will need to escape the single characters so that they lose their regex nature:
str_in.replaceAll("\\(\\\\w\\+\\\\s\\+\\)\\{0,8\\}", "")
Each escaping backslash needs to be escaped for itself because of the string literal.

Do you mean that (\\w+\\s+){0,8} is literally in the string, and you want to remove it? You will need to escape each \ again in your replaceAll, so that they are interpreted literally, not as part of a regex, and also the ( and {.

Use str_in.replace("(\\w+\\s+){0,8}", "");
replace()
Replaces each substring of this string that matches the literal target
sequence with the specified literal replacement sequence. The
replacement proceeds from the beginning of the string to the end, for
example, replacing "aa" with "b" in the string "aaa" will result in
"ba" rather than "ab"
replaceAll()
Replaces each substring of this string that matches the given regular expression with the given replacement

Related

Regular expression to replace (") with (\")?

Why does the following not work:
String test = "hello\"world".replaceAll("\"", "\\\"");
System.out.println(test);
What I'm trying to do is replace any occurrence of " with \".
So I want to get as output:
hello\"world
Regular expressions are overkill for this.
myString.replace("\"", "\\\"")
should do just fine and is more readable to someone familiar with the core libraries.
The replace method just replaces one substring with another.
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".
You need to two more \\ to escape the escape character, for a total of 5 \s.
\\ - escape the escape character
\\ - to display the character
\ - to escape the quote.
Try:
String test = "hello\"world".replaceAll("\"", "\\\\\"");
String test = "hello\"world".replaceAll("\"", "\\\\\"");
System.out.println(test);

Replace "\" to a String

I have a URL string
http:\/\/a0.twimg.com\/profile_images\/2170585961\/ETimes_normal.png
I want replace "\" by "" but I use:
String.replaceAll("\","");
And it display error. How do i must?
(Retreived from this url key profile_image_url)
Escape the backslash with another backslash:
String.replaceAll("\\\\","");
As the first argument is a regular expression, there should be two backslashes (\ is a special character in regex). But it's also a string, so each backslash should be escaped. So there are four \s.
Use String.replace(CharSequence, CharSequence) instead, it repleaces all occurrences!
str = str.replace("\\", "");
From your example:
String u = "http:\\/\\/a0.twimg.com\\/profile_images\\/2170585961\\/ETimes_normal.png";
System.out.println(u.replace("\\",""));
Outputs:
http://a0.twimg.com/profile_images/2170585961/ETimes_normal.png
Note that String.replaceAll method takes a regular expression and in this case you don't need it..

How do I replace all "[", "]" and double quotes in Java

I'm am having difficulty using the replaceAll method to replace square brackets and double quotes. Any ideas?
Edit:
So far I've tried:
replace("\[", "some_thing") // returns illegal escape character
replace("[[", "some_thing") // returns Unclosed character class
replace("^[", "some_thing") // returns Unclosed character class
Don't use replaceAll, use replace. The former uses regular expressions, and [] are special characters within a regex.
String replaced = input.replace("]", ""); //etc
The double quote is special in Java so you need to escape it with a single backslash ("\"").
If you want to use a regex you need to escape those characters and put them in a character class. A character class is surrounded by [] and escaping a character is done by preceding it with a backslash \. However, because a backslash is also special in Java, it also needs to be escaped, and so to give the regex engine a backslash you have to use two backslashes (\\[).
In the end it should look like this (if you were to use regex):
String replaced = input.replaceAll("[\\[\\]\"]", "");
The replaceAll method is operating against Regular Expressions. You're probably just wanting to use the "replace" method, which despite its name, does replace all occurrences.
Looking at your edit, you probably want:
someString
.replace("[", "replacement")
.replace("]", "replacement")
.replace("\"", "replacement")
or, use an appropriate regular expression, the approach I'd actually recommend if you're willing to learn regular expressions (see Mark Peter's answer for a working example).
replaceAll() takes a regex so you have to escape special characters. If you don't want all the fancy regex, use replace().
String s = "[h\"i]";
System.out.println( s.replace("[","").replace("]","").replace("\"","") );
With double quotes, you have to escape them like so: "\""
In java:
String resultString = subjectString.replaceAll("[\\[\\]\"]", "");
this will replace []" with nothing.
Alternatively, if you wished to replace ", [ and ] with different characters (instead of replacing all with empty String) you could use the replaceEachRepeatedly() method in the StringUtils class from Commons Lang.
For example,
String input = "abc\"de[fg]hi\"";
String replaced = StringUtils.replaceEachRepeatedly(input,
new String[]{"[","]","\""},
new String[]{"<open_bracket>","<close_bracket>","<double_quote>"});
System.out.println(replaced);
Prints the following:
abc<double_quote>de<open_bracket>fg<close_bracket>hi<double_quote>

Replace string by matching with the regular expressions in Java

here monitorUrl contains- http://host:8810/solr/admin/stats.jsp
and monitorUrl sometimes can be-- http://host:8810/solr/admin/monitor.jsp
So i want to replace stats.jsp and monitor.jsp to ping
if(monitorUrl.contains("stats.jsp") || monitorUrl.contains("monitor.jsp")) {
trimUrl = monitorUrl.replace("[stats|monitor].jsp", "ping");
}
Anything wrong with the above code. As I get the same value of monitorUrl in trimUrl.
Try using replaceAll instead of replace (and escape the dot as Alan pointed out):
trimUrl = monitorUrl.replaceAll("(stats|monitor)\\.jsp", "ping");
From the documentation:
replaceAll
public String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
Note: You may also want to consider matching only after a / and checking that it is at the end of the line by using $ at the end of your regular expression.
I think this is what you're looking for:
trimUrl = monitorUrl.replaceAll("(?:stats|monitor)\\.jsp", "ping");
Explanation:
replaceAll() treats the first argument as a regex, while replace() treats it as a literal string.
You use parentheses, not square brackets, to group things. (?:...) is the non-capturing form of group; you should use the capturing form - (...) - only when you really need to capture something.
. is a metacharacter, so you need to escape it if you want to match a literal dot.
And finally, you don't have to check for the presence of the sentinel string separately; if it's not there, replaceAll() just returns the original string. For that matter, so does replace(); you could also have done this:
trimUrl = monitorUrl.replace("stats.jsp", "ping")
.replace("monitor.jsp", "ping");
No needs to use regex (also replace() don't use regex).
trimUrl = monitorUrl.replace("stats.jsp", "ping").replace("monitor.jsp", "ping");

Split string with dot as delimiter

I am wondering if I am going about splitting a string on a . the right way? My code is:
String[] fn = filename.split(".");
return fn[0];
I only need the first part of the string, that's why I return the first item. I ask because I noticed in the API that . means any character, so now I'm stuck.
split() accepts a regular expression, so you need to escape . to not consider it as a regex meta character. Here's an example :
String[] fn = filename.split("\\.");
return fn[0];
I see only solutions here but no full explanation of the problem so I decided to post this answer
Problem
You need to know few things about text.split(delim). split method:
accepts as argument regular expression (regex) which describes delimiter on which we want to split,
if delim exists at end of text like in a,b,c,, (where delimiter is ,) split at first will create array like ["a" "b" "c" "" ""] but since in most cases we don't really need these trailing empty strings it also removes them automatically for us. So it creates another array without these trailing empty strings and returns it.
You also need to know that dot . is special character in regex. It represents any character (except line separators but this can be changed with Pattern.DOTALL flag).
So for string like "abc" if we split on "." split method will
create array like ["" "" "" ""],
but since this array contains only empty strings and they all are trailing they will be removed (like shown in previous second point)
which means we will get as result empty array [] (with no elements, not even empty string), so we can't use fn[0] because there is no index 0.
Solution
To solve this problem you simply need to create regex which will represents dot. To do so we need to escape that .. There are few ways to do it, but simplest is probably by using \ (which in String needs to be written as "\\" because \ is also special there and requires another \ to be escaped).
So solution to your problem may look like
String[] fn = filename.split("\\.");
Bonus
You can also use other ways to escape that dot like
using character class split("[.]")
wrapping it in quote split("\\Q.\\E")
using proper Pattern instance with Pattern.LITERAL flag
or simply use split(Pattern.quote(".")) and let regex do escaping for you.
Split uses regular expressions, where '.' is a special character meaning anything. You need to escape it if you actually want it to match the '.' character:
String[] fn = filename.split("\\.");
(one '\' to escape the '.' in the regular expression, and the other to escape the first one in the Java string)
Also I wouldn't suggest returning fn[0] since if you have a file named something.blabla.txt, which is a valid name you won't be returning the actual file name. Instead I think it's better if you use:
int idx = filename.lastIndexOf('.');
return filename.subString(0, idx);
the String#split(String) method uses regular expressions.
In regular expressions, the "." character means "any character".
You can avoid this behavior by either escaping the "."
filename.split("\\.");
or telling the split method to split at at a character class:
filename.split("[.]");
Character classes are collections of characters. You could write
filename.split("[-.;ld7]");
and filename would be split at every "-", ".", ";", "l", "d" or "7". Inside character classes, the "." is not a special character ("metacharacter").
As DOT( . ) is considered as a special character and split method of String expects a regular expression you need to do like this -
String[] fn = filename.split("\\.");
return fn[0];
In java the special characters need to be escaped with a "\" but since "\" is also a special character in Java, you need to escape it again with another "\" !
String str="1.2.3";
String[] cats = str.split(Pattern.quote("."));
Wouldn't it be more efficient to use
filename.substring(0, filename.indexOf("."))
if you only want what's up to the first dot?
Usually its NOT a good idea to unmask it by hand. There is a method in the Pattern class for this task:
java.util.regex
static String quote(String s)
The split must be taking regex as a an argument... Simply change "." to "\\."
The solution that worked for me is the following
String[] fn = filename.split("[.]");
Note: Further care should be taken with this snippet, even after the dot is escaped!
If filename is just the string ".", then fn will still end up to be of 0 length and fn[0] will still throw an exception!
This is, because if the pattern matches at least once, then split will discard all trailing empty strings (thus also the one before the dot!) from the array, leaving an empty array to be returned.
Using ApacheCommons it's simplest:
File file = ...
FilenameUtils.getBaseName(file.getName());
Note, it also extracts a filename from full path.
split takes a regex as argument. So you should pass "\." instead of "." because "." is a metacharacter in regex.

Categories

Resources