获取子字符串周围的设置数字半径

时间:2016-04-03 01:47:49

标签: python find substring

在Python中,我如何在子字符串周围获得一定数量的字符?

例如,这是我的字符串:

string='Mad Max: Fury Road'

我想说我想在两侧将'ax: Fur'的四个字符添加到输出中,因此它会是'ad Max: Fury Ro'

如果'Fury Road'中要查找的子字符串为string,那么输出将为'ax: Fury Road',并且会忽略右侧没有要添加的内容

2 个答案:

答案 0 :(得分:3)

str.partition非常方便:

def get_sub(string, sub, length):
    before, search, after = string.partition(sub)
    if not search:
        raise ValueError("substring not found")
    return before[-length:] + sub + after[:length]

您也可以在before语句中返回if,而不是提出ValueError。这将使字符串保持不变。用法:

print(get_sub("Mad Max: Fury Road", "Fury Road", 4))
#ax: Fury Road
print(get_sub("Mad Max: Fury Road", "Fu", 4))
#ax: Fury R

答案 1 :(得分:0)

您还可以使用.split()获取子字符串前后的字符串,然后返回两者的部分内容:

def get_sub_and_surrounding(string,sub,length):
    before,after = string.split(sub,1) #limit to only one split
    return before[-length:] + sub + after[:length]

值得注意的是,在这种情况下,如果sub实际上不是子字符串,那么第一行将引发ValueError

但你可以得到准确的索引,将它拆分为:

def get_sub_and_surrounding(string,sub,length):
    i_start = string.index(sub) #index of the start of the substring
    i_end = i_start + len(sub) #index of the end of the substring (one after)

    my_start = max(0, i_start -length)
    # ^prevents use of negative indices from counting
    # from the end of the string by accident

    my_end = min(len(string), i_end+length) #this part isn't actually necessary, "a"[:100] just goes to the end of the string

    return string[my_start : my_end]

在这种情况下,string.index(sub)如果sub不在字符串中,则会引发ValueError