Arun, thanks for both of your suggestions.

I played with your second idea some more and seemed to have found a more general solution (If not a digit nor period then replace with blank).


 x <- "\"$ 1,200,300,400.50\""
 gsub("[^0-9|.]", "", x, perl=TRUE)
[1] "1200300400.50"

Regards,
Mauricio



________________________________


Cc: R help <r-help@r-project.org> 
Sent: Wednesday, November 21, 2012 5:26 PM
Subject: Re: [R] Problems understanding use of regular expression (in gsub) for manipulating currency

HI,
One more method:
x <- "\"$ 1,200,300,400.50\""
 gsub("[\",$ ]","",x)
#[1] "1200300400.50"
A.K.




----- Original Message -----

To: "r-help@r-project.org" <r-help@r-project.org>
Cc: 
Sent: Wednesday, November 21, 2012 4:41 PM
Subject: [R] Problems understanding use of regular expression (in gsub) for manipulating currency

Hello,

After reading help file, various threads on this board, and other online tutorials, I've attempted to use gsub (using Perl-like syntax) to change a currency string into something that can be converted to numeric type using only one regular expression.  Can anybody point out my error?  Note that


>  x <- "\"$ 1,200,300,400.50\""

Tried the following in an attempt to arrive at "1200300400.50"

>  gsub("(^[\\D]*)(([\\d]*)[,])*([\\d]*[.]*[\\d]*)([\\D]*)", "\\3\\4", x, perl=TRUE)
[1]  "300400.50"

Note that "\d" matches a digit character and "\D" matches a non-digit character.
Results group "\2" was intentionally omitted from the replacement pattern as it would have included commas.

I was expecting multiple results for group "\3"

Many thanks,
Mauricio

    [[alternative HTML version deleted]]


______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
	[[alternative HTML version deleted]]


