简体   繁体   English

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

[英]how to use the regex findall list

so i'm a js trainee and at my internship someone asked me to do something on the python code, but i never did something on Python so i'm a bit lost.. I would like to separate a string in differents block. 所以我是一名JS培训生,在实习期间,有人要求我对python代码做一些事情,但是我从未在Python上做过什么,所以我有点迷路了。我想将字符串分隔为differents块。

here is what i have : 这是我所拥有的:

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

what i would like is a regex that separate this string in differents parts : 我想要的是将这个字符串分为不同部分的正则表达式:

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

what i tried to do is that : 我试图做的是:

def getStructure(string):

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

but this gave me a list... and i don't understand how do i navigate through a list in python ... 但这给了我一个列表...我不明白如何在python中浏览列表...

[("#<start>", '\n', '#</start>'), ('#<azeaze>', '\n', '#</azeaze>'), ('#<sgdfs>', 'x', '#</sgdfs>')]

i tried : 我试过了 :

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

but it game me an error "too many values to unpack" 但它给我带来了一个错误:“无法解开太多的价值”

Can someone show me how i can do this? 有人可以告诉我我该怎么做吗?

Your print statement is a little faulty . 您的印刷声明有误。 Try this instead 试试这个

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

You are getting an error because, you are trying to get two values from a tuple having three elements 您收到错误是因为,您试图从具有三个元素的元组中获取两个值

for p1, strings in separatedString 

Here separatedString has 3 elements in each of its members 在这里, 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>')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在列表理解中使用正则表达式 re.compile Match() 或 findall() - How to use regex re.compile Match() or findall() in list comprehension 如何在python中的regex(findall)中使用枚举? - how to use enumerate with regex(findall) in python? 如何在findex + Python的正则表达式模式中使用{} - How to use {} in regex pattern with findall + Python Ansible 或 Python,如何在列表中使用 regex_findall 来返回以特定模式开头的单词 - Ansible or Python, how to use regex_findall on a list to return words that start with a certain pattern 如何从regex.findall的匹配中返回字典列表? - How to return a list of dictionaries from the match of regex.findall? 尝试使用正则表达式 findall 进行对话 - Trying to use regex findall for a dialogue 如何从Pandas Dataframe中提取RegEx并将其与re.findall结合使用 - How to Extract RegEx from Pandas Dataframe and use it with re.findall 正则表达式 findall() python 返回空列表 - Regex findall() python returning empty list regex.findall 返回一个字典而不是列表 - regex.findall returns a dict instead of list 尝试动态使用正则表达式 findall 来获取注册表项。 如何将 append 单转义为正则表达式字符串? - Trying to dynamically use regex findall to pick up registry keys. How to append single escape to regex string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM