Python获得两个字符之间的最小单词

时间:2021-05-21 21:37:08

How do I find the smallest string between two characters in python. I tried this to extract words between [[ and ]] and put them into a list:

如何在python中找到两个字符之间的最小字符串。我试着在[[和]]之间提取单词并将它们放入一个列表中:

clause_text ="Bien_id  [[bien_id.name]] dossier [[dossier_id.name]] "
list_variables = re.findall(re.escape("[[")+"(.*)"+re.escape("]]"),clause_text)
print list_variables

In this case i need to get two variables on my list bien_id.name and dossier_id.name

在这种情况下,我需要在我的列表bien_id.name和dossier_id.name上获得两个变量

But the result i get is one variable:

但我得到的结果是一个变量:

[u'bien_id.name]] dfdfdf [[dossier_id.name']

It means that it takes the biggest word between brackets.Can you help me please to solve my problem?

这意味着它需要括号之间的最大字。你能帮我解决我的问题吗?

2 个解决方案

#1


8  

You are looking for a non-greedy regex. Use (.*?) instead of (.*)

你正在寻找一个非贪婪的正则表达式。使用(。*?)代替(。*)

#2


4  

list_variables = re.findall(re.escape("[[")+"(.*?)"+re.escape("]]"),clause_text)

                                                ^^

Make your quantifier non greedy

让你的量词不贪婪

#1


8  

You are looking for a non-greedy regex. Use (.*?) instead of (.*)

你正在寻找一个非贪婪的正则表达式。使用(。*?)代替(。*)

#2


4  

list_variables = re.findall(re.escape("[[")+"(.*?)"+re.escape("]]"),clause_text)

                                                ^^

Make your quantifier non greedy

让你的量词不贪婪