给定一个字符串 #abcde#jfdkjfd
,我怎样才能得到两个 #
之间的字符串?我还希望如果没有 #
对(意味着没有 #
或只有一个 #
),该函数将返回 None
.
最佳答案
>>> import re
>>> s = "abc#def#ghi#jkl"
>>> re.findall(r"(?<=#)[^#]+(?=#)", s)
['def', 'ghi']
解释:
(?<=#) # Assert that the previous character is a #
[^#]+ # Match 1 or more non-# characters
(?=#) # Assert that the next character is a #
关于使用正则表达式解析python字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8559398/