我如何输出两个字符串的匹配部分,同时让%字符分隔所有非连续匹配部分? [处于保留状态]

问题描述 投票:-4回答:1

我希望我可以为我一直在从事的项目提供一些帮助。给定两个字符串,我想输出这些字符串的匹配部分。此外,我希望匹配输出的任何非连续部分都由%符号分隔。

例如,如果我的两个字符串输入是:

  • This is a test case see if it works
  • test case it hopefully works

然后我想要的输出将是:

  • test case%it%works

编辑:我已经写了我希望代码的结构,但需要一些帮助来微调确切的语法,我们将不胜感激。这是我认为可以完成的方式:

string1 = A1 cell
string2 = B1 cell
output = ""
counter = 0
if LENGTH(string1) < LENGTH(string2) then split_string=string1 and other_string=string2             '
ELSE split_string=string2 and other_string=string1
matchable_values=split(split_string)
for each element in matchable_values      
    if ISNUMBER(SEARCH(element, other_string,counter)) then 
       output = output & element & %   and counter = counter + 
       LENGTH(element) + 1
     ELSEIF counter = counter + LENGTH(element) + 1
next element

return output
excel vba excel-formula excel-2010 excel-2016
1个回答
4
投票

您尝试做的事不容易完成,并且您需要一些高级开发技能(dynamic programming中的基本知识非常有用)。

您尝试做的实际上与在生物信息学中比对DNA序列的想法相同。


所以您需要做的是同时获取两个字符串(序列)

This is a test case, see if it works
test case, it hopefully works

例如,使用Needleman–Wunsch algorithm对齐它们(有更多已知的算法来对齐):

This is a test case, see if it ----------works
----------test case, -------it hopefully works

然后检查哪些字符相同,所以结果将是…

----------test case, -------it ----------works

然后用%替换多个破折号,同时从末尾开始删除破折号。因此,您的最终结果将是:

test case, %it %works

请注意,对于您的问题,没有确定的结果。永远会有更多结果!如果您进行比对,可能会有不同的方法来比对2个序列。


所以上面对齐的Needleman Wunsch回溯看起来像这样:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.