我想從字符串'/?user=hello&user=man&user='
中提取'user='
之後的任何字符串。在這種情況下,我會得到我'hello'
,'man'
和''
。使用正則表達式在'/?user = hello&user = man&user ='中查找'hello','man'和''
我被困在這裏:
>>> import re
>>> s = '/?user=hello&user=man&user='
>>> re.findall("user=(.*)",s)
['hello&user=man&user=']
我能找到我想要的東西如果user=
的最後一次出現也與&
結束,但沒有任何人知道如何在這個字符串找到['hello', 'man', '']
?
小心你的貪婪搜索參數。 – L0j1k
'。*'是一個貪婪的量詞,它總是試圖匹配它所能達到的最大值,而不是'。*'你應該使用'。*?'非貪婪版本。所以,試試這個're.findall(r'user =(。*?)&',s)' – RanRag