I have this data
datos <- read.table(text = 'col1
"Casa Verde 2015_HU/AJE/9999/2017"
"Casa Roja 2015_HU/AJE/0263/2000"
"Casa Verde_U/AJE/4441/2017"
"Casa Verde_HU/AJE/22225/2017_F4"
"Casa Azul 5_HU/AJE/1111/2017_B2"
"Casa Verde_HU/AJE/0000/2017_76"
', header = TRUE, stringsAsFactors = FALSE)
The third row Casa Verde_U/AJE/4441/2017
starts with U/AJE and I want to change it to HU/AJE
but if i do
datos$col2 <- gsub("U/AJE", "HU/AJE", datos$col1)
The HUs in the rest of the rows become HHUs. I want it to change only the one that starts with U.
I have tried with ^U/AJE, but neither.
Search directly for the pattern
_U
or 'U' and replace theU
withHU
:This is a match and capture pattern, it "matches" the space or hyphen, plus the text
U/AJE
and with\\1HU\\2
, we just replace theU
withHU
. The patterns\\1
and\\2
add the captured texts([_| ])
and(/AJE)