3

I want to change the "." in the middle of each character name to _, but I dont know how to specify that when there are several ".". Does anyone have a suggestion?

Another problem is that it doesnt seem to like the "." When I try to change all "." to _, it doesnt work. How to handle this?

From: "L234.346546.24.3654" "L34.547567.78.79878" "L456.7474.22.07806" 
Expected outcome:
"L234.346546_24.3654" "L34.547567_78.79878" "L456.7474_22.07806" 

v <- c("L234.346546.24.3654", "L34.547567.78.79878", "L456.7474.22.07806")
vv <- gsub(".", "_", v)
vv
[1] "___________________" "___________________" "__________________" 

Thanks!

user11916948
  • 944
  • 5
  • 12
  • 3
    `.` is a special character in regular expressions, just add `fixed = TRUE` if you just want to target literally `.`. – s_baldur Mar 16 '21 at 13:54
  • 1
    `.` has special meaning in regular expressions: it means "any character". You need to escape it: '\\.`. A simple Google search will give you all you need to know. – Limey Mar 16 '21 at 13:55

1 Answers1

2

This should do it:

v <- c("L234.346546.24.3654", "L34.547567.78.79878", "L456.7474.22.07806")
vv <- gsub("(\\..*?)\\.", "\\1_", v)
vv

Output:

[1] "L234.346546_24.3654" "L34.547567_78.79878" "L456.7474_22.07806" 
Johnny
  • 751
  • 12
  • 24