# String formatting

I have the following string:

"174,861.00 (      4,053.52)     206,850.48   118.29 (     31,989.48)"


Parentheses stand for negative numbers. I will like to get it converted/formatted to the following:

"174861.00,(4053.52),206850.48,118.29,(31989.48)"


Just having a problem collapsing the spaces between the ( and the next valid digit.

-

But if you always want to replace the sequence brace, spaces, digit, why don't you just say so?

str="174,861.00 (      4,053.52)     206,850.48   118.29 (     31,989.48)";

StringReplace[
StringReplace[str, d1 : DigitCharacter ~~ "," ~~ d2 : DigitCharacter :> d1 ~~ d2],
{"(" ~~ Whitespace .. ~~ i : DigitCharacter -> "(" ~~ i,
Longest[Whitespace ..] -> ","}
]

(* "174861.00,(4053.52),206850.48,118.29,(31989.48)" *)

-

Possibly this is sufficient:

in = "174,861.00 (      4,053.52)     206,850.48   118.29 (     31,989.48)";

StringReplace[in, {"," -> "", "(" ~~ Whitespace -> "(", Whitespace -> ","}]

"174861.00,(4053.52),206850.48,118.29,(31989.48)"

-

Personally, I would use common regural expression syntax, since this can be checked also by non-Mathematica programmers:

str = "174,861.00 (      4,053.52)     206,850.48   118.29 (     31,989.48)";

StringReplace[str, {
"," -> "",
RegularExpression[" $+"] -> ",(", RegularExpression["$ +"] -> ")," ,
RegularExpression[" +"] -> ","}
]


which returns:

"174861.00,(4053.52),206850.48,118.29,(31989.48)"

Note the double backslash "$" before the parentheses, which are needed to become a "\(" undestandable by the regex. - If you're coding for Mathematica why does it matter if it can be "checked also by non-Mathematica programmers?" Even were it to matter I think Whitespace is quite transparent. – Mr.Wizard Apr 5 '14 at 18:20 @Mr.Wizard sorry for the late reply: it may matter if for example, as I do, you need to integrate MMA with the web and you may need to perform similar tasks in MMA and Javascript or PHP. In these cases, using the same syntax helps to reduce errors. – user8074 Apr 17 '14 at 21:29 I see your logic. In that case I suggest strict use of regular expressions unmixed with Mathematica patterns; this would also simplify syntax. One could write e.g. regexReplace[string_, rules_, n_: Infinity] := StringReplace[string, #, n] &[ rules /. (L_ -> R_) | (L_ :> R_) :> (RegularExpression[L] :> R) ] and then perform this manipulation with regexReplace[str, {"," -> "", " \\( +" -> ",(", "$ +" -> "),", " +" -> ","}]. What do you think of that proposal? – Mr.Wizard Apr 17 '14 at 23:23
@Mr.Wizard The proposal is of course valid. The reason I often write this code as I reported in the reply is that it makes clearer to me where the regexes start and finish. If you look at your regexReplace[s...] section, you will see that it becomes rather difficult to distinguish symbols inside and outside the regex, while delimiting with RegularExpression[] (or a short form like RE=RegularExpression) makes it easier to figure out. Just a matter of taste. – user8074 Apr 18 '14 at 4:59