I have string and character vector. I would like to find all strings in character vector matching as much as possible characters from beging of string.
For example:
s <- "abs"
vc <- c("ab","bb","abc","acbd","dert")
result <- c("ab","abc")
String s should be matched exactly up to first K characters. I want match for as much as possible (max K<=length(s)).
Here there is no match for “abs” (grep(“abs”,vc)), but for “ab” there are two matches (result <-grep(“ab”,vc)).
Another interpretation:
Since we are considering only beginnings of strings, in the first case the longest match was
"ab", even though there is"abwabsabs"which has"abs".Edit: Here is a “single pattern” solution, possibly it could be more concise, but here we go…