R 使用正则表达式提取重复子字符串

R 使用正则表达式提取重复子字符串,r,regex,regexp-replace,R,Regex,Regexp Replace,我正在尝试删除在之后开始的子字符串:“使用r中的regex "Disaster - Natural (ex - fire, tornado, flood, etc.): Flood 1/1/2020;Event - Income: 1/1/2020;Disaster - Natural (ex - fire, tornado, flood, etc.): Fire" "Disaster - Natural (ex - fire, tornado, flood, e

我正在尝试删除在之后开始的子字符串:“使用r中的regex

"Disaster - Natural (ex - fire, tornado, flood, etc.): Flood 1/1/2020;Event - Income: 1/1/2020;Disaster - Natural (ex - fire, tornado, flood, etc.): Fire"

"Disaster - Natural (ex - fire, tornado, flood, etc.): Flood 1/1/2020"

"Event - Health; Disaster - Natural (ex - fire, tornado, flood, etc.): Fire"

在本例中,我试图删除每个分隔类别后的“洪水1/1/2020”、“1/1/2020”和“火灾”。每个“:”后面的单词和日期各不相同。数据在一列中结构化,每行的分隔类别数量不同


正则表达式对我来说是个弱点。非常感谢您的帮助。

您是说下面这样的正则表达式吗

gsub(":.*?(;|$)","\\1",s,perl = TRUE)

[1] "Disaster - Natural (ex - fire, tornado, flood, etc.);Event - Income;Disaster - Natural (ex - fire, tornado, flood, etc.)"
- fire, tornado, flood, etc.)"
[2] "Disaster - Natural (ex - fire, tornado, flood, etc.)"
[3] "Event - Health; Disaster - Natural (ex - fire, tornado, flood, etc.)"
数据

s <- c("Disaster - Natural (ex - fire, tornado, flood, etc.): Flood 1/1/2020;Event - Income: 1/1/2020;Disaster - Natural (ex - fire, tornado, flood, etc.): Fire",
"Disaster - Natural (ex - fire, tornado, flood, etc.): Flood 1/1/2020",
"Event - Health; Disaster - Natural (ex - fire, tornado, flood, etc.): Fire"
)

s带有
stru-remove的选项

library(stringr)
str_remove_all(s, "Flood \\d+/\\d+/\\d{4}|:\\s+Fire")
-输出

#[1] "Disaster - Natural (ex - fire, tornado, flood, etc.): ;Event - Income: 1/1/2020;Disaster - Natural (ex - fire, tornado, flood, etc.)"
#[2] "Disaster - Natural (ex - fire, tornado, flood, etc.): "                                                                              
#[3] "Event - Health; Disaster - Natural (ex - fire, tornado, flood, etc.)"    
数据
s这与我想要实现的目标非常接近。我只想保留行中每个类别之间的“;”分隔符。谢谢你的回复!
s <- c("Disaster - Natural (ex - fire, tornado, flood, etc.): Flood 1/1/2020;Event - Income: 1/1/2020;Disaster - Natural (ex - fire, tornado, flood, etc.): Fire", 
"Disaster - Natural (ex - fire, tornado, flood, etc.): Flood 1/1/2020", 
"Event - Health; Disaster - Natural (ex - fire, tornado, flood, etc.): Fire"
)