简体   繁体   中英

Strip single forward slash from text only in R

I am trying to remove only the / from any text using R. I have tried different approaches and I got mixed results.

This is the text I am dealing with s/p Left IOLI 3/9/04.

I am trying to produce an output like this sp Left IOLI 3/9/04.

Only strip the / in text and not numbers.

I have tried these four

gsub("\", "", str, fixed=T) 
gsub("/", ".", str, fixed=T)
gsub("[^A-Za-z]", ".", str, perl =T)
str_replace( str, "/", "")

So far only gsub("[^A-Za-z]", ".", str, perl =T) worked. sucker stripped the / off of everything text numbers and everything. I just need the / from text be gone. Any help is much appreciated folks.

We can use regex lookarounds to remove the forward slash that are not between numbers.

gsub('(?<![0-9])/(?![0-9])', '', str, perl=TRUE)
#[1] "sp Left IOLI 3/9/04."

If we also need to remove / when either the left or right side contain non-numeric characters,

gsub('(?<![0-9])/|/(?![0-9])', '', str1, perl=TRUE)
#[1] "sp Left IOLI 3/9/04." "s12 45p sp Left"     

data

str <- 's/p Left IOLI 3/9/04.'
str1 <- c(str, 's/12 45/p s/p Left')

An alternative way is to run multiple regexes. Demonstrated here using str_replace_all of package stringr, but obviously will work using base functions as well.

#First correct for / between 2 alphabets like s/p
mystring <- str_replace_all(mystring, "([a-zA-Z])/([a-zA-Z])", "\\1\\2")

#Next, correct for / between 1 alphabet and 1 number like s/12 or 45/p
mystring <- str_replace_all(mystring, "([a-zA-Z])/([\\d])", "\\1\\2")
mystring <- str_replace_all(mystring, "([\\d])/([a-zA-Z])", "\\1\\2")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM