3

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.

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for detailed explanations. How can we match starting from the last / and till the end of string. As I very much new with the regex.
"/(stats|monitor).jsp$" or "(?<=/)(stats|monitor).jsp$"
@Raihan Jamal: What do you mean by "will not work"? You can just replace it with "/ping" isntead of "ping". Won't that work for you?
3

I think this is what you're looking for:

trimUrl = monitorUrl.replaceAll("(?:stats|monitor)\\.jsp", "ping");

Explanation:

  1. replaceAll() treats the first argument as a regex, while replace() treats it as a literal string.

  2. 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.

  3. . 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");

2 Comments

+1 for the escape dot, but the last line has the same problem as Zernike's answer: "monitor.jsstats.jsp" will become "pinging".
True, but according to the question, the final segment can only be monitor.jsp or stats.jsp. That's how I read it, anyway.
1

No needs to use regex (also replace() don't use regex).

trimUrl = monitorUrl.replace("stats.jsp", "ping").replace("monitor.jsp", "ping");

1 Comment

If you do that "monitor.jsstats.jsp" will become "pinging".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.