Another option is to use regular expressions, a less readable but more direct approach.
example_matrix <- structure(c(1, 2), .Dim = 1:2, .Dimnames = list(NULL, c("ABCD-123A-1234-AB1AB1",
"ABCD-123AX-1234-AB1AB1")))
colnames(example_matrix) <- regmatches(colnames(example_matrix), regexpr("^.{4}-[^-]{4,5}", colnames(example_matrix)))
example_matrix
#> ABCD-123A ABCD-123AX
#> [1,] 1 2
Created on 2022-07-26 by the reprex package (v2.0.1)