0

I'm trying to improve my understanding on RegEx. I have the following RegEx in php

preg_replace("/(\d)(?=(\d{3})+$)/", '$1,', "35000");

What this does is add commas into the number (35,000). What i would like to understand is how does this work?

From what i know so far, it seems that it starts from a single digit and looks ahead for occurrence of 3 digits. But i don't think my understanding is correct.

Thanks in advance.

EDIT

I noticed that the following also works:

preg_replace("/(?=(\d{3})+$)/", ',', "35000");
Prem Raj
  • 799
  • 4
  • 14
  • 1
    ...and looks ahead for *1 or more sequences* of 3 digits *up to the end of string without adding the lookahead pattern match into the result (as lookaheads are zero-width assertions)*. Also, see [*Lookarounds Stand their Ground*](http://www.rexegg.com/regex-lookarounds.html#stand_their_ground). – Wiktor Stribiżew Nov 18 '16 at 11:36
  • Also you can check it by yourself just by pasting your expression here: http://regexr.com/ – Sebastian Kaczmarek Nov 18 '16 at 11:38

0 Answers0