根据条件替换字符串中匹配的模式

Dmi*_*nko 1 regex r

我有一个包含数字、字母和空格的文本字符串。它的一些子字符串是月份的缩写。我想执行基于条件的模式替换,即当且仅当满足给定条件时,才将月份缩写括在空格中。例如,让条件如下:“前面是一个数字,后面是一个字母”。

我试过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)

Jan*_*Jan 5

您正在寻找环视:

(?<=\d)DEC(?=[A-Z])
Run Code Online (Sandbox Code Playgroud)

在 regex101.com 上查看演示


环视确保匹配某个位置而不消耗任何字符。他们在某事面前可用。(称为lookbehind)或确保后面的任何内容都属于某种类型(称为lookahead)。两边都有正面和负面的,因此有四种类型(pos./neg.lookbehind/-ahead)。

一个简短的备忘录:

  • (?=...)是一个位置。展望
  • (?!...)是否定的。展望
  • (?<=...)是一个位置。向后看
  • (?<!...)是否定的。向后看