简体   繁体   English

仅在 R 中从文本中删除单个正斜杠

[英]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.我正在尝试使用 R 从任何文本中仅删除 /。我尝试了不同的方法,但结果喜忧参半。

This is the text I am dealing with s/p Left IOLI 3/9/04.这是我正在处理的文本s/p Left IOLI 3/9/04.

I am trying to produce an output like this sp Left IOLI 3/9/04.我试图产生这样的输出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.到目前为止,只有gsub("[^A-Za-z]", ".", str, perl =T)有效。 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.此处使用包 stringr 的 str_replace_all 进行演示,但显然也可以使用基本函数。

#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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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