0

I have a String of text that contains various urls surrounded by [url]

For example:

[url]http://www.example.com[/url]

And I want it to read like this:

http://www.example.com

I've tried:

String test = body3.replaceAll("[url]", "");

But I end up still with the brackets []

3 Answers 3

1

You could try using a regex replacement:

String input = "Example text [url]http://www.example.com[/url] Example text";
input = input.replaceAll("\\[url\\](.*?)\\[/url\\]", "$1");

Demo here:

Rextester

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

3 Comments

Thanks that works perfectly. Just what i was looking for. Thanks for your help.
Could i ask what function the (.*?) And "$1" perform?
The quantity (.*?) is a lazy match all, which means it will match everything up until the first closing [/url] tag. And $1 is the first (and only) capture group, which is the URL itself.
0

Try this. It seems you want both [url],[/url] to be replaced with empty spaces.

String a = "Example text [url]http://www.example.com[/url] Example text";
String[] b = {"[url]","[/url]"};
String[] c = {"",""};
StringUtils.replaceEach(a,b,c);

result will be : Example text http://www.example.com Example text.

Remember that if you want to replace number of strings with corresponding empty strings or any other string you can do it in above way.

4 Comments

What is StringUtils? (Yes, i know its an external library) But why should someone want to use this for such a simple operation?
This might be simple operation but StringUtils is useful for various string operations. It provides number of function which you write manually for e.g null and empty checks and many more which will make code less readable.Moreover if you want to replace one more string with corresponding string, you can simply add it in array rather than dealing with complex regular expressions.
Sure, you can also use an SDK for several other stuff, but it still doesnt make sense at this question. Including a library for a simple operation is just overkill.
If you dealing with java web applications, then i think "org.apache.commons" is usually part of your dependencies. So in that case you can easily use it. And above is code written in different statements to make the clearity, how things are working, you can directly put arrays in in replace statements. things will be simple :)
0

In a regular expression, square brackets are used to specify a range of items to match, so [url] will match the characters, u, r and l.

To match an actual square bracket, you will need to escape it by prefixing it a backslash

\[

But a single backslash will also need to be escaped in java, so you need to precede the square brackets with two backslashes.

So to remove the url block with replaceAll, you could use

    String text = "[url]http://www.example.com[/url]";
    String replaced = text.replaceAll("\\[[url/]+\\]", "");
    System.out.println("replaced = " + replaced);

Comments

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.