I have a string id <- "Hello these are words N12345678 hooray how fun"
.
I would like to extract just N12345678 from this string.
So far I have used strsplit(id, " ")
. Now I have
>id
>[[1]]
>[1] "Hello" "these" "are" "words" "N12345678" "hooray" "how"
>[8] "fun"
Which is of type list and of length 1 (despite apparently having 8 elements?)
If I then use id <- id[grep("^[N][0-9]",id)]
,
id is an empty list.
I think what I need to do is split the string into a list of length 8 with each element as a substring and then grep should be able to pick out the pattern, but I'm not sure how to go about that.
If you insist on using
strsplit
. I think this can solve the problem:Notice that I haven't changed your regex. It could be more precise expression such as
^N\\d+$
.