1

I'm new to regular expressions and I have an expression that I have to write for work. I was looking at using replaceAll() to do this but I'm unsure if I can do what I'm proposing using one regular expression.

I have to change a string in the format of "abc12378" to "$$$ $$$ 78". I can do this with two different regular expressions without issue:

  • String: "abc12378"
  • RegEx: "([1-6])"
  • Replacement: "\$"
  • Result: "$$$$$$78"

Or

  • String: "$$$$$$78"
  • RegEx: "(.{3})"
  • Replacement: "$1 "
  • Result: $$$ $$$ 78

Would you know if I can do this with one combined replaceAll()?

Any help would be much appreciated.

Oaryx
  • 67
  • 4
  • You can replace three characters by `$$$ `. [Find: `(.{3})`. Replace: `$$$ `](https://regex101.com/r/sT4lT0/1) – Tushar Apr 19 '16 at 15:01
  • 1
    https://regex101.com/r/uE3gD5/2 – rock321987 Apr 19 '16 at 15:03
  • You haven't provided any information about the string that you're expecting or the string that you want to replace it with. Do you need to just replace first 6 characters ? what if the string doesn't have 6 characters ? why have you used `[1-6]` ? does that mean that you're expecting first 3 digits of the number to be less than 6 ? – 11thdimension Apr 19 '16 at 15:33
  • The string will always be 8 characters long (either int or string). – Oaryx Apr 19 '16 at 15:47

1 Answers1

0

This single replaceAll call do what you want, indeed it simply replaces each group of 3 alphanumerical characters by "$$$ ":

 String s = "abc12378";
 System.out.println(s.replaceAll("[a-z0-9]{3}", "\\$\\$\\$ "));

Output

$$$ $$$ 78

Assuming that you have one more character, here is how you should proceed:

String s = "123456789";
System.out.println(s.replaceAll("[a-z0-9]{3}(?!$)", "\\$\\$\\$ "));

Output

$$$ $$$ 789
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • Just out of curiosity (for future reference)- what if the final three digits were in groups of three as well? e.g. 123456789. This wouldn't work because it would just turn it to XXX XXX XXX instead of XXX XXX 789 – Oaryx Apr 19 '16 at 15:56
  • Possible duplicate : http://stackoverflow.com/questions/17969436/java-regex-capturing-groups – MensSana Apr 19 '16 at 16:06
  • you simply use the same approach but on a substring from 0 to length - 3, then you append the last 3 characters to it. – Nicolas Filotto Apr 19 '16 at 16:07
  • @menssana I didn't mean to be rude, but this should be really a comment. If you think that question is already answered, then please add the comment on question. With enough rep. you can vote to close it yourself. – Tushar Apr 19 '16 at 16:08
  • @Oaryx why my answer is no more good enough? Could you please clarify? – Nicolas Filotto Apr 19 '16 at 16:11
  • I was just wondering if there was any way I could do this with one replaceAll(). I know I could use substrings, two consecutive replaceAll()s or even Patter and Matcher like the post mentioned above. It was a pretty specific question, I suppose. – Oaryx Apr 19 '16 at 16:53
  • You asked a specific question, I believe that I provided a valid answer, don't you agree? If you have a different question, you can submit another question, you will then get the best answer for this particular question, it is how it works – Nicolas Filotto Apr 19 '16 at 17:00
  • In my original question I was specific: I said, and I quote: "I can do this no problem with two different regular expressions" "Would you know if I can do this with one combined regEx?" In addition, the title of my question is: "Combining two regular expressions with replaceAll()". I was just wondering if it was possible to do what I was asking with one regular expression, not a combination of multiple things which I already know how to do. – Oaryx Apr 19 '16 at 17:19
  • @Oaryx actually I thought that it was what I proposed above, you have only one replaceAll to get your result instead of 2, it is not what you want? – Nicolas Filotto Apr 19 '16 at 17:23
  • Yes it did answer my question, I'm not denying that. But then I asked politely (just out of curiosity) what would happen if there were multiple groups of three. I could have gone ahead and make another post which specifies this but I'm pretty sure I would get a comment referring back to my original post saying it's a duplicate question. So my second (just out of curiosity) question wouldn't be answered. – Oaryx Apr 19 '16 at 17:27
  • @Oaryx I updated my answer to provide the solution of the second problem – Nicolas Filotto Apr 19 '16 at 17:38
  • Thanks a billion! Sorry for the trouble, I was just genuinely curious. – Oaryx Apr 19 '16 at 17:39