Home > other >  regex repetitive parts in monetary amounts
regex repetitive parts in monetary amounts

Time:02-14

In a monetary pattern I look for thousand separator

(?:[. ]\d{3})*

In this case the thousand separator could either be . or . But how to make sure that the pattern will not match patterns where the thousand separator is mixed?

Only match patterns like

.123.123.123
 123 123 123

Do not match

.123 123 123.123

CodePudding user response:

You can match the first separator and then use a backreference to match the same separator for the remaining digit groups:

^(?:([. ])\d{3}(\1\d{3})*)?$

Demo: https://regex101.com/r/u4zPuf/1

  • Related