0

I got a page with prime-faces editor component to create document models, so that the model is saved in DB and used as template to create new documents easier in the future.

The problem is that the older models saved by a legacy system are not being properly formatted in the editor, to be more specific, it is ignoring left blank spaces.

I found out that if I replace spaces " " by "&nbsp" the editor shows the spaces, but I just can't get the right regex working on it.

What I need to do is: get all spaces major than " " (e.g. " " or " ") and replace it by the right "&nbsp" number. So that " " would be replaced by "&nbsp&nbsp".

Using the following regex I can reach the conditional of more than 1 space, but I don't know how to replace it.

 myString.replaceAll(" [\\s]+", "????");

Is that possible to do? Or I'd better think on something else?

4
  • Are you trying to get rid of the double quotes? Commented May 23, 2016 at 13:59
  • Not sure I understand. Do you only need to replace any 2+ consecutive whitespaces with one whitespace? Commented May 23, 2016 at 13:59
  • Not exactly, the number of white spaces is the variable, if I have " " (3 white spaces within quotes) I need to replace it by "&nbsp&nbsp&nbsp" so that the editor will show 3 white spaces. If I have 5 white spaces I need 5 "&nbsp" and so on Commented May 23, 2016 at 14:02
  • @user2424758 do you need to replace all spaces or only when there's 2 or more consecutive? Commented May 23, 2016 at 14:06

2 Answers 2

3

You can use this solution to conditionally replace any 2+ consecutive whitespaces with their equivalent number of   instances:

String spaces = "1 2  3   4    1 5     end";
System.out.println(
    //                 | space followed by space
    //                 |        | or
    //                 |        | | space preceded by space
    spaces.replaceAll("\\s(?=\\s)|(?<=\\s)\\s", "&nbsp;")
);

Output

1 2&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;1 5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

Note

This will leave single whitespaces alone, as it seems to be your intention.

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

4 Comments

I think the lookahead part is enough and you could remove the right-hand part of your alternation, since the regex engine will replace in the string from left to right. Nice one anyway.
@Aaron thanks. Unfortunately it needs both, otherwise it'll miss one replacement.
You're right, but I still stand my point for other reasons : OP's problem is probably that HTML browsers ignore multiple adjacent spaces, so leaving a single space in the sequence would be ok (and a little bit more efficient regarding to the regex execution and the bandwith used to serve the ressource). Now I agree that your answer perfectly answers the asked question.
@Aaron glad to hear :) And thanks for the explanation, I actually had no idea why OP wanted that - looks like you got it figured out.
1

If your intent is simply to replace every space with nbsp, then remove the first space from your regex.

Code

String myString = "this is   a test";
System.out.println(myString.replaceAll("[\\s]", "&nbsp;"));

Output

this&nbsp;is&nbsp;&nbsp;&nbsp;a&nbsp;test

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.