R: find largest common substring starting at the beginning

后端 未结 11 2697
星月不相逢
星月不相逢 2021-02-19 18:33

I\'ve got 2 vectors:

word1 <- \"bestelling\"   
word2 <- \"bestelbon\"

Now I want to find the largest common substring that starts at the

11条回答
  •  [愿得一人]
    2021-02-19 18:48

    A bit of regex can do this:

    sub('^([^|]*)[^|]*(?:\\|\\1[^|]*)$', '\\1', paste0(word1, '|', word2))
    #[1] "bestel"
    

    I used | as a separator - pick one that makes sense for your strings.

提交回复
热议问题