views:

261

answers:

2

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'>
+4  A: 
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
Jenko
@Jeremy, did you try putting `(?<=\d)(?=(\d\d\d)+(?!\d))` in the Regular Expression field and `,` in the Replace Expression field?
Alan Moore
Excellent! Thank you. Much of this goes over my head!
Jenko
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!
Jenko
That's a very different question. See my answer for that.
Alan Moore
+2  A: 

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
My data: <node num='12345678'> 50000 </node> -- I don't want to convert the 50000 number, only the 12345678 one. How can I do this?
Jenko
Have you tried the regex in this answer? It works for me.
Alan Moore