【问题标题】:Python - Capture multiple sub strings between multiple sub string Sizes aPython - 在多个子字符串之间捕获多个子字符串
【发布时间】:2018-11-02 16:42:56
【问题描述】:

我拥有的数据格式非常糟糕 .txt 。我试图捕捉这些开始和结束字符串之间的完整单词/句子。现在,我在一个文本中发现了大约 4 种类型的子字符串模式。我正在尝试捕获这些多个开始和结束子字符串之间的字符串。我能够正确捕获第一个字符串出现,但不能正确捕获第二个、第三个、...等。

开始和结束字符串: FOO,酒吧,酒吧,酒吧2

text = 'I do not want this FOO string1 BARS I do not want this FOO string 2 BAR I do not want this FOO string3 BAR2 I do not want this FOO string4 BARS '


snippet1 = text[text.index('FOO')+len('FOO'):text.index('BARS')] \
            if text[text.index('FOO')+len('FOO'):text.index('BARS')] else ''

snippet2 = text[text.index('FOO')+len('FOO'):text.index('BAR')] \
            if text[text.index('FOO')+len('FOO'):text.index('BAR')] else ''

snippet3 = text[text.index('FOO')+len('FOO'):text.index('BAR2')] \
            if text[text.index('FOO')+len('FOO'):text.index('BAR2')] else ''

# print(type(snippet1))
print('')
print('snippet1:',snippet1) #Output: snippet1:  string1
print('')
print('snippet2',snippet2) # Output: snippet2  string1
print('')
print('snippet3',snippet3) # Output: snippet3  string1 BARS I do not want this FOO string2 BAR I do not want this FOO string3

# How do I get this output? Is it possible to code this?
snippet1:  string1
snippet2:  string2
snippet3:  string3

【问题讨论】:

    标签: python regex string python-3.x


    【解决方案1】:

    IIUC:您可以使用regex

    import re
    txt='I do not want this FOO string1 BARS I do not want this FOO string 2 BAR I do not want this FOO string3 BAR2 I do not want this FOO string4 BARS '
    re.findall('FOO(.*?)BAR', txt)
    

    会产生这样的匹配字符串列表:

    [' string1 ', ' string 2 ', ' string3 ', ' string4 ']
    

    更新匹配多个关键字:

    import re
    txt='I do not want this FOO string1 BARS I do not want this FOO string 2 SECTION I do not want this FOO string3 BAR2 I do not want this FOO string4 BARS'
    re.findall('FOO(.*?)[BAR|SECTION]', txt)
    

    将导致:

    [' string1 ', ' string 2 ', ' string3 ', ' string4 ']
    

    【讨论】:

    • 可以去掉空格:re.findall('FOO\s*(.*?)\s*BAR', text)
    【解决方案2】:

    这样的东西就是你想要的。

    def find_substrings(text, start_marker, end_marker):
        index = 0
        results = []
    
        while True:
            index = text.find(start_marker, index)
            if index == -1: # If the start string wasn't found then there are no more instances left in the string
                break
            index2 = text.find(end_marker, index+len(start_marker))
            if index2 == -1: # Sub string was not terminated. 
                break
            results.append(text[index+len(start_marker):index2])
            index = index2 + len(end_marker)
    
        return results
    

    目前您正在使用索引(类似于 find,但如果找不到任何内容则会抛出错误),但它每次都会开始查看字符串的开头。

    text = 'I do not want this FOO string1 BARS I do not want this FOO string 2 BAR I do not want this FOO string3 BAR2 I do not want this FOO string4 BARS '
    find_substrings(text, "FOO ", " BAR")
    

    将返回

    ['string1', 'string 2', 'string3', 'string4']
    

    【讨论】:

    • 两个答案都有帮助,但@TobySuch 的答案很适合我的代码。非常感谢!!!我花了太长时间来解决这个问题!
    猜你喜欢
    • 2021-03-16
    • 2015-06-24
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 2019-12-04
    • 2017-08-28
    • 1970-01-01
    • 2019-06-26
    相关资源
    最近更新 更多