简体   繁体   中英

How do I find a common substring between two strings?

y = "dee"
x = "ree"

I want to right a function that accepts 2 parameters like above and identify the same patterns they have in this case its only

ee

Assuming your two input strings be only text, we can try concatenating the strings together with some unique separator, and then use a regex approach:

y = "dee"
x = "ree"
inp = x + '|' + y
output = re.findall(r'^.*?([^|]+).*\|.*?\1.*$', inp)
print(output)  # ['ee']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM