我有一个包含数字、字母和空格的文本字符串。它的一些子字符串是月份的缩写。我想执行基于条件的模式替换,即当且仅当满足给定条件时,才将月份缩写括在空格中。例如,让条件如下:“前面是一个数字,后面是一个字母”。
我试过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
Run Code Online (Sandbox Code Playgroud)
您正在寻找环视:
(?<=\d)DEC(?=[A-Z])
Run Code Online (Sandbox Code Playgroud)
(?=...)是一个位置。展望(?!...)是否定的。展望(?<=...)是一个位置。向后看(?<!...)是否定的。向后看| 归档时间: |
|
| 查看次数: |
132 次 |
| 最近记录: |