# Thousands separator (comma) option for NumberString/StringCases?

I've been looking for a way to read numbers that have thousands separators in them:

StringCases[" 1142.123 ",  Whitespace ~~ NumberString ~~ Whitespace, 1]


gives

{" 1142.123 "}


but

StringCases[" 1,142.123 ", Whitespace ~~ NumberString ~~ Whitespace, 1]


gives

{}


So ideally there's a Mathematica way of defining NumberString to recognize commas in numbers. Or should I be looking into regexen at this point?

-

This is admittedly a bit of a hack, but you could remove the commas first, using StringReplace:

StringCases[
StringReplace[" 1,142.123 ", "," -> ""],
Whitespace ~~ NumberString ~~ Whitespace, 1]

(* ==> {" 1142.123 "} *)

-
This was the solution I used for another question, and as of yet, I don't see a way around it. –  rcollyer May 3 '12 at 14:02

I would just go to RegExp:

StringCases[" 1,142.123 ", RegularExpression["\\s[0-9,.]+\\s"], 1]

(* {" 1,142.123 "} *)


You could also go with:

ImportString[" 1,142.123 ","List"]


Though that will automatically change it to Numeric, which doesn't seem to be what you want...

-
+1 For anyone who understands regular expressions... –  Eli Lansey May 3 '12 at 14:15
I think you meant ImportString, so I fixed it. I had forgotten about that function, +1. –  rcollyer May 3 '12 at 14:17
ImportString does many tricks, but sometimes is too slow. Anyway it's a very good option if you don't want to build your own transformation. –  FJRA May 3 '12 at 14:18
@rcollyer oops, thanks. My MMA crashed for some reason, so I just typed from memory, but apparently had bad short-term memory –  tkott May 3 '12 at 14:19
@EliLansey this made everything crystal clear: regular-expressions.info (sort-of) :) –  tkott May 3 '12 at 14:20

Regular expressions are nice, but there is no need to use them here.

numString = (NumberString | ",") .. ;

StringCases[" 1,142.123 ", Whitespace ~~ numString ~~ Whitespace, 1]

{" 1,142.123 "}


The form above is admittedly not robust as a valid number string should not start or end with a comma or have two decimal points, and I believe the comma should not immediately precede a decimal point or appear to its right side. The easiest way I can think to preclude these is to check for them explicitly:

numString = x : (NumberString | ",") .. /;
! StringMatchQ[x, ",*" | "*," | "*.*,*" | "*,.*" | "*.*.*"];

StringCases[" 1,2. ,3 4, 5.6,7,.89 0.1.2sam, i am ", numString]

{"1,2.", "3", "4", "5.6", "7", ".89", "0.1", ".2"}

-
@R.M maybe I need to lern me my's RegEx, but I believe tkott's pattern is equivalent (besides whitespace) to my first form: (NumberString | ",") .. in which case I would argue that, in the context of Mathematica, his is clunky. (Incidentally I already voted for his answer.) –  Mr.Wizard Aug 18 '12 at 14:45
I don't know regex well either :P I would've written something like this had I seen this question then too. I assumed his regex was equivalent to the second half of your answer, but on looking closer, I see that's not the case. Including all the exceptions would make his just as clunky, so comment revoked! :) –  The Toad Aug 18 '12 at 16:03