如何使用正则表达式 findall 列表

how to use the regex findall list

所以我是一名 js 实习生,在我实习期间有人让我在 python 代码上做一些事情,但我从来没有在 Python 上做过什么所以我有点迷茫。 . 我想在不同的块中分隔一个字符串。

这是我所拥有的:

    buffer = """
#<start>
    idothings
#</start>
#<params>
    otherthings
#</params>
#<end>
    andifinish
#</end>

我想要的是一个将这个字符串分成不同部分的正则表达式:

separatedString = [["#<start>,"idothings","#</start>"],["#<params>,"otherthings","#</params>"],["#<end>,"andifinish","#</end>"]]

我想做的是:

def getStructure(string):

    separatedString = re.findall('(#<.+?>)(.|\n)+?(#<\/.+?>)', string)
    return

但这给了我一个列表...我不明白如何在 python...

中浏览列表
[("#<start>", '\n', '#</start>'), ('#<azeaze>', '\n', '#</azeaze>'), ('#<sgdfs>', 'x', '#</sgdfs>')]

我试过了:

print '\n'.join(["%s a %s et %s" %(p1,p2,p3) for p1, strings in separatedString ])

但它让我犯了一个错误"too many values to unpack"

谁能告诉我怎么做?

你的打印语句有点错误 .试试这个

print '\n'.join(["%s a %s et %s" %(p1,p2,p3) for p1, p2, p3 in separatedString ])

您收到错误消息是因为您正试图从具有三个元素的元组中获取两个值

for p1, strings in separatedString 

此处separatedString的每个成员中都有3个元素

buffer = """#<start>
    idothings
#</start>
#<params>
    otherthings
#</params>
#<end>
    andifinish
#</end>"""

spl = buffer.splitlines()
print([spl[i:i+3] for i in range(0,len(spl),3)])
[['#<start>', '    idothings', '#</start>'], ['#<params>', '    otherthings', '#</params>'], ['#<end>', '    andifinish', '#</end>']]




spl = buffer.splitlines()
sliced = [spl[i:i+3] for i in range(0,len(spl),3)]

for a,b,c in sliced:
    print(a.strip(),b.strip(),c.striip())
('#<start>', 'idothings', '#</start>')
('#<params>', 'otherthings', '#</params>')
('#<end>', 'andifinish', '#</end>')