我有一个包含数字,字母和空格的文本字符串。它的某些子字符串是月份的缩写。我想执行基于条件的模式替换,即在且仅在满足给定条件的情况下,在空格内加上一个月的缩写。例如,使条件如下:“以数字开头并以字母开头”。
我尝试了stringr
程序包,但是未能合并功能str_replace_all()
和str_locate_all()
:
# Input:
txt = "START1SEP2 1DECX JANEND"
# Desired output:
# "START1SEP2 1 DEC X JANEND"
# (A) What I could do without checking the condition:
library(stringr)
patt_month = paste("(", paste(toupper(month.abb), collapse = "|"), ")", sep='')
str_replace_all(string = txt, pattern = patt_month, replacement = " \\1 ")
# "START1 SEP 2 1 DEC X JAN END"
# (B) But I actually only need replacements inside the condition-based bounds:
str_locate_all(string = txt, pattern = paste("[0-9]", patt_month, "[A-Z]", sep=''))[[1]]
# start end
# [1,] 12 16
# To combine (A) and (B), I'm currently using an ugly for() loop not shown here and want to get rid of it
答案 0 :(得分:5)
您正在寻找环顾四周:
(?<=\d)DEC(?=[A-Z])
(?=...)
是pos。前瞻(?!...)
是一个否定词。前瞻(?<=...)
是pos。向后看(?<!...)
是一个否定词。向后看答案 1 :(得分:0)
基本R版本
patt_month <- capture.output(cat(toupper(month.abb),"|"))#concatenate all month.abb with OR
pat <- paste0("(\\s\\d)(", patt_month, ")([A-Z]\\s)")#make it a three group thing
gsub(pattern = pat, replacement = "\\1 \\2 \\3", txt, perl =TRUE)#same result as above
也可直接用于txt2 <- "START1SEP2 1JANY JANEND"
。
[1] "START1SEP2 1 JAN Y JANEND"