0

I have following html string and I want to add space after all tags like </strong> where space is missing, only inside body. If there is a space already, extra space should not be added.

<html><head><title>test</title></head><body>This <strong>Super</strong> subject can be <strong>super</strong>test into object</body></html>

This should get converted to following:

<html><head><title>test</title></head><body>This <strong>Super</strong> subject can be <strong>super</strong> test into object</body></html>

Is this doable using regex? Can you help me with the regex?

1
  • 3
    s = s.replaceAll("(?<=</strong>)(?! )", " ") --- See demo Commented Dec 5, 2019 at 6:31

1 Answer 1

4

You can use this regex, which will look for any closing tag (in the form </zzzz>) that is not followed by a space, and then replace it with $1:

(<\/[a-z]+>)(?! )

Demo on regex101

This will modify any closing tags in your HTML though. To only modify closing tags within the body, you could split the string first into the parts before the <body, between that and the closing </body and the balance of the string, and then only modify the part in the middle. For example,

String s = "<html><head><title>test</title></head><body>This <strong>Super</strong> subject can be <strong>super</strong>test into object</body></html>";
String [] pieces = s.split("</?body");
pieces[1] = pieces[1].replaceAll("(</[a-z]+>)(?! )", "$1 ");
s = pieces[0] + "<body" + pieces[1] + "</body" + pieces[2];
System.out.println(s);

Output:

<html><head><title>test</title></head><body>This <strong>Super</strong> subject can be <strong>super</strong> test into object</body></html>
Sign up to request clarification or add additional context in comments.

7 Comments

this is good, but it adds extra spaces: <html><head><title>test</title> </head>
@dev009 I interpreted the question as you wanted space after every closing tag... is that not what you want?
ohh no... not all.. only where there is no space, see the converted text
@dev009 sorry, what I meant was, after </title> etc., or did you just mean after </strong>?
@dev009 see my edit, it gives a way to only do the replacement inside the body
|

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.