So here's my dilemma…
I'm using Java to write a program in which I'd like to use regular expressions to parse a string and insert a character at regular intervals. For those of you familiar with Java, I'm using the string class's replaceAll method to accomplish the task. Specifically, I'd like to insert spaces between groups of digits that represent binary, hexadecimal or octal numbers. For example, between every four digits of a binary number, I want to insert a space. Likewise, the digits representing a hexadecimal number would be grouped by twos, and the digits in a string representing an octal number would be grouped by threes. For example, the binary number "11110110" would be replaced by "1111 0110." Ideally, I'd like to devise a single expression to accomplish the task, regardless of the base (i.e., binary, hexadecimal or octal) of the number that the string contains. In other words, I'd rather not have to code three separate calls to the replaceAll method. I have tried alternation using the "or" (|) operator, but without much success.
The other issue is that the digits in the string must be grouped from right to left, which is contrary to the way in which the parser works. To get around that limitation I've been reversing the order of the characters in the string prior to performing the replacement. Afterwards, I reverse the order again.
Years ago I was pretty good at regular expressions. Even so, this challenge might be beyond any expertise I ever attained. Perhaps it can't be done. Any assistance would be greatly appreciated!