2

I have an example input that can be of any length and want to add a space every 4 characters.

for example input = xxxxxxxxxxxx

result I expect: xxxx xxxx xxxx

I have looked at the replaceAll() method but wondering how I can create a match that returns me the 4th, 8th, 12th etc character index so I can do something like this:

input.replaceAll("([\\S\\s)]{4})\\G", " " + input.charAt(index - 1))

where index somehow gets modified to get the appropriate index in which the regular expression of mine has found the 4th character.

3
  • Do you want a regex solution? It can be done easier without Commented Apr 30, 2015 at 14:04
  • How can it be done easier? I must note down that the input string is dynamic and can change. So while its being changed, i want to auto format it Commented Apr 30, 2015 at 14:05
  • 2
    I was thinking of Rahul Tripathi's answer, where you just parse the string and add a space every 4th character Commented Apr 30, 2015 at 14:11

5 Answers 5

4
"xxxxxxxxxxxx".replaceAll(".{4}(?!$)","$0 ");

This won't add trailing space to the last segment.

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

5 Comments

.... is the same length, and easier to read, than .{4}, but +1 anyway, especially the "not at the end" whistle
@Bohemian thx. you are right for the ...., but if n>5, I would say .{n} would be easier to read. :)
It works if the format is already xxxxxxxxx But what if the format is only partially done ie xxxx xxxxxxxxxx? I tried that approach and what happens is that it increases the space from the first group of characters leading to something like this instead: xxxx xxxxxxxx with double space
@jonney want to add a space every 4 characters you didn't say anything about the "partially done" case. if you want to add space for every 4 non-empty chars, you can try \\S{4}(?!=\\s) I didn't test though.
@Kent any suggestions? tried the above you mentioned and still added extra space
2

You can try this wihout using regex:

StringBuilder str = new StringBuilder("xxxxxxxxxxxx");
int i = str.length() - 4;

while (i > 0)
{
    str.insert(i, " ");
    i = i - 4;
}

System.out.println(str.toString());

With regex:

String myString = "xxxxxxxxxxxx";
String str = myString.replaceAll("(.{4})(?!$)", "$0 ");
return str;

Comments

2

You can try with replaceAll(".{1,4}+(?!$)", "$0 ")

  • .{1,4}+ will match any 1-4 characters (+ will make it possessive)
  • (?!$) which are not right before end of string

  • $0 " will replace it with content from group 0 (which is current match) plus space


Actually I overcomplicated this regex. You can find simpler version based on same idea in @Kent's answer.

Comments

0

Use grouping; $1 (and upwards) will get replaced with matcher.getGroup(1) .

Comments

0

This will not add a space if there is one

String s = "xxxxxxxx xxxxx x xx x";
System.out.println(s.replaceAll("\\w{4}(?!\\s)", "$0 " ));

Outmput: xxxx xxxx xxxx x x xx x

demo

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.