Longest common prefix of two strings in bash

前端 未结 13 2253
一向
一向 2020-12-03 00:49

I have two strings. For the sake of the example they are set like this:

string1=\"test toast\"
string2=\"test test\"

What I want is to find

相关标签:
13条回答
  • 2020-12-03 01:47

    If using other languages, how about python:

    cmnstr() { python -c "from difflib import SequenceMatcher
    s1, s2 = ('''$1''', '''$2''')
    m = SequenceMatcher(None,s1,s2).find_longest_match(0,len(s1),0,len(s2))
    if m.a == 0: print(s1[m.a: m.a+m.size])"
    }
    $ cmnstr x y
    $ cmnstr asdfas asd
    asd
    

    (h/t to @RickardSjogren's answer to stack overflow 18715688)

    0 讨论(0)
提交回复
热议问题