3

I'm using this free RegExp Designer which does find and replace. How do I search for all numbers and add thousand separators?

Input:      <node num='12345678'>
Output:     <node num='12,345,678'>
Welbog
  • 59,154
  • 9
  • 110
  • 123
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607

2 Answers2

7
s/(?<=\d)(?=(\d\d\d)+(?!\d))/,/g

That said, if you're working with this data as anything other than strings at some point, whatever language you're working in probably has facilities for formatting numeric output.

John Hyland
  • 6,855
  • 28
  • 32
  • Should I match this expression with my text? It does not work in the RegExp Designer app I mentioned earlier. Could you download it and play around within it to see if it actually does the find & replace? – Robin Rodricks Aug 04 '09 at 16:21
  • 2
    @Jeremy, did you try putting `(?<=\d)(?=(\d\d\d)+(?!\d))` in the Regular Expression field and `,` in the Replace Expression field? – Alan Moore Aug 04 '09 at 16:27
  • Excellent! Thank you. Much of this goes over my head! – Robin Rodricks Aug 04 '09 at 17:07
  • Just one more thing, how do I let this regex only run for the "num" attribute's value? Because right now it replaces every number it finds! – Robin Rodricks Aug 04 '09 at 17:22
  • That's a very different question. See my answer for that. – Alan Moore Aug 04 '09 at 18:54
  • 1
    it works! though, a short explanation, like "it will start at the end of the found number (with the negative lookahead at the end), and then subsequently replace packs of three digits..or something"...;_) – benzkji Feb 17 '21 at 21:37
7

To reformat numbers only in "num" attribute values you can do this:

(?<=num='\d+)(?=(?:\d{3})+(?!\d))

But note that this will only work in .NET regexes, which is what RegExp Designer uses. Most regex flavors only allow lookbehinds that match a fixed number of characters. Java regexes allow variable-length lookbehinds as long as there's an obvious maximum length, so you can fake it out by using {min,max} quantifier an arbitrary number for the maximum:

(?<=num='\d{1,20})(?=(?:\d{3})+(?!\d))

John Hyland's regex will work in any flavor that supports lookbehinds.

EDIT: I almost forgot; here's how you can do it without lookbehinds:

(num='\d{1,3}|\G\d{3})(?=(?:\d{3})+(?!\d))

I like this one best for purely aesthetic reasons. :)

EDIT2: I forgot to mention that the replacement string for the last one is "$1,"

Alan Moore
  • 73,866
  • 12
  • 100
  • 156