If I understand you correctly this is what you are trying to do, if not, then please provide a REPRoducible EXample (reprex) illustrating your issue
library(tidyverse)
# Sample data to illustrate the problem
df <- data.frame(stringsAsFactors = FALSE,
section = c("ALICE_!AAAA", "!AAAA_!AAAB", "!AAAB_NADIR",
"NADIR_!AAAC", "!AAAC_MANDI")
)
df %>%
mutate(beginning = str_extract(section, "^[^!]+(?=_)"),
end = str_extract(section, "(?<=_)[^!]+$")) %>%
fill(beginning, .direction = "down") %>%
fill(end, .direction = "up") %>%
transmute(line = paste(beginning, end, sep = "_"), section = section)
#> line section
#> 1 ALICE_NADIR ALICE_!AAAA
#> 2 ALICE_NADIR !AAAA_!AAAB
#> 3 ALICE_NADIR !AAAB_NADIR
#> 4 NADIR_MANDI NADIR_!AAAC
#> 5 NADIR_MANDI !AAAC_MANDI
Created on 2019-10-07 by the reprex package (v0.3.0.9000)