It looks like you're a bit mixed up about how to use look-around assertions. The pattern you're using, "(?<=.{4})"
, is a look-behind assertion that says "find me all inter-character spaces that are preceded by four characters of any kind", which is not what you really want.
The pattern you actually want, "(?=.{4}$)"
, is a look-ahead assertion that finds the single inter-character space that is followed by four characters of any kind followed by the end of the string.
There is, unfortunately, an unpleasant twist. For reasons discussed in the answers to this question, strsplit()
interacts oddly with look-ahead assertions; as a result, the pattern you'll actually need is "(?<=.)(?=.{4}$)"
. Here's what that looks like in action:
x <- c("Samp003A", "Sam003A")
strsplit(x, split="(?<=.)(?=.{4}$)", perl=T)
# [[1]]
# [1] "Samp" "003A"
#
# [[2]]
# [1] "Sam" "003A"
If all you really want are the final four characters of each entry, maybe just use substr()
, like this:
x <- c("Samp003A", "Sam003A")
substr(x, start=nchar(x)-3, stop=nchar(x))
# [1] "003A" "003A"