I would need to change the format to 123.456.789.11
, so far I have only managed to do it as shown in the example https://regex101.com/r/sY1nH4/1, but I would need it to always have 3 digits at the beginning, thank you for your help
$repl = preg_replace('/(?!^)(?=(?:d{3})+$)/m', '.', $input);
4
Answers
I would use this approach, using a capture group:
The above replaces every 3 numbers, starting from the left, with the same numbers followed by a dot, provided that at least one other digit follows.
You should assert that it is not the end of the string instead to prevent adding a dot at the end:
d{3}
Match 3 digits(?!$)
Negative lookahead, assert not the end of the string to the rightK
Forget what is matched so farRegex demo
Output
Here is a possible solution using
B
(opposite ofb
):Replace it with
.
RegEx Demo
By using
B
after 3 digits we ensure that we don’t match the last position.No need for a regular expression actually. split into equal parts, join with comma: