首页 > 解决方案 > 在python中查找2个字符串之间的匹配表达式

问题描述

例如

string1 = "Hello this is the first string, it will be used in this project"

string2 = "Hello this is string2 and i will not be used in this project"

我希望这个脚本从 string2 返回 string1 中的所有匹配表达式

输出应该是:[Hello this is, will, be used in this project]

我已经制作了一个自定义函数来使用循环来实现这一点,但我担心它太慢了

这是我的自定义函数,我确信它在很多方面都很糟糕,但我仍在学习 python。

from re import search
fullstring = "Hello this is the first string, it will be used in this project"
substring = "Hello this is string2 and i will not be used in this project"
last_found_str = ""
#while string is not empty keep looping
while test_substring:
    print("Searching for {}".format(test_substring))

    #if substring in fullstring
    if search(r'\b{}\b'.format(test_substring), fullstring):
        print("Found! : {}".format(test_substring))
        #add to list
        phrases_list.append(test_substring)
        #remove the found substring
        substring = substring.replace(test_substring,'')
        if substring == " ":
            break
        test_substring = substring
        #this is the substring from the last found results
        last_found_str = substring
    else:
        #if only one word is left
        if len(test_substring.split()) == 1:
            print("{} is not found".format(test_substring))
            if len(last_found_str.split()) > 1:
                #if substring from last found results is more than 1 word, remove the first word and loop through the rest
                substring = substring.replace(r'\b{}\b'.format(test_substring),'')
                test_substring = last_found_str.partition(' ')[2]
                last_found_str = test_substring
            else:
                #if its a word or less then stop looping
                break
        else:
            #if there is more than one word remove the last word and keep looping
            test_substring = test_substring.rsplit(' ', 1)[0] 

print(phrases_list)

标签: pythonpython-3.xalgorithmpython-2.7replace

解决方案


这是应该完成这项工作的列表理解:

[i for i in string1.split() if i in string2.split()]

输出:

['Hello', 'this', 'is', 'will', 'be', 'used', 'in', 'this', 'project']

推荐阅读