Ricard Le
Ricard Le

Reputation: 41

regular expression help in R

I have data that looks like this, where the negative sign is a suffix at the end of the numeric.

"general_amount"
"0000000441244"
"0000000127769-"
"0000000043819"
"0000000522600-"

Can someone help me find a regular expression to produce the desired output below.

"general_amount"
 441244
-127769
 43819
-522600

Upvotes: 1

Views: 146

Answers (3)

agstudy
agstudy

Reputation: 121608

Using gsub , with another idea.

The idea is to divide the input into 3 elements

  1. series of 0 :(^0+)
  2. series of number :([0-9]+)
  3. find the '-' 1 or zero times : (-?)"

       as.numeric(gsub("(^0+)([0-9]+)(-?)","\\3\\2",tt))
       [1]  441244 -127769   43819 -522600
    

Upvotes: 1

Mirage
Mirage

Reputation: 31568

Dude it took me 3 hours to find answer to your question

sed -re 's/[^a-zA-Z0-9]0+([0-9]+)(-?)/\2\1/g' anyfile.txt 

But in the end i did it. May have some short coming but i got it nearly

Upvotes: 0

Matthew Lundberg
Matthew Lundberg

Reputation: 42689

sub('^0*([^-]*)(-?)$', '\\2\\1', x)

## [1] "general_amount" "441244"         "-127769"        "43819"          "-522600"

^0* matches all leading 0 characters.
[^-]* matches all non-- characters.
-? matches zero or one - character.
Finally, the $ matches the end of the string.

The middle two pieces are captured with (), as \\1 and \\2, and printed in reverse order.

Upvotes: 9

Related Questions