0

A have a string that contains one ore more number. The numbers are normally integers, but also decimal could be expected at some point. I looking for a solution that adds the thousand separator(. or ,) to all numbers inside the string.

For example

  1. 100000 -> ?100,000?
  2. ?100000?50000 -> ?100,000?50,000
  3. X10000.5? -> X100,00.5?

Any idea?

Sebastjan
  • 142
  • 1
  • 5
  • 13

1 Answers1

1

A hacky solution in pure regex is to replace all occurences of

(?<=\d)(\d{3})(?!\d)

with ,$1. This is, of course, very limited, as it only adds one separator per number, and also adds separators after a comma. See regex101 demo.


A far cleaner solution is to search for numbers using a regex such as \d+(?:\.\d+)?, convert each match to a number, and re-insert the formatted number into the text.

(You'll have to forgive me for not including any code, but I haven't coded in C# in ages.)

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149