I got really confused about the usage of backreferences
strings <- c("^ab", "ab", "abc", "abd", "abe", "ab 12")
gsub("(ab) 12", "\\1 34", strings)
[1] "^ab" "ab" "abc" "abd" "abe" "ab 34"
gsub("(ab)12", "\\2 34", strings)
[1] "^ab" "ab" "abc" "abd" "abe" "ab 12"
I know \1 refers to the first subpattern (reading from the left), \2 refers to the second subpattern, and so on. But I dont know what this subpattern means. Why \1 and \2 give different output
gsub("(ab)", "\\1 34", strings)
[1] "^ab 34" "ab 34" "ab 34c" "ab 34d" "ab 34e" "ab 34 12"
Also, why I remove 12 after (ab) then it gives such result?
gsub("ab", "\\1 34", strings)
[1] "^ 34" " 34" " 34c" " 34d" " 34e" " 34 12"
Furthermore, what if ab has no parenthesis? What does it indicate?
I really messed up with backreference and hope someone could explain the logic clearly