1

I'm trying to remove every "." except the last one from a dataframe of strings. I'm not that good with regular expressions, so I tried using the one from this answer: Regex to remove all but the last occurrence of a single character

x <- c("1.134.89", "1.125.55", "1.268.61", "1.022.28", "1.386.73", 
       "1.306.64", "1.085.78", "1.700.79", "1.260.60", "1.393.48", "1.498.10", 
       "909.49", "454.98", "874.41", "998.03", "883.70", "941.03", "961.94", 
       "170.27", "174.67", "183.58", "196.09", "167.42", "163.54", "174.22", 
       "182.91", "175.38", "179.80", "180.75", "182.66")

gsub("\.(?![^.]+$)|[^0-9.]", "", x)

Error: '\.' is an unrecognized escape in character string starting ""\."

So i tried adding another \ before the \.

gsub("\\.(?![^.]+$)|[^0-9.]", "", x)

Error in gsub("\\.(?![^.]+$)|[^0-9.]", "", x) : invalid regular expression '\.(?![^.]+$)|[^0-9.]', reason 'Invalid regexp'

Could you help me fix the regular expression so it works with R? Thank you very much!

f.lechleitner
  • 3,554
  • 1
  • 17
  • 35

0 Answers0