11

I want to replace a content inside some tags, eg:

<p>this it to be replaced</p>

I could extract the content between with groups like this, but can i actually replace the group?

str = str.replaceAll("<p>([^<]*)</p>", "replacement");
0

2 Answers 2

9

You can use lookaround (positive lookahead and lookbehind) for this:

Change the regex to: "(?<=<p>)(.*?)(?=</p>)" and you will be fine.


Example:

String str = "<p>this it to be replaced</p>";
System.out.println(str.replaceAll("(?<=<p>)(.*?)(?=</p>)", "replacement"));

Output:

<p>replacement</p>

Note however that if you are parsing HTML you should be using some kind of a HTML parser, often regular expressions is not good enough...

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

Comments

8

Change the regex to this:

(?<=<p>).*?(?=</p>)

ie

str = str.replaceAll("(?<=<p>).*?(?=</p>)", "replacement");

This uses a "look behind" and a "look ahead" to assert, but not capture, input before/after the matching (non-greedy) regex

Just in case anyone is wondering, this answer is different to dacwe's: His uses unnecessary brackets. This answer is the more elegant :)

1 Comment

@dacwe They're totally unnecessary. You can use $0 (which is how you refer to the entire match) which in this case is equivalent to your $1 :)

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.