I am trying to write a regex with go to match prices that composed by at least five digits with comma on the price.
For example:
"10,400,000","10,900,000","500,000",
I was trying the following expression: "((\d+)(,)(\d+))(,)..."(,) which matches only for the sequence that has eight digits (two commas).
For example:
"10,400,000", valid
"10,900,000", valid
"500,000", invalid
I don't think it would be efficient if I process it twice (one for the number with two commas and the other with one comma). how can I make the expression for the whole pattern?
Thank u
Observe that we need to either match a number between 10,000 and 999,999 or a number 1,000,000 or larger, with commas in the right places.
To match a number between 10,000 and 999,999 we can use
and to match a number 1,000,000 or larger we can use
We therefore merely need to construct an alternation comprised of these two expressions.
Demo
This expression can be broken down as follows.
Lastly, we can factor out the match of the leading digit.