longest common substring in R finding non-contiguous matches between the two strings

Here are three possible solutions. library(stringi) library(stringdist) a <- “hello” b <- “hel123l5678o” ## get all forward substrings of ‘b’ sb <- stri_sub(b, 1, 1:nchar(b)) ## extract them from ‘a’ if they exist sstr <- na.omit(stri_extract_all_coll(a, sb, simplify=TRUE)) ## match the longest one sstr[which.max(nchar(sstr))] # [1] “hel” There are also adist() and agrep() in base … Read more